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

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, 3 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 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 break; 545 break;
545 case WebContextMenuData::MediaTypePlugin: 546 case WebContextMenuData::MediaTypePlugin:
546 AppendPluginItems(); 547 AppendPluginItems();
547 break; 548 break;
548 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED 549 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
549 case WebContextMenuData::MediaTypeFile: 550 case WebContextMenuData::MediaTypeFile:
550 break; 551 break;
551 #endif 552 #endif
552 } 553 }
553 554
555 if (params_.is_editable) {
556 // Add a menu item that shows suggestions from the Spelling service.
557 PrefService* pref = profile_->GetPrefs();
558 bool use_spelling_service =
559 pref && pref->GetBoolean(prefs::kSpellCheckUseSpellingService);
560 if (use_spelling_service) {
561 if (!spelling_menu_observer_.get())
562 spelling_menu_observer_.reset(new SpellingMenuObserver(this));
563
564 if (spelling_menu_observer_.get())
565 observers_.AddObserver(spelling_menu_observer_.get());
566 }
567 }
568
569 // Ask our observers to add their menu items.
570 FOR_EACH_OBSERVER(RenderViewContextMenuObserver, observers_,
571 InitMenu(params_));
572
554 if (params_.is_editable) 573 if (params_.is_editable)
555 AppendEditableItems(); 574 AppendEditableItems();
556 else if (has_selection) 575 else if (has_selection)
557 AppendCopyItem(); 576 AppendCopyItem();
558 577
559 if (has_selection) 578 if (has_selection)
560 AppendSearchProvider(); 579 AppendSearchProvider();
561 580
562 if (!IsDevToolsURL(params_.page_url)) 581 if (!IsDevToolsURL(params_.page_url))
563 AppendAllExtensionItems(); 582 AppendAllExtensionItems();
564 583
565 AppendDeveloperItems(); 584 AppendDeveloperItems();
566 } 585 }
567 586
568 void RenderViewContextMenu::LookUpInDictionary() { 587 void RenderViewContextMenu::LookUpInDictionary() {
569 // Used only in the Mac port. 588 // Used only in the Mac port.
570 NOTREACHED(); 589 NOTREACHED();
571 } 590 }
572 591
592 void RenderViewContextMenu::AddMenuItem(int command_id,
593 const string16& title) {
594 menu_model_.AddItem(command_id, title);
595 }
596
597 void RenderViewContextMenu::UpdateMenuItem(int command_id,
598 bool enabled,
599 const string16& label) {
600 // This function needs platform-specific implementation.
601 NOTIMPLEMENTED();
602 }
603
604
605 RenderViewHost* RenderViewContextMenu::GetRenderViewHost() const {
606 return source_tab_contents_->render_view_host();
607 }
608
609 Profile* RenderViewContextMenu::GetProfile() const {
610 return profile_;
611 }
612
573 bool RenderViewContextMenu::AppendCustomItems() { 613 bool RenderViewContextMenu::AppendCustomItems() {
574 size_t total_items = 0; 614 size_t total_items = 0;
575 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this, 615 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this,
576 &menu_model_); 616 &menu_model_);
577 return total_items > 0; 617 return total_items > 0;
578 } 618 }
579 619
580 void RenderViewContextMenu::AppendDeveloperItems() { 620 void RenderViewContextMenu::AppendDeveloperItems() {
581 // Show Inspect Element in DevTools itself only in case of the debug 621 // Show Inspect Element in DevTools itself only in case of the debug
582 // devtools build. 622 // devtools build.
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 menu_model_.AddSeparator(); 826 menu_model_.AddSeparator();
787 827
788 // |spellcheck_host| can be null when the suggested word is 828 // |spellcheck_host| can be null when the suggested word is
789 // provided by Web SpellCheck API. 829 // provided by Web SpellCheck API.
790 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost(); 830 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost();
791 if (spellcheck_host && spellcheck_host->GetMetrics()) 831 if (spellcheck_host && spellcheck_host->GetMetrics())
792 spellcheck_host->GetMetrics()->RecordSuggestionStats(1); 832 spellcheck_host->GetMetrics()->RecordSuggestionStats(1);
793 } 833 }
794 834
795 // If word is misspelled, give option for "Add to dictionary" 835 // If word is misspelled, give option for "Add to dictionary"
796 if (!params_.misspelled_word.empty()) { 836 if (!spelling_menu_observer_.get() && !params_.misspelled_word.empty()) {
797 if (params_.dictionary_suggestions.empty()) { 837 if (params_.dictionary_suggestions.empty()) {
798 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, 838 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS,
799 l10n_util::GetStringUTF16( 839 l10n_util::GetStringUTF16(
800 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS)); 840 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS));
801 } 841 }
802 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY, 842 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY,
803 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY); 843 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY);
804 menu_model_.AddSeparator(); 844 menu_model_.AddSeparator();
805 } 845 }
806 846
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 ExtensionMenuItem* item = manager->GetItemById(i->second); 993 ExtensionMenuItem* item = manager->GetItemById(i->second);
954 if (item) 994 if (item)
955 return item; 995 return item;
956 } 996 }
957 return NULL; 997 return NULL;
958 } 998 }
959 999
960 // Menu delegate functions ----------------------------------------------------- 1000 // Menu delegate functions -----------------------------------------------------
961 1001
962 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const { 1002 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
1003 // If this command is is added by one of our observers, we dispatch it to the
1004 // observer.
1005 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
1006 RenderViewContextMenuObserver* observer;
1007 while ((observer = it.GetNext()) != NULL) {
1008 if (observer->IsCommandIdSupported(id))
1009 return observer->IsCommandIdEnabled(id);
1010 }
1011
963 if (id == IDC_PRINT && 1012 if (id == IDC_PRINT &&
964 (source_tab_contents_->content_restrictions() & 1013 (source_tab_contents_->content_restrictions() &
965 CONTENT_RESTRICTION_PRINT)) { 1014 CONTENT_RESTRICTION_PRINT)) {
966 return false; 1015 return false;
967 } 1016 }
968 1017
969 if (id == IDC_SAVE_PAGE && 1018 if (id == IDC_SAVE_PAGE &&
970 (source_tab_contents_->content_restrictions() & 1019 (source_tab_contents_->content_restrictions() &
971 CONTENT_RESTRICTION_SAVE)) { 1020 CONTENT_RESTRICTION_SAVE)) {
972 return false; 1021 return false;
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1321 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) || 1370 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) ||
1322 (id >= IDC_SPELLCHECK_LANGUAGES_LAST)) 1371 (id >= IDC_SPELLCHECK_LANGUAGES_LAST))
1323 return false; 1372 return false;
1324 1373
1325 std::vector<std::string> languages; 1374 std::vector<std::string> languages;
1326 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) == 1375 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) ==
1327 (id - IDC_SPELLCHECK_LANGUAGES_FIRST); 1376 (id - IDC_SPELLCHECK_LANGUAGES_FIRST);
1328 } 1377 }
1329 1378
1330 void RenderViewContextMenu::ExecuteCommand(int id) { 1379 void RenderViewContextMenu::ExecuteCommand(int id) {
1380 // If this command is is added by one of our observers, we dispatch it to the
1381 // observer.
1382 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
1383 RenderViewContextMenuObserver* observer;
1384 while ((observer = it.GetNext()) != NULL) {
1385 if (observer->IsCommandIdSupported(id))
1386 return observer->ExecuteCommand(id);
1387 }
1388
1331 // Check to see if one of the spell check language ids have been clicked. 1389 // Check to see if one of the spell check language ids have been clicked.
1332 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 1390 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
1333 id < IDC_SPELLCHECK_LANGUAGES_LAST) { 1391 id < IDC_SPELLCHECK_LANGUAGES_LAST) {
1334 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST; 1392 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST;
1335 std::vector<std::string> languages; 1393 std::vector<std::string> languages;
1336 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages); 1394 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages);
1337 if (language_number < languages.size()) { 1395 if (language_number < languages.size()) {
1338 StringPrefMember dictionary_language; 1396 StringPrefMember dictionary_language;
1339 dictionary_language.Init(prefs::kSpellCheckDictionary, 1397 dictionary_language.Init(prefs::kSpellCheckDictionary,
1340 profile_->GetPrefs(), NULL); 1398 profile_->GetPrefs(), NULL);
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1854 g_browser_process->clipboard()); 1912 g_browser_process->clipboard());
1855 } 1913 }
1856 1914
1857 void RenderViewContextMenu::MediaPlayerActionAt( 1915 void RenderViewContextMenu::MediaPlayerActionAt(
1858 const gfx::Point& location, 1916 const gfx::Point& location,
1859 const WebMediaPlayerAction& action) { 1917 const WebMediaPlayerAction& action) {
1860 RenderViewHost* rvh = source_tab_contents_->render_view_host(); 1918 RenderViewHost* rvh = source_tab_contents_->render_view_host();
1861 rvh->Send(new ViewMsg_MediaPlayerActionAt( 1919 rvh->Send(new ViewMsg_MediaPlayerActionAt(
1862 rvh->routing_id(), location, action)); 1920 rvh->routing_id(), location, action));
1863 } 1921 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698