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

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 19 matching lines...) Expand all
30 #include "chrome/browser/prefs/pref_service.h" 30 #include "chrome/browser/prefs/pref_service.h"
31 #include "chrome/browser/printing/print_view_manager.h" 31 #include "chrome/browser/printing/print_view_manager.h"
32 #include "chrome/browser/profiles/profile.h" 32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/profiles/profile_io_data.h" 33 #include "chrome/browser/profiles/profile_io_data.h"
34 #include "chrome/browser/search_engines/template_url.h" 34 #include "chrome/browser/search_engines/template_url.h"
35 #include "chrome/browser/search_engines/template_url_service.h" 35 #include "chrome/browser/search_engines/template_url_service.h"
36 #include "chrome/browser/search_engines/template_url_service_factory.h" 36 #include "chrome/browser/search_engines/template_url_service_factory.h"
37 #include "chrome/browser/spellchecker/spellcheck_host.h" 37 #include "chrome/browser/spellchecker/spellcheck_host.h"
38 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" 38 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
39 #include "chrome/browser/spellchecker/spellchecker_platform_engine.h" 39 #include "chrome/browser/spellchecker/spellchecker_platform_engine.h"
40 #include "chrome/browser/tab_contents/spelling_menu_observer.h"
40 #include "chrome/browser/translate/translate_manager.h" 41 #include "chrome/browser/translate/translate_manager.h"
41 #include "chrome/browser/translate/translate_prefs.h" 42 #include "chrome/browser/translate/translate_prefs.h"
42 #include "chrome/browser/translate/translate_tab_helper.h" 43 #include "chrome/browser/translate/translate_tab_helper.h"
43 #include "chrome/browser/ui/browser.h" 44 #include "chrome/browser/ui/browser.h"
44 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" 45 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
45 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 46 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
46 #include "chrome/common/chrome_constants.h" 47 #include "chrome/common/chrome_constants.h"
47 #include "chrome/common/chrome_switches.h" 48 #include "chrome/common/chrome_switches.h"
48 #include "chrome/common/pref_names.h" 49 #include "chrome/common/pref_names.h"
49 #include "chrome/common/print_messages.h" 50 #include "chrome/common/print_messages.h"
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 break; 546 break;
546 case WebContextMenuData::MediaTypePlugin: 547 case WebContextMenuData::MediaTypePlugin:
547 AppendPluginItems(); 548 AppendPluginItems();
548 break; 549 break;
549 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED 550 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
550 case WebContextMenuData::MediaTypeFile: 551 case WebContextMenuData::MediaTypeFile:
551 break; 552 break;
552 #endif 553 #endif
553 } 554 }
554 555
556 if (params_.is_editable) {
557 // Add a menu item that shows suggestions from the Spelling service.
558 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
559 if (command_line.HasSwitch(switches::kExperimentalSpellcheckerFeatures)) {
560 PrefService* pref = profile_->GetPrefs();
561 bool use_spelling_service =
562 pref && pref->GetBoolean(prefs::kSpellCheckUseSpellingService);
563 if (use_spelling_service) {
564 if (!spelling_menu_observer_.get())
565 spelling_menu_observer_.reset(new SpellingMenuObserver(this));
566
567 if (spelling_menu_observer_.get())
568 observers_.AddObserver(spelling_menu_observer_.get());
569 }
570 }
571 }
572
573 // Ask our observers to add their menu items.
574 FOR_EACH_OBSERVER(RenderViewContextMenuObserver, observers_,
575 InitMenu(params_));
576
555 if (params_.is_editable) 577 if (params_.is_editable)
556 AppendEditableItems(); 578 AppendEditableItems();
557 else if (has_selection) 579 else if (has_selection)
558 AppendCopyItem(); 580 AppendCopyItem();
559 581
560 if (has_selection) 582 if (has_selection)
561 AppendSearchProvider(); 583 AppendSearchProvider();
562 584
563 if (!IsDevToolsURL(params_.page_url)) 585 if (!IsDevToolsURL(params_.page_url))
564 AppendAllExtensionItems(); 586 AppendAllExtensionItems();
565 587
566 AppendDeveloperItems(); 588 AppendDeveloperItems();
567 } 589 }
568 590
569 void RenderViewContextMenu::LookUpInDictionary() { 591 void RenderViewContextMenu::LookUpInDictionary() {
570 // Used only in the Mac port. 592 // Used only in the Mac port.
571 NOTREACHED(); 593 NOTREACHED();
572 } 594 }
573 595
596 void RenderViewContextMenu::AddMenuItem(int command_id,
597 const string16& title) {
598 menu_model_.AddItem(command_id, title);
599 }
600
601 void RenderViewContextMenu::UpdateMenuItem(int command_id,
602 bool enabled,
603 const string16& label) {
604 // This function needs platform-specific implementation.
605 NOTIMPLEMENTED();
606 }
607
608
609 RenderViewHost* RenderViewContextMenu::GetRenderViewHost() const {
610 return source_tab_contents_->render_view_host();
611 }
612
613 Profile* RenderViewContextMenu::GetProfile() const {
614 return profile_;
615 }
616
574 bool RenderViewContextMenu::AppendCustomItems() { 617 bool RenderViewContextMenu::AppendCustomItems() {
575 size_t total_items = 0; 618 size_t total_items = 0;
576 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this, 619 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this,
577 &menu_model_); 620 &menu_model_);
578 return total_items > 0; 621 return total_items > 0;
579 } 622 }
580 623
581 void RenderViewContextMenu::AppendDeveloperItems() { 624 void RenderViewContextMenu::AppendDeveloperItems() {
582 // Show Inspect Element in DevTools itself only in case of the debug 625 // Show Inspect Element in DevTools itself only in case of the debug
583 // devtools build. 626 // devtools build.
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 menu_model_.AddSeparator(); 830 menu_model_.AddSeparator();
788 831
789 // |spellcheck_host| can be null when the suggested word is 832 // |spellcheck_host| can be null when the suggested word is
790 // provided by Web SpellCheck API. 833 // provided by Web SpellCheck API.
791 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost(); 834 SpellCheckHost* spellcheck_host = profile_->GetSpellCheckHost();
792 if (spellcheck_host && spellcheck_host->GetMetrics()) 835 if (spellcheck_host && spellcheck_host->GetMetrics())
793 spellcheck_host->GetMetrics()->RecordSuggestionStats(1); 836 spellcheck_host->GetMetrics()->RecordSuggestionStats(1);
794 } 837 }
795 838
796 // If word is misspelled, give option for "Add to dictionary" 839 // If word is misspelled, give option for "Add to dictionary"
797 if (!params_.misspelled_word.empty()) { 840 if (!spelling_menu_observer_.get() && !params_.misspelled_word.empty()) {
798 if (params_.dictionary_suggestions.empty()) { 841 if (params_.dictionary_suggestions.empty()) {
799 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, 842 menu_model_.AddItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS,
800 l10n_util::GetStringUTF16( 843 l10n_util::GetStringUTF16(
801 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS)); 844 IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS));
802 } 845 }
803 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY, 846 menu_model_.AddItemWithStringId(IDC_SPELLCHECK_ADD_TO_DICTIONARY,
804 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY); 847 IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY);
805 menu_model_.AddSeparator(); 848 menu_model_.AddSeparator();
806 } 849 }
807 850
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 ExtensionMenuItem* item = manager->GetItemById(i->second); 997 ExtensionMenuItem* item = manager->GetItemById(i->second);
955 if (item) 998 if (item)
956 return item; 999 return item;
957 } 1000 }
958 return NULL; 1001 return NULL;
959 } 1002 }
960 1003
961 // Menu delegate functions ----------------------------------------------------- 1004 // Menu delegate functions -----------------------------------------------------
962 1005
963 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const { 1006 bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
1007 // If this command is is added by one of our observers, we dispatch it to the
1008 // observer.
1009 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
1010 RenderViewContextMenuObserver* observer;
1011 while ((observer = it.GetNext()) != NULL) {
1012 if (observer->IsCommandIdSupported(id))
1013 return observer->IsCommandIdEnabled(id);
1014 }
1015
964 if (id == IDC_PRINT && 1016 if (id == IDC_PRINT &&
965 (source_tab_contents_->content_restrictions() & 1017 (source_tab_contents_->content_restrictions() &
966 CONTENT_RESTRICTION_PRINT)) { 1018 CONTENT_RESTRICTION_PRINT)) {
967 return false; 1019 return false;
968 } 1020 }
969 1021
970 if (id == IDC_SAVE_PAGE && 1022 if (id == IDC_SAVE_PAGE &&
971 (source_tab_contents_->content_restrictions() & 1023 (source_tab_contents_->content_restrictions() &
972 CONTENT_RESTRICTION_SAVE)) { 1024 CONTENT_RESTRICTION_SAVE)) {
973 return false; 1025 return false;
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) || 1374 if ((id < IDC_SPELLCHECK_LANGUAGES_FIRST) ||
1323 (id >= IDC_SPELLCHECK_LANGUAGES_LAST)) 1375 (id >= IDC_SPELLCHECK_LANGUAGES_LAST))
1324 return false; 1376 return false;
1325 1377
1326 std::vector<std::string> languages; 1378 std::vector<std::string> languages;
1327 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) == 1379 return SpellCheckHost::GetSpellCheckLanguages(profile_, &languages) ==
1328 (id - IDC_SPELLCHECK_LANGUAGES_FIRST); 1380 (id - IDC_SPELLCHECK_LANGUAGES_FIRST);
1329 } 1381 }
1330 1382
1331 void RenderViewContextMenu::ExecuteCommand(int id) { 1383 void RenderViewContextMenu::ExecuteCommand(int id) {
1384 // If this command is is added by one of our observers, we dispatch it to the
1385 // observer.
1386 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
1387 RenderViewContextMenuObserver* observer;
1388 while ((observer = it.GetNext()) != NULL) {
1389 if (observer->IsCommandIdSupported(id))
1390 return observer->ExecuteCommand(id);
1391 }
1392
1332 // Check to see if one of the spell check language ids have been clicked. 1393 // Check to see if one of the spell check language ids have been clicked.
1333 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 1394 if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
1334 id < IDC_SPELLCHECK_LANGUAGES_LAST) { 1395 id < IDC_SPELLCHECK_LANGUAGES_LAST) {
1335 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST; 1396 const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST;
1336 std::vector<std::string> languages; 1397 std::vector<std::string> languages;
1337 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages); 1398 SpellCheckHost::GetSpellCheckLanguages(profile_, &languages);
1338 if (language_number < languages.size()) { 1399 if (language_number < languages.size()) {
1339 StringPrefMember dictionary_language; 1400 StringPrefMember dictionary_language;
1340 dictionary_language.Init(prefs::kSpellCheckDictionary, 1401 dictionary_language.Init(prefs::kSpellCheckDictionary,
1341 profile_->GetPrefs(), NULL); 1402 profile_->GetPrefs(), NULL);
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1855 g_browser_process->clipboard()); 1916 g_browser_process->clipboard());
1856 } 1917 }
1857 1918
1858 void RenderViewContextMenu::MediaPlayerActionAt( 1919 void RenderViewContextMenu::MediaPlayerActionAt(
1859 const gfx::Point& location, 1920 const gfx::Point& location,
1860 const WebMediaPlayerAction& action) { 1921 const WebMediaPlayerAction& action) {
1861 RenderViewHost* rvh = source_tab_contents_->render_view_host(); 1922 RenderViewHost* rvh = source_tab_contents_->render_view_host();
1862 rvh->Send(new ViewMsg_MediaPlayerActionAt( 1923 rvh->Send(new ViewMsg_MediaPlayerActionAt(
1863 rvh->routing_id(), location, action)); 1924 rvh->routing_id(), location, action));
1864 } 1925 }
OLDNEW
« no previous file with comments | « chrome/browser/tab_contents/render_view_context_menu.h ('k') | chrome/browser/tab_contents/render_view_context_menu_gtk.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698