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

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 98be0cc31a0a55defb3c2018a4014840686811f5..f981632611f4ccf1a46555cfa270c94c6fada149 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,14 +12,24 @@
#include "base/command_line.h"
#include "base/macros.h"
+#include "chrome/browser/ui/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;
+using input_method::InputMethodEngineBase;
namespace {
+const char kErrorAPIDisabled[] =
+ "The chrome.input.ime API is not supported on the current platform";
+
bool IsInputImeEnabled() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableInputImeAPI);
-}
+
+} // namespace
class ImeObserverNonChromeOS : public ui::ImeObserver {
public:
@@ -45,9 +55,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);
};
@@ -56,20 +64,103 @@ class ImeObserverNonChromeOS : public ui::ImeObserver {
namespace extensions {
-InputImeEventRouter::InputImeEventRouter(Profile* profile)
- : InputImeEventRouterBase(profile) {}
-
-InputImeEventRouter::~InputImeEventRouter() {}
-
void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
- const Extension* extension) {}
+ const Extension* extension) {
+ // No-op if called multiple times.
+ ui::IMEBridge::Initialize();
+}
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) {}
+InputImeEventRouter::InputImeEventRouter(Profile* profile)
+ : InputImeEventRouterBase(profile), active_engine_(nullptr) {}
+
+InputImeEventRouter::~InputImeEventRouter() {
+ DeleteInputMethodEngine();
+}
+
+bool InputImeEventRouter::RegisterImeExtension(
+ const std::string& extension_id) {
+ // Check if the IME extension is already registered.
+ 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()) {
+ if (GetActiveEngine(extension_id))
+ DeleteInputMethodEngine();
+ extension_ids_.erase(it);
+ }
+}
+
+InputMethodEngine* InputImeEventRouter::GetActiveEngine(
+ const std::string& extension_id) {
+ return (active_engine_ && active_engine_->GetExtensionId() == extension_id)
+ ? active_engine_
+ : nullptr;
+}
+
+void InputImeEventRouter::SetActiveEngine(const std::string& extension_id) {
+ if (active_engine_) {
+ if (active_engine_->GetExtensionId() == extension_id)
+ return;
+ DeleteInputMethodEngine();
+ }
+
+ RegisterImeExtension(extension_id);
+ scoped_ptr<input_method::InputMethodEngine> engine(
+ new input_method::InputMethodEngine());
+ scoped_ptr<InputMethodEngineBase::Observer> observer(
+ new ImeObserverNonChromeOS(extension_id, profile()));
+ engine->Initialize(std::move(observer), extension_id.c_str(), profile());
+ engine->Enable("");
+ active_engine_ = engine.release();
+ ui::IMEBridge::Get()->SetCurrentEngineHandler(active_engine_);
+}
+
+void InputImeEventRouter::DeleteInputMethodEngine() {
+ if (active_engine_) {
+ ui::IMEBridge::Get()->SetCurrentEngineHandler(nullptr);
+ delete active_engine_;
+ active_engine_ = nullptr;
+ }
+}
+
+ExtensionFunction::ResponseAction InputImeActivateFunction::Run() {
+ if (!IsInputImeEnabled())
+ return RespondNow(Error(kErrorAPIDisabled));
+
+ InputImeEventRouter* event_router =
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
+ event_router->SetActiveEngine(extension_id());
+ return RespondNow(NoArguments());
+}
+
+ExtensionFunction::ResponseAction InputImeDeactivateFunction::Run() {
+ if (!IsInputImeEnabled())
+ return RespondNow(Error(kErrorAPIDisabled));
+
+ InputImeEventRouter* event_router =
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
+ event_router->UnregisterImeExtension(extension_id());
+ return RespondNow(NoArguments());
+}
+
ExtensionFunction::ResponseAction InputImeCreateWindowFunction::Run() {
// TODO(shuchen): Implement this API.
return RespondNow(NoArguments());

Powered by Google App Engine
This is Rietveld 408576698