| 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 |
| 11 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 11 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "chrome/browser/ui/input_method/input_method_engine.h" |
| 15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "ui/base/ime/ime_bridge.h" |
| 18 |
| 19 using ui::IMEEngineHandlerInterface; |
| 20 using input_method::InputMethodEngine; |
| 21 using input_method::InputMethodEngineBase; |
| 16 | 22 |
| 17 namespace { | 23 namespace { |
| 18 | 24 |
| 25 const char kErrorAPIDisabled[] = |
| 26 "The chrome.input.ime API is not supported on the current platform"; |
| 27 |
| 19 bool IsInputImeEnabled() { | 28 bool IsInputImeEnabled() { |
| 20 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 29 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 21 switches::kEnableInputImeAPI); | 30 switches::kEnableInputImeAPI); |
| 22 } | 31 |
| 32 } // namespace |
| 23 | 33 |
| 24 class ImeObserverNonChromeOS : public ui::ImeObserver { | 34 class ImeObserverNonChromeOS : public ui::ImeObserver { |
| 25 public: | 35 public: |
| 26 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) | 36 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) |
| 27 : ImeObserver(extension_id, profile) {} | 37 : ImeObserver(extension_id, profile) {} |
| 28 | 38 |
| 29 ~ImeObserverNonChromeOS() override {} | 39 ~ImeObserverNonChromeOS() override {} |
| 30 | 40 |
| 31 private: | 41 private: |
| 32 // ImeObserver overrides. | 42 // ImeObserver overrides. |
| 33 void DispatchEventToExtension( | 43 void DispatchEventToExtension( |
| 34 extensions::events::HistogramValue histogram_value, | 44 extensions::events::HistogramValue histogram_value, |
| 35 const std::string& event_name, | 45 const std::string& event_name, |
| 36 scoped_ptr<base::ListValue> args) override { | 46 scoped_ptr<base::ListValue> args) override { |
| 37 if (!IsInputImeEnabled()) { | 47 if (!IsInputImeEnabled()) { |
| 38 return; | 48 return; |
| 39 } | 49 } |
| 40 | 50 |
| 41 scoped_ptr<extensions::Event> event( | 51 scoped_ptr<extensions::Event> event( |
| 42 new extensions::Event(histogram_value, event_name, std::move(args))); | 52 new extensions::Event(histogram_value, event_name, std::move(args))); |
| 43 event->restrict_to_browser_context = profile_; | 53 event->restrict_to_browser_context = profile_; |
| 44 extensions::EventRouter::Get(profile_) | 54 extensions::EventRouter::Get(profile_) |
| 45 ->DispatchEventToExtension(extension_id_, std::move(event)); | 55 ->DispatchEventToExtension(extension_id_, std::move(event)); |
| 46 } | 56 } |
| 47 | 57 |
| 48 std::string GetCurrentScreenType() override { | 58 std::string GetCurrentScreenType() override { return "normal"; } |
| 49 return "normal"; | |
| 50 } | |
| 51 | 59 |
| 52 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS); | 60 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS); |
| 53 }; | 61 }; |
| 54 | 62 |
| 55 } // namespace | 63 } // namespace |
| 56 | 64 |
| 57 namespace extensions { | 65 namespace extensions { |
| 58 | 66 |
| 59 InputImeEventRouter::InputImeEventRouter(Profile* profile) | |
| 60 : InputImeEventRouterBase(profile) {} | |
| 61 | |
| 62 InputImeEventRouter::~InputImeEventRouter() {} | |
| 63 | |
| 64 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, | 67 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 65 const Extension* extension) {} | 68 const Extension* extension) { |
| 69 // No-op if called multiple times. |
| 70 ui::IMEBridge::Initialize(); |
| 71 } |
| 66 | 72 |
| 67 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 73 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 68 const Extension* extension, | 74 const Extension* extension, |
| 69 UnloadedExtensionInfo::Reason reason) {} | 75 UnloadedExtensionInfo::Reason reason) { |
| 76 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)) |
| 77 ->UnregisterImeExtension(extension->id()); |
| 78 } |
| 70 | 79 |
| 71 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {} | 80 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {} |
| 72 | 81 |
| 82 InputImeEventRouter::InputImeEventRouter(Profile* profile) |
| 83 : InputImeEventRouterBase(profile), active_engine_(nullptr) {} |
| 84 |
| 85 InputImeEventRouter::~InputImeEventRouter() { |
| 86 DeleteInputMethodEngine(); |
| 87 } |
| 88 |
| 89 bool InputImeEventRouter::RegisterImeExtension( |
| 90 const std::string& extension_id) { |
| 91 // Check if the IME extension is already registered. |
| 92 if (std::find(extension_ids_.begin(), extension_ids_.end(), extension_id) != |
| 93 extension_ids_.end()) |
| 94 return false; |
| 95 |
| 96 extension_ids_.push_back(extension_id); |
| 97 return true; |
| 98 } |
| 99 |
| 100 void InputImeEventRouter::UnregisterImeExtension( |
| 101 const std::string& extension_id) { |
| 102 std::vector<std::string>::iterator it = |
| 103 std::find(extension_ids_.begin(), extension_ids_.end(), extension_id); |
| 104 if (it != extension_ids_.end()) { |
| 105 if (GetActiveEngine(extension_id)) |
| 106 DeleteInputMethodEngine(); |
| 107 extension_ids_.erase(it); |
| 108 } |
| 109 } |
| 110 |
| 111 InputMethodEngine* InputImeEventRouter::GetActiveEngine( |
| 112 const std::string& extension_id) { |
| 113 return (active_engine_ && active_engine_->GetExtensionId() == extension_id) |
| 114 ? active_engine_ |
| 115 : nullptr; |
| 116 } |
| 117 |
| 118 void InputImeEventRouter::SetActiveEngine(const std::string& extension_id) { |
| 119 if (active_engine_) { |
| 120 if (active_engine_->GetExtensionId() == extension_id) |
| 121 return; |
| 122 DeleteInputMethodEngine(); |
| 123 } |
| 124 |
| 125 RegisterImeExtension(extension_id); |
| 126 scoped_ptr<input_method::InputMethodEngine> engine( |
| 127 new input_method::InputMethodEngine()); |
| 128 scoped_ptr<InputMethodEngineBase::Observer> observer( |
| 129 new ImeObserverNonChromeOS(extension_id, profile())); |
| 130 engine->Initialize(std::move(observer), extension_id.c_str(), profile()); |
| 131 engine->Enable(""); |
| 132 active_engine_ = engine.release(); |
| 133 ui::IMEBridge::Get()->SetCurrentEngineHandler(active_engine_); |
| 134 } |
| 135 |
| 136 void InputImeEventRouter::DeleteInputMethodEngine() { |
| 137 if (active_engine_) { |
| 138 ui::IMEBridge::Get()->SetCurrentEngineHandler(nullptr); |
| 139 delete active_engine_; |
| 140 active_engine_ = nullptr; |
| 141 } |
| 142 } |
| 143 |
| 144 ExtensionFunction::ResponseAction InputImeActivateFunction::Run() { |
| 145 if (!IsInputImeEnabled()) |
| 146 return RespondNow(Error(kErrorAPIDisabled)); |
| 147 |
| 148 InputImeEventRouter* event_router = |
| 149 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 150 event_router->SetActiveEngine(extension_id()); |
| 151 return RespondNow(NoArguments()); |
| 152 } |
| 153 |
| 154 ExtensionFunction::ResponseAction InputImeDeactivateFunction::Run() { |
| 155 if (!IsInputImeEnabled()) |
| 156 return RespondNow(Error(kErrorAPIDisabled)); |
| 157 |
| 158 InputImeEventRouter* event_router = |
| 159 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 160 event_router->UnregisterImeExtension(extension_id()); |
| 161 return RespondNow(NoArguments()); |
| 162 } |
| 163 |
| 73 ExtensionFunction::ResponseAction InputImeCreateWindowFunction::Run() { | 164 ExtensionFunction::ResponseAction InputImeCreateWindowFunction::Run() { |
| 74 // TODO(shuchen): Implement this API. | 165 // TODO(shuchen): Implement this API. |
| 75 return RespondNow(NoArguments()); | 166 return RespondNow(NoArguments()); |
| 76 } | 167 } |
| 77 | 168 |
| 78 } // namespace extensions | 169 } // namespace extensions |
| OLD | NEW |