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

Unified Diff: chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc

Issue 1528483002: Add chrome.input.ime.activate and chrome.input.ime.deactivate API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc
diff --git a/chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc b/chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc
index 9511f015971cd427dfc4a5f808727f35043a0f25..85e43fa11b6d52e91363d48836e99847a98bf8a3 100644
--- a/chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc
+++ b/chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.cc
@@ -12,9 +12,16 @@
#include "base/command_line.h"
#include "base/macros.h"
+#include "chrome/browser/input_method/input_method_engine.h"
#include "chrome/common/chrome_switches.h"
+#include "ui/base/ime/ime_bridge.h"
+
+using ui::IMEEngineHandlerInterface;
+using input_method::InputMethodEngine;
namespace {
Devlin 2016/01/11 21:48:33 \n
Azure Wei 2016/01/13 02:28:10 Done.
+const char kErrorAPIDisabled[] =
+ "The chrome.input.ime API is not supported on the current platform";
bool IsInputImeEnabled() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
@@ -45,9 +52,7 @@ class ImeObserverNonChromeOS : public ui::ImeObserver {
->DispatchEventToExtension(extension_id_, std::move(event));
}
- std::string GetCurrentScreenType() override {
- return "normal";
- }
+ std::string GetCurrentScreenType() override { return "normal"; }
DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS);
};
@@ -61,12 +66,94 @@ InputImeEventRouter::InputImeEventRouter(Profile* profile)
InputImeEventRouter::~InputImeEventRouter() {}
+bool InputImeEventRouter::RegisterImeExtension(
+ const std::string& extension_id) {
+ VLOG(1) << "RegisterImeExtension: " << extension_id;
Devlin 2016/01/11 21:48:33 Remove debugging logs.
Azure Wei 2016/01/13 02:28:10 Done.
+
+ // Already regsitered
Devlin 2016/01/11 21:48:33 typo + add period.
Azure Wei 2016/01/13 02:28:10 Done.
+ if (std::find(extension_ids_.begin(), extension_ids_.end(), extension_id) !=
+ extension_ids_.end())
+ return false;
+
+ extension_ids_.push_back(extension_id);
+ return true;
+}
+
+void InputImeEventRouter::UnregisterImeExtension(
+ const std::string& extension_id) {
+ std::vector<std::string>::iterator it =
+ std::find(extension_ids_.begin(), extension_ids_.end(), extension_id);
+ if (it != extension_ids_.end()) {
+ IMEEngineHandlerInterface* engine =
+ ui::IMEBridge::Get()->GetCurrentEngineHandler();
+ if (engine->GetExtensionId() == extension_id) {
+ engine = nullptr;
Devlin 2016/01/11 21:48:33 What is this supposed to do? It's just a local ra
Azure Wei 2016/01/13 02:28:10 Changed it with ui::IMEBridge()::Get()->SetCurrent
Devlin 2016/01/13 18:22:59 Heh that's quite a difference...
+ }
+ extension_ids_.erase(it);
+ }
+}
+
+IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine(
+ const std::string& extension_id) {
+ IMEEngineHandlerInterface* engine =
+ ui::IMEBridge::Get()->GetCurrentEngineHandler();
+ if (engine && engine->GetExtensionId() == extension_id)
+ return engine;
Devlin 2016/01/11 21:48:33 ternary.
Azure Wei 2016/01/13 02:28:10 Done.
+ return nullptr;
+}
+
+void InputImeEventRouter::SetActiveEngine(const std::string& extension_id) {
+ IMEEngineHandlerInterface* engine =
+ ui::IMEBridge::Get()->GetCurrentEngineHandler();
+ if (engine) {
+ engine->Disable();
+ }
+ input_method::InputMethodEngine* input_engine =
Devlin 2016/01/11 21:48:33 nit: Wrap in a scoped_ptr until ownership is passe
Azure Wei 2016/01/13 02:28:10 Done.
+ new input_method::InputMethodEngine();
+ scoped_ptr<ui::IMEEngineObserver> observer(
+ new ImeObserverNonChromeOS(extension_id, profile()));
+ input_engine->Initialize(std::move(observer), extension_id.c_str(),
+ profile());
+ ui::IMEBridge::Get()->SetCurrentEngineHandler(input_engine);
+ engine = ui::IMEBridge::Get()->GetCurrentEngineHandler();
Devlin 2016/01/11 21:48:33 Is there any way this is different from |input_eng
Azure Wei 2016/01/13 02:28:10 The only difference is that |input_engine| has Ini
+ engine->Enable("");
+ return;
+}
+
+bool InputImeActivateFunction::RunSync() {
+ if (!IsInputImeEnabled()) {
+ error_ = kErrorAPIDisabled;
+ return false;
+ }
+ InputImeEventRouter* event_router =
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
+ event_router->RegisterImeExtension(extension_id());
+ event_router->SetActiveEngine(extension_id());
+ return true;
+}
+
+bool InputImeDeactivateFunction::RunSync() {
+ if (!IsInputImeEnabled()) {
+ error_ = kErrorAPIDisabled;
+ return false;
+ }
+ InputImeEventRouter* event_router =
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
+ event_router->UnregisterImeExtension(extension_id());
+ return true;
+}
+
void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
- const Extension* extension) {}
+ const Extension* extension) {
+ ui::IMEBridge::Initialize();
Devlin 2016/01/11 21:48:33 If this is guaranteed to be a no-op, put a comment
Azure Wei 2016/01/13 02:28:10 Done.
Devlin 2016/01/13 18:22:59 I'd prefer just a "// No-op if called multiple tim
Azure Wei 2016/01/14 14:03:48 Done.
+}
void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
- UnloadedExtensionInfo::Reason reason) {}
+ UnloadedExtensionInfo::Reason reason) {
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context))
+ ->UnregisterImeExtension(extension->id());
+}
void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {}

Powered by Google App Engine
This is Rietveld 408576698