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

Side by Side 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: Addressed Devlin's comments. 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 unified diff | Download patch
OLDNEW
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/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;
16 21
17 namespace { 22 namespace {
18 23
24 const char kErrorAPIDisabled[] =
25 "The chrome.input.ime API is not supported on the current platform";
26
19 bool IsInputImeEnabled() { 27 bool IsInputImeEnabled() {
20 return base::CommandLine::ForCurrentProcess()->HasSwitch( 28 return base::CommandLine::ForCurrentProcess()->HasSwitch(
21 switches::kEnableInputImeAPI); 29 switches::kEnableInputImeAPI);
22 } 30
31 } // namespace
23 32
24 class ImeObserverNonChromeOS : public ui::ImeObserver { 33 class ImeObserverNonChromeOS : public ui::ImeObserver {
25 public: 34 public:
26 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) 35 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile)
27 : ImeObserver(extension_id, profile) {} 36 : ImeObserver(extension_id, profile) {}
28 37
29 ~ImeObserverNonChromeOS() override {} 38 ~ImeObserverNonChromeOS() override {}
30 39
31 private: 40 private:
32 // ImeObserver overrides. 41 // ImeObserver overrides.
33 void DispatchEventToExtension( 42 void DispatchEventToExtension(
34 extensions::events::HistogramValue histogram_value, 43 extensions::events::HistogramValue histogram_value,
35 const std::string& event_name, 44 const std::string& event_name,
36 scoped_ptr<base::ListValue> args) override { 45 scoped_ptr<base::ListValue> args) override {
37 if (!IsInputImeEnabled()) { 46 if (!IsInputImeEnabled()) {
38 return; 47 return;
39 } 48 }
40 49
41 scoped_ptr<extensions::Event> event( 50 scoped_ptr<extensions::Event> event(
42 new extensions::Event(histogram_value, event_name, std::move(args))); 51 new extensions::Event(histogram_value, event_name, std::move(args)));
43 event->restrict_to_browser_context = profile_; 52 event->restrict_to_browser_context = profile_;
44 extensions::EventRouter::Get(profile_) 53 extensions::EventRouter::Get(profile_)
45 ->DispatchEventToExtension(extension_id_, std::move(event)); 54 ->DispatchEventToExtension(extension_id_, std::move(event));
46 } 55 }
47 56
48 std::string GetCurrentScreenType() override { 57 std::string GetCurrentScreenType() override { return "normal"; }
49 return "normal";
50 }
51 58
52 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS); 59 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS);
53 }; 60 };
54 61
55 } // namespace 62 } // namespace
56 63
57 namespace extensions { 64 namespace extensions {
58 65
59 InputImeEventRouter::InputImeEventRouter(Profile* profile) 66 InputImeEventRouter::InputImeEventRouter(Profile* profile)
60 : InputImeEventRouterBase(profile) {} 67 : InputImeEventRouterBase(profile) {}
61 68
62 InputImeEventRouter::~InputImeEventRouter() {} 69 InputImeEventRouter::~InputImeEventRouter() {}
63 70
71 bool InputImeEventRouter::RegisterImeExtension(
72 const std::string& extension_id) {
73 // Already regsitered.
Devlin 2016/01/13 18:22:59 Typo: registered Also, let's make this a bit more
Azure Wei 2016/01/14 14:03:48 Done.
74 if (std::find(extension_ids_.begin(), extension_ids_.end(), extension_id) !=
75 extension_ids_.end())
76 return false;
77
78 extension_ids_.push_back(extension_id);
79 return true;
80 }
81
82 void InputImeEventRouter::UnregisterImeExtension(
83 const std::string& extension_id) {
84 std::vector<std::string>::iterator it =
85 std::find(extension_ids_.begin(), extension_ids_.end(), extension_id);
86 if (it != extension_ids_.end()) {
87 IMEEngineHandlerInterface* engine =
88 ui::IMEBridge::Get()->GetCurrentEngineHandler();
89 if (engine && engine->GetExtensionId() == extension_id) {
90 engine->Disable();
91 delete engine;
Devlin 2016/01/13 18:22:59 This lifetime management is pretty wonky. Do we h
Azure Wei 2016/01/14 14:03:48 A new private function DeleteInputMethodEngine() i
92 ui::IMEBridge::Get()->SetCurrentEngineHandler(nullptr);
93 }
94 extension_ids_.erase(it);
95 }
96 }
97
98 IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine(
99 const std::string& extension_id) {
100 IMEEngineHandlerInterface* engine =
101 ui::IMEBridge::Get()->GetCurrentEngineHandler();
102 return (engine && engine->GetExtensionId() == extension_id) ? engine
103 : nullptr;
104 }
105
106 void InputImeEventRouter::SetActiveEngine(const std::string& extension_id) {
107 IMEEngineHandlerInterface* pre_engine =
108 ui::IMEBridge::Get()->GetCurrentEngineHandler();
109 if (pre_engine) {
110 if (pre_engine->GetExtensionId() == extension_id)
111 return;
112 pre_engine->Disable();
113 delete pre_engine;
114 ui::IMEBridge::Get()->SetCurrentEngineHandler(nullptr);
115 }
116 scoped_ptr<input_method::InputMethodEngine> engine(
117 new input_method::InputMethodEngine());
118 scoped_ptr<ui::IMEEngineObserver> observer(
119 new ImeObserverNonChromeOS(extension_id, profile()));
120 engine->Initialize(std::move(observer), extension_id.c_str(), profile());
121 engine->Enable("");
122 ui::IMEBridge::Get()->SetCurrentEngineHandler(engine.release());
123 return;
Devlin 2016/01/13 18:22:59 ?
Azure Wei 2016/01/14 14:03:48 Deleted.
124 }
125
126 ExtensionFunction::ResponseAction InputImeActivateFunction::Run() {
127 if (!IsInputImeEnabled())
128 return RespondNow(Error(kErrorAPIDisabled));
129
130 InputImeEventRouter* event_router =
131 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
132 event_router->RegisterImeExtension(extension_id());
133 event_router->SetActiveEngine(extension_id());
134 return RespondNow(NoArguments());
135 }
136
137 ExtensionFunction::ResponseAction InputImeDeactivateFunction::Run() {
138 if (!IsInputImeEnabled())
139 return RespondNow(Error(kErrorAPIDisabled));
140
141 InputImeEventRouter* event_router =
142 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
143 event_router->UnregisterImeExtension(extension_id());
144 return RespondNow(NoArguments());
145 }
146
64 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, 147 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
65 const Extension* extension) {} 148 const Extension* extension) {
149 // Initialize the static |g_ime_bridge | if it is NULL.
150 ui::IMEBridge::Initialize();
151 }
66 152
67 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, 153 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
68 const Extension* extension, 154 const Extension* extension,
69 UnloadedExtensionInfo::Reason reason) {} 155 UnloadedExtensionInfo::Reason reason) {
156 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context))
157 ->UnregisterImeExtension(extension->id());
158 }
70 159
71 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {} 160 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {}
72 161
73 } // namespace extensions 162 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698