Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // This file is for non-chromeos (win & linux) functions, such as | 5 // This file is for non-chromeos (win & linux) functions, such as |
| 6 // chrome.input.ime.activate, chrome.input.ime.createWindow and | 6 // chrome.input.ime.activate, chrome.input.ime.createWindow and |
| 7 // chrome.input.ime.onSelectionChanged. | 7 // chrome.input.ime.onSelectionChanged. |
| 8 // TODO(azurewei): May refactor the code structure by using delegate or | 8 // TODO(azurewei): May refactor the code structure by using delegate or |
| 9 // redesign the API to remove this platform-specific file in the future. | 9 // redesign the API to remove this platform-specific file in the future. |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 const char kErrorNoActiveEngine[] = "The extension has not been activated."; | 39 const char kErrorNoActiveEngine[] = "The extension has not been activated."; |
| 40 const char kErrorPermissionDenied[] = "User denied permission."; | 40 const char kErrorPermissionDenied[] = "User denied permission."; |
| 41 const char kErrorCouldNotFindActiveBrowser[] = | 41 const char kErrorCouldNotFindActiveBrowser[] = |
| 42 "Cannot find the active browser."; | 42 "Cannot find the active browser."; |
| 43 const char kErrorNotCalledFromUserAction[] = | 43 const char kErrorNotCalledFromUserAction[] = |
| 44 "This API is only allowed to be called from a user action."; | 44 "This API is only allowed to be called from a user action."; |
| 45 | 45 |
| 46 // A preference determining whether to hide the warning bubble next time. | 46 // A preference determining whether to hide the warning bubble next time. |
| 47 const char kPrefWarningBubbleNeverShow[] = "skip_ime_warning_bubble"; | 47 const char kPrefWarningBubbleNeverShow[] = "skip_ime_warning_bubble"; |
| 48 | 48 |
| 49 // A preference to see whether it is the first time to call input.ime.activate | |
| 50 // since the extension is loaded. | |
| 51 const char kPrefImeActivatedCalledBefore[] = "ime_activate_called_before"; | |
| 52 | |
| 49 bool IsInputImeEnabled() { | 53 bool IsInputImeEnabled() { |
| 50 return !base::CommandLine::ForCurrentProcess()->HasSwitch( | 54 return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 51 switches::kDisableInputImeAPI); | 55 switches::kDisableInputImeAPI); |
| 52 } | 56 } |
| 53 | 57 |
| 54 class ImeObserverNonChromeOS : public ui::ImeObserver { | 58 class ImeObserverNonChromeOS : public ui::ImeObserver { |
| 55 public: | 59 public: |
| 56 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) | 60 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) |
| 57 : ImeObserver(extension_id, profile) {} | 61 : ImeObserver(extension_id, profile) {} |
| 58 | 62 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 event_router ? static_cast<InputMethodEngine*>( | 120 event_router ? static_cast<InputMethodEngine*>( |
| 117 event_router->GetActiveEngine(extension_id)) | 121 event_router->GetActiveEngine(extension_id)) |
| 118 : nullptr; | 122 : nullptr; |
| 119 return engine; | 123 return engine; |
| 120 } | 124 } |
| 121 | 125 |
| 122 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, | 126 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 123 const Extension* extension) { | 127 const Extension* extension) { |
| 124 // No-op if called multiple times. | 128 // No-op if called multiple times. |
| 125 ui::IMEBridge::Initialize(); | 129 ui::IMEBridge::Initialize(); |
| 130 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 131 ExtensionPrefs::Get(profile)->UpdateExtensionPref( | |
| 132 extension->id(), kPrefImeActivatedCalledBefore, | |
| 133 new base::FundamentalValue(false)); | |
| 126 } | 134 } |
| 127 | 135 |
| 128 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 136 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 129 const Extension* extension, | 137 const Extension* extension, |
| 130 UnloadedExtensionInfo::Reason reason) { | 138 UnloadedExtensionInfo::Reason reason) { |
| 131 InputImeEventRouter* event_router = | 139 InputImeEventRouter* event_router = |
| 132 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)); | 140 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)); |
| 133 if (event_router) | 141 if (event_router) |
| 134 event_router->DeleteInputMethodEngine(extension->id()); | 142 event_router->DeleteInputMethodEngine(extension->id()); |
| 135 } | 143 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 bool InputImeActivateFunction::disable_bubble_for_testing_ = false; | 195 bool InputImeActivateFunction::disable_bubble_for_testing_ = false; |
| 188 | 196 |
| 189 ExtensionFunction::ResponseAction InputImeActivateFunction::Run() { | 197 ExtensionFunction::ResponseAction InputImeActivateFunction::Run() { |
| 190 if (!IsInputImeEnabled()) | 198 if (!IsInputImeEnabled()) |
| 191 return RespondNow(Error(kErrorAPIDisabled)); | 199 return RespondNow(Error(kErrorAPIDisabled)); |
| 192 Profile* profile = Profile::FromBrowserContext(browser_context()); | 200 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 193 InputImeEventRouter* event_router = GetInputImeEventRouter(profile); | 201 InputImeEventRouter* event_router = GetInputImeEventRouter(profile); |
| 194 if (!event_router) | 202 if (!event_router) |
| 195 return RespondNow(Error(kErrorNoActiveEngine)); | 203 return RespondNow(Error(kErrorNoActiveEngine)); |
| 196 | 204 |
| 197 // This API is only allowed to be called from a user action. | 205 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
| 206 bool allowed_to_activate = false; | |
|
Shu Chen
2016/04/01 08:10:52
s/allowed_to_activate/warning_bubble_never_show/g
Azure Wei
2016/04/01 13:34:04
Done.
| |
| 207 bool ime_activated_called = false; | |
| 208 | |
| 209 if (prefs->ReadPrefAsBoolean(extension_id(), kPrefImeActivatedCalledBefore, | |
|
Shu Chen
2016/04/01 08:10:52
I think this block should also be after the disabl
Azure Wei
2016/04/01 13:34:04
If put this after disable_bubble_for_testing_, it
| |
| 210 &ime_activated_called) && | |
| 211 prefs->ReadPrefAsBoolean(extension_id(), kPrefWarningBubbleNeverShow, | |
| 212 &allowed_to_activate) && | |
| 213 !ime_activated_called && | |
| 214 allowed_to_activate) { | |
| 215 // If it the first time that the extension call input.ime.activate() since | |
| 216 // loaded, we allow it to be automatically activated from a non user action. | |
| 217 event_router->SetActiveEngine(extension_id()); | |
| 218 ExtensionPrefs::Get(profile)->UpdateExtensionPref( | |
| 219 extension_id(), kPrefImeActivatedCalledBefore, | |
| 220 new base::FundamentalValue(true)); | |
| 221 return RespondNow(NoArguments()); | |
| 222 } | |
| 223 | |
| 224 ExtensionPrefs::Get(profile)->UpdateExtensionPref( | |
| 225 extension_id(), kPrefImeActivatedCalledBefore, | |
| 226 new base::FundamentalValue(true)); | |
| 227 | |
| 228 // If not the first time, this API is only allowed to be called from a user | |
| 229 // action. | |
| 198 if (!user_gesture()) | 230 if (!user_gesture()) |
| 199 return RespondNow(Error(kErrorNotCalledFromUserAction)); | 231 return RespondNow(Error(kErrorNotCalledFromUserAction)); |
| 200 | 232 |
| 201 // Disable using the warning bubble for testing. | 233 // Disable using the warning bubble for testing. |
| 202 if (disable_bubble_for_testing_) { | 234 if (disable_bubble_for_testing_) { |
| 203 GetInputImeEventRouter(profile)->SetActiveEngine(extension_id()); | 235 GetInputImeEventRouter(profile)->SetActiveEngine(extension_id()); |
| 204 return RespondNow(NoArguments()); | 236 return RespondNow(NoArguments()); |
| 205 } | 237 } |
| 206 | 238 |
| 207 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | 239 if (allowed_to_activate) { |
| 208 bool allowed_to_activate = false; | |
| 209 if (prefs->ReadPrefAsBoolean(extension_id(), kPrefWarningBubbleNeverShow, | |
| 210 &allowed_to_activate) && | |
| 211 allowed_to_activate) { | |
| 212 // If user allows to activate the extension without showing the warning | 240 // If user allows to activate the extension without showing the warning |
| 213 // bubble, sets the active engine directly. | 241 // bubble, sets the active engine directly. |
| 214 // Otherwise, the extension will be activated when the user presses the 'OK' | 242 // Otherwise, the extension will be activated when the user presses the 'OK' |
| 215 // button on the warning bubble. | 243 // button on the warning bubble. |
| 216 event_router->SetActiveEngine(extension_id()); | 244 event_router->SetActiveEngine(extension_id()); |
| 217 return RespondNow(NoArguments()); | 245 return RespondNow(NoArguments()); |
| 218 } | 246 } |
| 219 | 247 |
| 220 Browser* browser = chrome::FindLastActiveWithProfile(profile); | 248 Browser* browser = chrome::FindLastActiveWithProfile(profile); |
| 221 if (!browser) | 249 if (!browser) |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 return RespondNow(Error(kErrorNoActiveEngine)); | 366 return RespondNow(Error(kErrorNoActiveEngine)); |
| 339 | 367 |
| 340 scoped_ptr<api::input_ime::HideWindow::Params> params( | 368 scoped_ptr<api::input_ime::HideWindow::Params> params( |
| 341 api::input_ime::HideWindow::Params::Create(*args_)); | 369 api::input_ime::HideWindow::Params::Create(*args_)); |
| 342 EXTENSION_FUNCTION_VALIDATE(params.get()); | 370 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 343 engine->HideImeWindow(params->window_id); | 371 engine->HideImeWindow(params->window_id); |
| 344 return RespondNow(NoArguments()); | 372 return RespondNow(NoArguments()); |
| 345 } | 373 } |
| 346 | 374 |
| 347 } // namespace extensions | 375 } // namespace extensions |
| OLD | NEW |