Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: chrome/browser/tab_contents/render_view_context_menu.cc

Issue 7713033: Integrate the Spelling service to Chrome. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 7
8 #include "chrome/browser/tab_contents/render_view_context_menu.h" 8 #include "chrome/browser/tab_contents/render_view_context_menu.h"
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 18 matching lines...) Expand all
29 #include "chrome/browser/prefs/pref_member.h" 29 #include "chrome/browser/prefs/pref_member.h"
30 #include "chrome/browser/prefs/pref_service.h" 30 #include "chrome/browser/prefs/pref_service.h"
31 #include "chrome/browser/profiles/profile.h" 31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_io_data.h" 32 #include "chrome/browser/profiles/profile_io_data.h"
33 #include "chrome/browser/search_engines/template_url.h" 33 #include "chrome/browser/search_engines/template_url.h"
34 #include "chrome/browser/search_engines/template_url_service.h" 34 #include "chrome/browser/search_engines/template_url_service.h"
35 #include "chrome/browser/search_engines/template_url_service_factory.h" 35 #include "chrome/browser/search_engines/template_url_service_factory.h"
36 #include "chrome/browser/spellchecker/spellcheck_host.h" 36 #include "chrome/browser/spellchecker/spellcheck_host.h"
37 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" 37 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
38 #include "chrome/browser/spellchecker/spellchecker_platform_engine.h" 38 #include "chrome/browser/spellchecker/spellchecker_platform_engine.h"
39 #include "chrome/browser/tab_contents/spelling_menu_observer.h"
39 #include "chrome/browser/translate/translate_manager.h" 40 #include "chrome/browser/translate/translate_manager.h"
40 #include "chrome/browser/translate/translate_prefs.h" 41 #include "chrome/browser/translate/translate_prefs.h"
41 #include "chrome/browser/translate/translate_tab_helper.h" 42 #include "chrome/browser/translate/translate_tab_helper.h"
42 #include "chrome/browser/ui/browser.h" 43 #include "chrome/browser/ui/browser.h"
43 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" 44 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
44 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 45 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
45 #include "chrome/common/chrome_constants.h" 46 #include "chrome/common/chrome_constants.h"
46 #include "chrome/common/chrome_switches.h" 47 #include "chrome/common/chrome_switches.h"
47 #include "chrome/common/pref_names.h" 48 #include "chrome/common/pref_names.h"
48 #include "chrome/common/print_messages.h" 49 #include "chrome/common/print_messages.h"
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 break; 527 break;
527 case WebContextMenuData::MediaTypePlugin: 528 case WebContextMenuData::MediaTypePlugin:
528 AppendPluginItems(); 529 AppendPluginItems();
529 break; 530 break;
530 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED 531 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
531 case WebContextMenuData::MediaTypeFile: 532 case WebContextMenuData::MediaTypeFile:
532 break; 533 break;
533 #endif 534 #endif
534 } 535 }
535 536
537 #if defined(GOOGLE_CHROME_BUILD)
538 if (params_.is_editable) {
539 // Add a menu item that shows suggestions from the Spelling service.
540 PrefService* pref = profile_->GetPrefs();
541 bool use_spelling_service =
542 pref && pref->GetBoolean(prefs::kSpellCheckUseSpellingService);
543 if (use_spelling_service) {
544 if (!spelling_menu_observer_.get())
545 spelling_menu_observer_.reset(new SpellingMenuObserver(this));
546
547 if (spelling_menu_observer_.get())
548 observers_.AddObserver(spelling_menu_observer_.get());
549 }
550 }
551 #endif
552
553 // Ask our observers to add their menu items.
554 FOR_EACH_OBSERVER(RenderViewContextMenuObserver, observers_,
555 InitMenu(params_));
556
536 if (params_.is_editable) 557 if (params_.is_editable)
537 AppendEditableItems(); 558 AppendEditableItems();
538 else if (has_selection) 559 else if (has_selection)
539 AppendCopyItem(); 560 AppendCopyItem();
540 561
541 if (has_selection) 562 if (has_selection)
542 AppendSearchProvider(); 563 AppendSearchProvider();
543 564
544 if (!IsDevToolsURL(params_.page_url)) 565 if (!IsDevToolsURL(params_.page_url))
545 AppendAllExtensionItems(); 566 AppendAllExtensionItems();
546 567
547 AppendDeveloperItems(); 568 AppendDeveloperItems();
548 } 569 }
549 570
550 void RenderViewContextMenu::LookUpInDictionary() { 571 void RenderViewContextMenu::LookUpInDictionary() {
551 // Used only in the Mac port. 572 // Used only in the Mac port.
552 NOTREACHED(); 573 NOTREACHED();
553 } 574 }
554 575
576 void RenderViewContextMenu::AddMenuItem(int command_id,
577 const string16& title) {
578 menu_model_.AddItem(command_id, title);
579 }
580
581 void RenderViewContextMenu::UpdateMenuItem(int command_id,
582 bool enabled,
583 const string16& label) {
584 // This function needs platform-specific implementation.
585 NOTIMPLEMENTED();
586 }
587
588
589 RenderViewHost* RenderViewContextMenu::GetRenderViewHost() const {
590 return source_tab_contents_->render_view_host();
591 }
592
593 Profile* RenderViewContextMenu::GetProfile() const {
594 return profile_;
595 }
596
555 bool RenderViewContextMenu::AppendCustomItems() { 597 bool RenderViewContextMenu::AppendCustomItems() {
556 size_t total_items = 0; 598 size_t total_items = 0;
557 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this, 599 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this,
558 &menu_model_); 600 &menu_model_);
559 return total_items > 0; 601 return total_items > 0;
560 } 602 }
561 603
562 void RenderViewContextMenu::AppendDeveloperItems() { 604 void RenderViewContextMenu::AppendDeveloperItems() {
563 // Show Inspect Element in DevTools itself only in case of the debug 605 // Show Inspect Element in DevTools itself only in case of the debug
564 // devtools build. 606 // devtools build.
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 menu_model_.AddSeparator(); 807 menu_model_.AddSeparator();
766 808
767 // |spellcheck_host| can be null when the suggested word is 809 // |spellcheck_host| can be null when the suggested word is
768 // provided by Web SpellCheck API. 810 // provided by Web SpellCheck API.
769 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost(); 811 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost();
770 if (spellcheck_host && spellcheck_host->GetMetrics()) 812 if (spellcheck_host && spellcheck_host->GetMetrics())
771 spellcheck_host->GetMetrics()->RecordSuggestionStats(1); 813 spellcheck_host->GetMetrics()->RecordSuggestionStats(1);
772 } 814 }
773 815
774 // If word is misspelled, give option for "Add to dictionary" 816 // If word is misspelled, give option for "Add to dictionary"
775 if (!params_.misspelled_word.empty()) { 817 if (!spelling_menu_observer_.get() && !params_.misspelled_word.empty()) {
776 if (params_.dictionary_suggestions.empty()) { 818 if (params_.dictionary_suggestions.empty()) {
777 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, 819 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS,
778 l10n_util::GetStringUTF16( 820 l10n_util::GetStringUTF16(
779 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS)); 821 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS));
780 } 822 }
781 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY, 823 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY,
782 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY); 824 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY);
783 menu_model_.AddSeparator(); 825 menu_model_.AddSeparator();
784 } 826 }
785 827
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 ExtensionMenuItem* item = manager->GetItemById(i->second); 974 ExtensionMenuItem* item = manager->GetItemById(i->second);
933 if (item) 975 if (item)
934 return item; 976 return item;
935 } 977 }
936 return NULL; 978 return NULL;
937 } 979 }
938 980
939 // Menu delegate functions ----------------------------------------------------- 981 // Menu delegate functions -----------------------------------------------------
940 982
941 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const { 983 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
984 // If this command is is added by one of our observers, we dispatch it to the
985 // observer.
986 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
987 RenderViewContextMenuObserver* observer;
988 while ((observer = it.GetNext()) != NULL) {
989 if (observer->IsCommandIdSupported(id))
990 return observer->IsCommandIdEnabled(id);
991 }
992
942 if (id == IDC_PRINT && 993 if (id == IDC_PRINT &&
943 (source_tab_contents_->content_restrictions() & 994 (source_tab_contents_->content_restrictions() &
944 CONTENT_RESTRICTION_PRINT)) { 995 CONTENT_RESTRICTION_PRINT)) {
945 return false; 996 return false;
946 } 997 }
947 998
948 if (id == IDC_SAVE_PAGE && 999 if (id == IDC_SAVE_PAGE &&
949 (source_tab_contents_->content_restrictions() & 1000 (source_tab_contents_->content_restrictions() &
950 CONTENT_RESTRICTION_SAVE)) { 1001 CONTENT_RESTRICTION_SAVE)) {
951 return false; 1002 return false;
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) || 1351 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) ||
1301 (id >= IDC_SPELLCHECK_LANGUAGES_LAST)) 1352 (id >= IDC_SPELLCHECK_LANGUAGES_LAST))
1302 return false; 1353 return false;
1303 1354
1304 std::vector<std::string> languages; 1355 std::vector<std::string> languages;
1305 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) == 1356 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) ==
1306 (id - IDC_SPELLCHECK_LANGUAGES_FIRST); 1357 (id - IDC_SPELLCHECK_LANGUAGES_FIRST);
1307 } 1358 }
1308 1359
1309 void RenderViewContextMenu::ExecuteCommand(int id) { 1360 void RenderViewContextMenu::ExecuteCommand(int id) {
1361 // If this command is is added by one of our observers, we dispatch it to the
1362 // observer.
1363 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
1364 RenderViewContextMenuObserver* observer;
1365 while ((observer = it.GetNext()) != NULL) {
1366 if (observer->IsCommandIdSupported(id))
1367 return observer->ExecuteCommand(id);
1368 }
1369
1310 // Check to see if one of the spell check language ids have been clicked. 1370 // Check to see if one of the spell check language ids have been clicked.
1311 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 1371 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
1312 id < IDC_SPELLCHECK_LANGUAGES_LAST) { 1372 id < IDC_SPELLCHECK_LANGUAGES_LAST) {
1313 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST; 1373 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST;
1314 std::vector<std::string> languages; 1374 std::vector<std::string> languages;
1315 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages); 1375 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages);
1316 if (language_number < languages.size()) { 1376 if (language_number < languages.size()) {
1317 StringPrefMember dictionary_language; 1377 StringPrefMember dictionary_language;
1318 dictionary_language.Init(prefs::kSpellCheckDictionary, 1378 dictionary_language.Init(prefs::kSpellCheckDictionary,
1319 profile_->GetPrefs(), NULL); 1379 profile_->GetPrefs(), NULL);
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1833 g_browser_process->clipboard()); 1893 g_browser_process->clipboard());
1834 } 1894 }
1835 1895
1836 void RenderViewContextMenu::MediaPlayerActionAt( 1896 void RenderViewContextMenu::MediaPlayerActionAt(
1837 const gfx::Point& location, 1897 const gfx::Point& location,
1838 const WebMediaPlayerAction& action) { 1898 const WebMediaPlayerAction& action) {
1839 RenderViewHost* rvh = source_tab_contents_->render_view_host(); 1899 RenderViewHost* rvh = source_tab_contents_->render_view_host();
1840 rvh->Send(new ViewMsg_MediaPlayerActionAt( 1900 rvh->Send(new ViewMsg_MediaPlayerActionAt(
1841 rvh->routing_id(), location, action)); 1901 rvh->routing_id(), location, action));
1842 } 1902 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698