From 955667fa435938c4df7b2a8e5c40f02bc8b31a2f Mon Sep 17 00:00:00 2001 From: "shuchen@chromium.org" Date: Thu, 17 Jul 2014 11:27:01 +0000 Subject: [PATCH] Checks whether background is defined in IME extension manifest. And also move KeyEventDone logic from InputMethodEngine to input_ime_api.cc. BUG=394682, 394420 TEST=Verified on linux_chromeos and Pixel device. Review URL: https://codereview.chromium.org/400523005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283755 0039d316-1c4b-4281-b951-d872f2087c98 --- .../chromeos/input_method/input_method_engine.cc | 8 ---- .../chromeos/input_method/input_method_engine.h | 2 - .../input_method/input_method_engine_interface.h | 4 -- .../input_method/mock_input_method_engine.cc | 4 -- .../input_method/mock_input_method_engine.h | 2 - .../extensions/api/input_ime/input_ime_api.cc | 49 +++++++++++++++------- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/chrome/browser/chromeos/input_method/input_method_engine.cc b/chrome/browser/chromeos/input_method/input_method_engine.cc index f8e6c87c2a7c..540766af3629 100644 --- a/chrome/browser/chromeos/input_method/input_method_engine.cc +++ b/chrome/browser/chromeos/input_method/input_method_engine.cc @@ -470,14 +470,6 @@ bool InputMethodEngine::IsActive() const { return active_; } -void InputMethodEngine::KeyEventDone(input_method::KeyEventHandle* key_data, - bool handled) { - KeyEventDoneCallback* callback = - reinterpret_cast(key_data); - callback->Run(handled); - delete callback; -} - bool InputMethodEngine::DeleteSurroundingText(int context_id, int offset, size_t number_of_chars, diff --git a/chrome/browser/chromeos/input_method/input_method_engine.h b/chrome/browser/chromeos/input_method/input_method_engine.h index 716d1366ed75..d841a5716465 100644 --- a/chrome/browser/chromeos/input_method/input_method_engine.h +++ b/chrome/browser/chromeos/input_method/input_method_engine.h @@ -77,8 +77,6 @@ class InputMethodEngine : public InputMethodEngineInterface { virtual bool SetMenuItems(const std::vector& items) OVERRIDE; virtual bool UpdateMenuItems(const std::vector& items) OVERRIDE; virtual bool IsActive() const OVERRIDE; - virtual void KeyEventDone(input_method::KeyEventHandle* key_data, - bool handled) OVERRIDE; virtual bool DeleteSurroundingText(int context_id, int offset, size_t number_of_chars, diff --git a/chrome/browser/chromeos/input_method/input_method_engine_interface.h b/chrome/browser/chromeos/input_method/input_method_engine_interface.h index f60ed453afb4..71d4298ee97d 100644 --- a/chrome/browser/chromeos/input_method/input_method_engine_interface.h +++ b/chrome/browser/chromeos/input_method/input_method_engine_interface.h @@ -231,10 +231,6 @@ class InputMethodEngineInterface : public IMEEngineHandlerInterface { // Returns true if this IME is active, false if not. virtual bool IsActive() const = 0; - // Inform the engine that a key event has been processed. - virtual void KeyEventDone(input_method::KeyEventHandle* key_data, - bool handled) = 0; - // Deletes |number_of_chars| unicode characters as the basis of |offset| from // the surrounding text. The |offset| is relative position based on current // caret. diff --git a/chrome/browser/chromeos/input_method/mock_input_method_engine.cc b/chrome/browser/chromeos/input_method/mock_input_method_engine.cc index fe104e388df5..2c3228a9faea 100644 --- a/chrome/browser/chromeos/input_method/mock_input_method_engine.cc +++ b/chrome/browser/chromeos/input_method/mock_input_method_engine.cc @@ -90,10 +90,6 @@ bool MockInputMethodEngine::IsActive() const { return true; } -void MockInputMethodEngine::KeyEventDone(input_method::KeyEventHandle* key_data, - bool handled) { -} - bool MockInputMethodEngine::DeleteSurroundingText(int context_id, int offset, size_t number_of_chars, diff --git a/chrome/browser/chromeos/input_method/mock_input_method_engine.h b/chrome/browser/chromeos/input_method/mock_input_method_engine.h index 3dc61732e033..c22ac5d56d20 100644 --- a/chrome/browser/chromeos/input_method/mock_input_method_engine.h +++ b/chrome/browser/chromeos/input_method/mock_input_method_engine.h @@ -66,8 +66,6 @@ class MockInputMethodEngine : public InputMethodEngineInterface { virtual bool SetMenuItems(const std::vector& items) OVERRIDE; virtual bool UpdateMenuItems(const std::vector& items) OVERRIDE; virtual bool IsActive() const OVERRIDE; - virtual void KeyEventDone(input_method::KeyEventHandle* key_data, - bool handled) OVERRIDE; virtual bool DeleteSurroundingText(int context_id, int offset, size_t number_of_chars, diff --git a/chrome/browser/extensions/api/input_ime/input_ime_api.cc b/chrome/browser/extensions/api/input_ime/input_ime_api.cc index 6d848aea22aa..b35997b4cc6d 100644 --- a/chrome/browser/extensions/api/input_ime/input_ime_api.cc +++ b/chrome/browser/extensions/api/input_ime/input_ime_api.cc @@ -7,12 +7,15 @@ #include "base/strings/string_number_conversions.h" #include "base/values.h" #include "chrome/browser/chromeos/profiles/profile_helper.h" +#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/common/extensions/api/input_ime.h" #include "chrome/common/extensions/api/input_ime/input_components_handler.h" #include "extensions/browser/event_router.h" #include "extensions/browser/extension_function_registry.h" #include "extensions/browser/extension_registry.h" +#include "extensions/browser/extension_system.h" +#include "extensions/common/manifest_handlers/background_info.h" #if defined(USE_X11) #include "chrome/browser/chromeos/input_method/input_method_engine.h" @@ -80,13 +83,32 @@ static void DispatchEventToExtension(Profile* profile, ->DispatchEventToExtension(extension_id, event.Pass()); } +void CallbackKeyEventHandle(chromeos::input_method::KeyEventHandle* key_data, + bool handled) { + base::Callback* callback = + reinterpret_cast*>(key_data); + callback->Run(handled); + delete callback; +} + } // namespace namespace chromeos { class ImeObserver : public InputMethodEngineInterface::Observer { public: ImeObserver(Profile* profile, const std::string& extension_id) - : profile_(profile), extension_id_(extension_id) {} + : profile_(profile), extension_id_(extension_id), has_background_(false) { + extensions::ExtensionSystem* extension_system = + extensions::ExtensionSystem::Get(profile_); + ExtensionService* extension_service = extension_system->extension_service(); + const extensions::Extension* extension = + extension_service->GetExtensionById(extension_id, false); + DCHECK(extension); + extensions::BackgroundInfo* info = static_cast( + extension->GetManifestData("background")); + if (info) + has_background_ = info->has_background_page(); + } virtual ~ImeObserver() {} @@ -163,13 +185,10 @@ class ImeObserver : public InputMethodEngineInterface::Observer { // If there is no listener for the event, no need to dispatch the event to // extension. Instead, releases the key event for default system behavior. - if (!HasKeyEventListener()) { + if (!ShouldForwardKeyEvent()) { // Continue processing the key event so that the physical keyboard can // still work. - base::Callback* callback = - reinterpret_cast*>(key_data); - callback->Run(false); - delete callback; + CallbackKeyEventHandle(key_data, false); return; } @@ -281,14 +300,20 @@ class ImeObserver : public InputMethodEngineInterface::Observer { } private: - bool HasKeyEventListener() const { - return extensions::EventRouter::Get(profile_) + // Returns true if the extension is ready to accept key event, otherwise + // returns false. + bool ShouldForwardKeyEvent() const { + // Need to check the background page first since the + // ExtensionHasEventListner returns true if the extension does not have a + // background page. See crbug.com/394682. + return has_background_ && extensions::EventRouter::Get(profile_) ->ExtensionHasEventListener(extension_id_, input_ime::OnKeyEvent::kEventName); } Profile* profile_; std::string extension_id_; + bool has_background_; DISALLOW_COPY_AND_ASSIGN(ImeObserver); }; @@ -415,13 +440,7 @@ void InputImeEventRouter::OnKeyEventHandled( chromeos::input_method::KeyEventHandle* key_data = request->second.second; request_map_.erase(request); - InputMethodEngineInterface* engine = GetEngine(extension_id, engine_id); - if (!engine) { - LOG(ERROR) << "Engine does not exist: " << engine_id; - return; - } - - engine->KeyEventDone(key_data, handled); + CallbackKeyEventHandle(key_data, handled); } std::string InputImeEventRouter::AddRequest( -- 2.11.4.GIT