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

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..1c8ba4fd1ae3968067eede74e42893ea57c6a1e3 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/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);
};
@@ -57,16 +65,99 @@ class ImeObserverNonChromeOS : public ui::ImeObserver {
namespace extensions {
InputImeEventRouter::InputImeEventRouter(Profile* profile)
- : InputImeEventRouterBase(profile) {}
+ : InputImeEventRouterBase(profile), active_engine_(nullptr) {}
+
+InputImeEventRouter::~InputImeEventRouter() {
+ DeleteInputMethodEngine();
+}
-InputImeEventRouter::~InputImeEventRouter() {}
+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();
+ }
+
+ 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_) {
+ delete active_engine_;
+ active_engine_ = nullptr;
+ ui::IMEBridge::Get()->SetCurrentEngineHandler(nullptr);
Devlin 2016/01/15 22:06:02 I'd prefer this to be the first call so that if Se
Azure Wei 2016/01/19 03:46:54 Done.
+ }
+}
+
+ExtensionFunction::ResponseAction InputImeActivateFunction::Run() {
+ if (!IsInputImeEnabled())
+ return RespondNow(Error(kErrorAPIDisabled));
+
+ InputImeEventRouter* event_router =
+ GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
+ event_router->RegisterImeExtension(extension_id());
+ event_router->SetActiveEngine(extension_id());
Shu Chen 2016/01/18 09:21:59 RegisterImeExtension & SetActiveEngine are always
Azure Wei 2016/01/19 03:46:54 Done.
+ 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());
+}
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) {}

Powered by Google App Engine
This is Rietveld 408576698