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

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: 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 {
Devlin 2016/01/11 21:48:33 \n
Azure Wei 2016/01/13 02:28:10 Done.
23 const char kErrorAPIDisabled[] =
24 "The chrome.input.ime API is not supported on the current platform";
18 25
19 bool IsInputImeEnabled() { 26 bool IsInputImeEnabled() {
20 return base::CommandLine::ForCurrentProcess()->HasSwitch( 27 return base::CommandLine::ForCurrentProcess()->HasSwitch(
21 switches::kEnableInputImeAPI); 28 switches::kEnableInputImeAPI);
22 } 29 }
23 30
24 class ImeObserverNonChromeOS : public ui::ImeObserver { 31 class ImeObserverNonChromeOS : public ui::ImeObserver {
25 public: 32 public:
26 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile) 33 ImeObserverNonChromeOS(const std::string& extension_id, Profile* profile)
27 : ImeObserver(extension_id, profile) {} 34 : ImeObserver(extension_id, profile) {}
(...skipping 10 matching lines...) Expand all
38 return; 45 return;
39 } 46 }
40 47
41 scoped_ptr<extensions::Event> event( 48 scoped_ptr<extensions::Event> event(
42 new extensions::Event(histogram_value, event_name, std::move(args))); 49 new extensions::Event(histogram_value, event_name, std::move(args)));
43 event->restrict_to_browser_context = profile_; 50 event->restrict_to_browser_context = profile_;
44 extensions::EventRouter::Get(profile_) 51 extensions::EventRouter::Get(profile_)
45 ->DispatchEventToExtension(extension_id_, std::move(event)); 52 ->DispatchEventToExtension(extension_id_, std::move(event));
46 } 53 }
47 54
48 std::string GetCurrentScreenType() override { 55 std::string GetCurrentScreenType() override { return "normal"; }
49 return "normal";
50 }
51 56
52 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS); 57 DISALLOW_COPY_AND_ASSIGN(ImeObserverNonChromeOS);
53 }; 58 };
54 59
55 } // namespace 60 } // namespace
56 61
57 namespace extensions { 62 namespace extensions {
58 63
59 InputImeEventRouter::InputImeEventRouter(Profile* profile) 64 InputImeEventRouter::InputImeEventRouter(Profile* profile)
60 : InputImeEventRouterBase(profile) {} 65 : InputImeEventRouterBase(profile) {}
61 66
62 InputImeEventRouter::~InputImeEventRouter() {} 67 InputImeEventRouter::~InputImeEventRouter() {}
63 68
69 bool InputImeEventRouter::RegisterImeExtension(
70 const std::string& extension_id) {
71 VLOG(1) << "RegisterImeExtension: " << extension_id;
Devlin 2016/01/11 21:48:33 Remove debugging logs.
Azure Wei 2016/01/13 02:28:10 Done.
72
73 // Already regsitered
Devlin 2016/01/11 21:48:33 typo + add period.
Azure Wei 2016/01/13 02:28:10 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->GetExtensionId() == extension_id) {
90 engine = nullptr;
Devlin 2016/01/11 21:48:33 What is this supposed to do? It's just a local ra
Azure Wei 2016/01/13 02:28:10 Changed it with ui::IMEBridge()::Get()->SetCurrent
Devlin 2016/01/13 18:22:59 Heh that's quite a difference...
91 }
92 extension_ids_.erase(it);
93 }
94 }
95
96 IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine(
97 const std::string& extension_id) {
98 IMEEngineHandlerInterface* engine =
99 ui::IMEBridge::Get()->GetCurrentEngineHandler();
100 if (engine && engine->GetExtensionId() == extension_id)
101 return engine;
Devlin 2016/01/11 21:48:33 ternary.
Azure Wei 2016/01/13 02:28:10 Done.
102 return nullptr;
103 }
104
105 void InputImeEventRouter::SetActiveEngine(const std::string& extension_id) {
106 IMEEngineHandlerInterface* engine =
107 ui::IMEBridge::Get()->GetCurrentEngineHandler();
108 if (engine) {
109 engine->Disable();
110 }
111 input_method::InputMethodEngine* input_engine =
Devlin 2016/01/11 21:48:33 nit: Wrap in a scoped_ptr until ownership is passe
Azure Wei 2016/01/13 02:28:10 Done.
112 new input_method::InputMethodEngine();
113 scoped_ptr<ui::IMEEngineObserver> observer(
114 new ImeObserverNonChromeOS(extension_id, profile()));
115 input_engine->Initialize(std::move(observer), extension_id.c_str(),
116 profile());
117 ui::IMEBridge::Get()->SetCurrentEngineHandler(input_engine);
118 engine = ui::IMEBridge::Get()->GetCurrentEngineHandler();
Devlin 2016/01/11 21:48:33 Is there any way this is different from |input_eng
Azure Wei 2016/01/13 02:28:10 The only difference is that |input_engine| has Ini
119 engine->Enable("");
120 return;
121 }
122
123 bool InputImeActivateFunction::RunSync() {
124 if (!IsInputImeEnabled()) {
125 error_ = kErrorAPIDisabled;
126 return false;
127 }
128 InputImeEventRouter* event_router =
129 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
130 event_router->RegisterImeExtension(extension_id());
131 event_router->SetActiveEngine(extension_id());
132 return true;
133 }
134
135 bool InputImeDeactivateFunction::RunSync() {
136 if (!IsInputImeEnabled()) {
137 error_ = kErrorAPIDisabled;
138 return false;
139 }
140 InputImeEventRouter* event_router =
141 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
142 event_router->UnregisterImeExtension(extension_id());
143 return true;
144 }
145
64 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, 146 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
65 const Extension* extension) {} 147 const Extension* extension) {
148 ui::IMEBridge::Initialize();
Devlin 2016/01/11 21:48:33 If this is guaranteed to be a no-op, put a comment
Azure Wei 2016/01/13 02:28:10 Done.
Devlin 2016/01/13 18:22:59 I'd prefer just a "// No-op if called multiple tim
Azure Wei 2016/01/14 14:03:48 Done.
149 }
66 150
67 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, 151 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
68 const Extension* extension, 152 const Extension* extension,
69 UnloadedExtensionInfo::Reason reason) {} 153 UnloadedExtensionInfo::Reason reason) {
154 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context))
155 ->UnregisterImeExtension(extension->id());
156 }
70 157
71 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {} 158 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {}
72 159
73 } // namespace extensions 160 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698