Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/tab_contents/spellchecker_submenu_observer.h" | 5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 10 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
| 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 12 #include "chrome/browser/tab_contents/spelling_bubble_model.h" | 12 #include "chrome/browser/tab_contents/spelling_bubble_model.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/spellcheck_messages.h" | 15 #include "chrome/common/spellcheck_messages.h" |
| 16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
| 18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/models/simple_menu_model.h" | 20 #include "ui/base/models/simple_menu_model.h" |
| 21 | 21 |
| 22 using content::BrowserThread; | 22 using content::BrowserThread; |
| 23 | 23 |
| 24 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 24 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
| 25 RenderViewContextMenuProxy* proxy, | 25 RenderViewContextMenuProxy* proxy, |
| 26 ui::SimpleMenuModel::Delegate* delegate, | 26 ui::SimpleMenuModel::Delegate* delegate, |
| 27 int group) | 27 int group) |
| 28 : proxy_(proxy), | 28 : proxy_(proxy), |
| 29 submenu_model_(delegate), | 29 submenu_model_(delegate) { |
| 30 check_spelling_while_typing_(false) { | |
| 31 DCHECK(proxy_); | 30 DCHECK(proxy_); |
| 32 } | 31 } |
| 33 | 32 |
| 34 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 33 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 35 } | 34 } |
| 36 | 35 |
| 37 void SpellCheckerSubMenuObserver::InitMenu( | 36 void SpellCheckerSubMenuObserver::InitMenu( |
| 38 const content::ContextMenuParams& params) { | 37 const content::ContextMenuParams& params) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 | 39 |
| 41 check_spelling_while_typing_ = params.spellcheck_enabled; | |
| 42 | |
| 43 // Add an item that toggles the spelling panel. | 40 // Add an item that toggles the spelling panel. |
| 44 submenu_model_.AddCheckItem( | 41 submenu_model_.AddCheckItem( |
| 45 IDC_SPELLPANEL_TOGGLE, | 42 IDC_SPELLPANEL_TOGGLE, |
| 46 l10n_util::GetStringUTF16( | 43 l10n_util::GetStringUTF16( |
| 47 spellcheck_mac::SpellingPanelVisible() ? | 44 spellcheck_mac::SpellingPanelVisible() ? |
| 48 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL : | 45 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL : |
| 49 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL)); | 46 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL)); |
| 50 submenu_model_.AddSeparator(); | 47 submenu_model_.AddSeparator(); |
| 51 | 48 |
| 52 // Add a 'Check Spelling While Typing' item in the sub menu. | 49 // Add a 'Check Spelling While Typing' item in the sub menu. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 77 | 74 |
| 78 return false; | 75 return false; |
| 79 } | 76 } |
| 80 | 77 |
| 81 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 78 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 82 DCHECK(IsCommandIdSupported(command_id)); | 79 DCHECK(IsCommandIdSupported(command_id)); |
| 83 | 80 |
| 84 // Check box for 'Check Spelling while typing'. | 81 // Check box for 'Check Spelling while typing'. |
| 85 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 82 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 86 Profile* profile = proxy_->GetProfile(); | 83 Profile* profile = proxy_->GetProfile(); |
| 87 if (!profile || !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)) | 84 return profile && profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck); |
|
jeremy
2012/06/14 14:49:50
same comment about splitting this into an if and e
Hironori Bono
2012/06/18 07:23:55
Thanks for your comment. I have removed this profi
| |
| 88 return false; | |
| 89 return check_spelling_while_typing_; | |
| 90 } | 85 } |
| 91 | 86 |
| 92 return false; | 87 return false; |
| 93 } | 88 } |
| 94 | 89 |
| 95 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { | 90 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { |
| 96 DCHECK(IsCommandIdSupported(command_id)); | 91 DCHECK(IsCommandIdSupported(command_id)); |
| 97 | 92 |
| 98 Profile* profile = proxy_->GetProfile(); | 93 Profile* profile = proxy_->GetProfile(); |
| 99 if (!profile) | 94 if (!profile) |
| 100 return false; | 95 return false; |
| 101 | 96 |
| 102 const PrefService* pref = profile->GetPrefs(); | |
| 103 | |
| 104 switch (command_id) { | 97 switch (command_id) { |
| 105 case IDC_CHECK_SPELLING_WHILE_TYPING: | 98 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 106 return pref->GetBoolean(prefs::kEnableSpellCheck); | |
| 107 | |
| 108 case IDC_SPELLPANEL_TOGGLE: | 99 case IDC_SPELLPANEL_TOGGLE: |
| 109 case IDC_SPELLCHECK_MENU: | 100 case IDC_SPELLCHECK_MENU: |
| 110 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE: | 101 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE: |
| 111 return true; | 102 return true; |
| 112 } | 103 } |
| 113 | 104 |
| 114 return false; | 105 return false; |
| 115 } | 106 } |
| 116 | 107 |
| 117 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 108 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 118 DCHECK(IsCommandIdSupported(command_id)); | 109 DCHECK(IsCommandIdSupported(command_id)); |
| 119 | 110 |
| 120 content::RenderViewHost* rvh = proxy_->GetRenderViewHost(); | 111 content::RenderViewHost* rvh = proxy_->GetRenderViewHost(); |
| 112 Profile* profile = proxy_->GetProfile(); | |
| 121 switch (command_id) { | 113 switch (command_id) { |
| 122 case IDC_CHECK_SPELLING_WHILE_TYPING: | 114 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 115 profile->GetPrefs()->SetBoolean( | |
|
jeremy
2012/06/14 14:49:50
null-check profile?
Hironori Bono
2012/06/18 07:23:55
Thanks for your comment. I was too focused on sync
| |
| 116 prefs::kEnableSpellCheck, | |
| 117 !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); | |
| 123 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->GetRoutingID())); | 118 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->GetRoutingID())); |
| 124 break; | 119 break; |
| 125 | 120 |
| 126 case IDC_SPELLPANEL_TOGGLE: | 121 case IDC_SPELLPANEL_TOGGLE: |
| 127 rvh->Send(new SpellCheckMsg_ToggleSpellPanel( | 122 rvh->Send(new SpellCheckMsg_ToggleSpellPanel( |
| 128 rvh->GetRoutingID(), spellcheck_mac::SpellingPanelVisible())); | 123 rvh->GetRoutingID(), spellcheck_mac::SpellingPanelVisible())); |
| 129 break; | 124 break; |
| 130 } | 125 } |
| 131 } | 126 } |
| OLD | NEW |