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

Side by Side Diff: components/arc/ime/arc_ime_service.cc

Issue 2661863002: ARC IME: Fix Crash on ARC Notification (Closed)
Patch Set: Created 3 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #include "components/arc/ime/arc_ime_service.h" 5 #include "components/arc/ime/arc_ime_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 ui::InputMethod* test_input_method) { 96 ui::InputMethod* test_input_method) {
97 test_input_method_ = test_input_method; 97 test_input_method_ = test_input_method;
98 } 98 }
99 99
100 void ArcImeService::SetArcWindowDelegateForTesting( 100 void ArcImeService::SetArcWindowDelegateForTesting(
101 std::unique_ptr<ArcWindowDelegate> delegate) { 101 std::unique_ptr<ArcWindowDelegate> delegate) {
102 arc_window_delegate_= std::move(delegate); 102 arc_window_delegate_= std::move(delegate);
103 } 103 }
104 104
105 ui::InputMethod* ArcImeService::GetInputMethod() { 105 ui::InputMethod* ArcImeService::GetInputMethod() {
106 if (focused_arc_window_.windows().empty()) 106 if (!focused_arc_window_)
107 return nullptr; 107 return nullptr;
108 108
109 if (test_input_method_) 109 if (test_input_method_)
110 return test_input_method_; 110 return test_input_method_;
111 return focused_arc_window_.windows().front()->GetHost()->GetInputMethod(); 111
112 DCHECK(focused_arc_window_->GetHost());
113 return focused_arc_window_->GetHost()->GetInputMethod();
112 } 114 }
113 115
114 //////////////////////////////////////////////////////////////////////////////// 116 ////////////////////////////////////////////////////////////////////////////////
115 // Overridden from aura::EnvObserver: 117 // Overridden from aura::EnvObserver:
116 118
117 void ArcImeService::OnWindowInitialized(aura::Window* new_window) { 119 void ArcImeService::OnWindowInitialized(aura::Window* new_window) {
118 if (arc_window_delegate_->IsArcWindow(new_window)) { 120 if (arc_window_delegate_->IsArcWindow(new_window)) {
119 if (!is_focus_observer_installed_) { 121 if (!is_focus_observer_installed_) {
120 arc_window_delegate_->RegisterFocusObserver(); 122 arc_window_delegate_->RegisterFocusObserver();
121 is_focus_observer_installed_ = true; 123 is_focus_observer_installed_ = true;
122 } 124 }
123 } 125 }
124 keyboard::KeyboardController* keyboard_controller = 126 keyboard::KeyboardController* keyboard_controller =
125 keyboard::KeyboardController::GetInstance(); 127 keyboard::KeyboardController::GetInstance();
126 if (keyboard_controller && keyboard_controller_ != keyboard_controller) { 128 if (keyboard_controller && keyboard_controller_ != keyboard_controller) {
127 // Registering |this| as an observer only once per KeyboardController. 129 // Registering |this| as an observer only once per KeyboardController.
128 keyboard_controller_ = keyboard_controller; 130 keyboard_controller_ = keyboard_controller;
129 keyboard_controller_->AddObserver(this); 131 keyboard_controller_->AddObserver(this);
130 } 132 }
131 } 133 }
132 134
133 //////////////////////////////////////////////////////////////////////////////// 135 ////////////////////////////////////////////////////////////////////////////////
136 // Overridden from aura::WindowObserver:
137
138 void ArcImeService::OnWindowDestroying(aura::Window* window) {
139 // This shouldn't be reached on production, since the window lost the focus
140 // and called OnWindowFocused() before destroying.
141 // But we handle this case for testing.
142 DCHECK_EQ(window, focused_arc_window_);
143 OnWindowFocused(nullptr, focused_arc_window_);
144 }
145
146 void ArcImeService::OnWindowRemovingFromRootWindow(aura::Window* window,
147 aura::Window* new_root) {
148 DCHECK_EQ(window, focused_arc_window_);
149 OnWindowFocused(nullptr, focused_arc_window_);
150 }
151
152 ////////////////////////////////////////////////////////////////////////////////
134 // Overridden from exo::WMHelper::FocusChangeObserver: 153 // Overridden from exo::WMHelper::FocusChangeObserver:
135 154
136 void ArcImeService::OnWindowFocused(aura::Window* gained_focus, 155 void ArcImeService::OnWindowFocused(aura::Window* gained_focus,
137 aura::Window* lost_focus) { 156 aura::Window* lost_focus) {
138 if (lost_focus == gained_focus) 157 if (lost_focus == gained_focus)
139 return; 158 return;
140 159
141 const bool detach = (lost_focus && focused_arc_window_.Contains(lost_focus)); 160 const bool detach = (lost_focus && focused_arc_window_ == lost_focus);
142 const bool attach = 161 const bool attach =
143 (gained_focus && arc_window_delegate_->IsArcWindow(gained_focus)); 162 (gained_focus && arc_window_delegate_->IsArcWindow(gained_focus));
144 163
145 // TODO(kinaba): Implicit dependency in GetInputMethod as described below is 164 // TODO(kinaba): Implicit dependency in GetInputMethod as described below is
146 // confusing. Consider getting InputMethod directly from lost_ or gained_focus 165 // confusing. Consider getting InputMethod directly from lost_ or gained_focus
147 // variables. For that, we need to change how to inject testing InputMethod. 166 // variables. For that, we need to change how to inject testing InputMethod.
148 // 167 //
149 // GetInputMethod() retrieves the input method associated to 168 // GetInputMethod() retrieves the input method associated to
150 // |forcused_arc_window_|. Hence, to get the object we are detaching from, we 169 // |forcused_arc_window_|. Hence, to get the object we are detaching from, we
151 // must call the method before updating the forcused ARC window. 170 // must call the method before updating the forcused ARC window.
152 ui::InputMethod* const detaching_ime = detach ? GetInputMethod() : nullptr; 171 ui::InputMethod* const detaching_ime = detach ? GetInputMethod() : nullptr;
153 172
154 if (detach) 173 if (detach) {
155 focused_arc_window_.Remove(lost_focus); 174 focused_arc_window_->RemoveObserver(this);
156 if (attach) 175 focused_arc_window_ = nullptr;
157 focused_arc_window_.Add(gained_focus); 176 }
177 if (attach) {
178 DCHECK_EQ(nullptr, focused_arc_window_);
179 focused_arc_window_ = gained_focus;
180 focused_arc_window_->AddObserver(this);
181 }
158 182
159 ui::InputMethod* const attaching_ime = attach ? GetInputMethod() : nullptr; 183 ui::InputMethod* const attaching_ime = attach ? GetInputMethod() : nullptr;
160 184
161 // Notify to the input method, either when this service is detached or 185 // Notify to the input method, either when this service is detached or
162 // attached. Do nothing when the focus is moving between ARC windows, 186 // attached. Do nothing when the focus is moving between ARC windows,
163 // to avoid unpexpected context reset in ARC. 187 // to avoid unpexpected context reset in ARC.
164 if (detaching_ime != attaching_ime) { 188 if (detaching_ime != attaching_ime) {
165 if (detaching_ime) 189 if (detaching_ime)
166 detaching_ime->DetachTextInputClient(this); 190 detaching_ime->DetachTextInputClient(this);
167 if (attaching_ime) 191 if (attaching_ime)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 void ArcImeService::ShowImeIfNeeded() { 225 void ArcImeService::ShowImeIfNeeded() {
202 ui::InputMethod* const input_method = GetInputMethod(); 226 ui::InputMethod* const input_method = GetInputMethod();
203 if (input_method && input_method->GetTextInputClient() == this) { 227 if (input_method && input_method->GetTextInputClient() == this) {
204 input_method->ShowImeIfNeeded(); 228 input_method->ShowImeIfNeeded();
205 } 229 }
206 } 230 }
207 231
208 //////////////////////////////////////////////////////////////////////////////// 232 ////////////////////////////////////////////////////////////////////////////////
209 // Overridden from keyboard::KeyboardControllerObserver 233 // Overridden from keyboard::KeyboardControllerObserver
210 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) { 234 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
211 if (focused_arc_window_.windows().empty()) 235 if (!focused_arc_window_)
212 return; 236 return;
213 aura::Window* window = focused_arc_window_.windows().front(); 237 aura::Window* window = focused_arc_window_;
214 // Multiply by the scale factor. To convert from DPI to physical pixels. 238 // Multiply by the scale factor. To convert from DPI to physical pixels.
215 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect( 239 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect(
216 new_bounds, window->layer()->device_scale_factor()); 240 new_bounds, window->layer()->device_scale_factor());
hidehiko 2017/02/01 04:12:35 nit: you can inline |focused_arc_window_| here.
217 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px); 241 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px);
218 } 242 }
219 243
220 void ArcImeService::OnKeyboardClosed() {} 244 void ArcImeService::OnKeyboardClosed() {}
221 245
222 //////////////////////////////////////////////////////////////////////////////// 246 ////////////////////////////////////////////////////////////////////////////////
223 // Overridden from ui::TextInputClient: 247 // Overridden from ui::TextInputClient:
224 248
225 void ArcImeService::SetCompositionText( 249 void ArcImeService::SetCompositionText(
226 const ui::CompositionText& composition) { 250 const ui::CompositionText& composition) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 has_composition_text_ = false; 306 has_composition_text_ = false;
283 ime_bridge_->SendInsertText(base::string16(1, event.GetText())); 307 ime_bridge_->SendInsertText(base::string16(1, event.GetText()));
284 } 308 }
285 } 309 }
286 310
287 ui::TextInputType ArcImeService::GetTextInputType() const { 311 ui::TextInputType ArcImeService::GetTextInputType() const {
288 return ime_type_; 312 return ime_type_;
289 } 313 }
290 314
291 gfx::Rect ArcImeService::GetCaretBounds() const { 315 gfx::Rect ArcImeService::GetCaretBounds() const {
292 if (focused_arc_window_.windows().empty()) 316 if (!focused_arc_window_)
293 return gfx::Rect(); 317 return gfx::Rect();
294 aura::Window* window = focused_arc_window_.windows().front(); 318 aura::Window* window = focused_arc_window_;
295 319
296 // |cursor_rect_| holds the rectangle reported from ARC apps, in the "screen 320 // |cursor_rect_| holds the rectangle reported from ARC apps, in the "screen
297 // coordinates" in ARC, counted by physical pixels. 321 // coordinates" in ARC, counted by physical pixels.
298 // Chrome OS input methods expect the coordinates in Chrome OS screen, within 322 // Chrome OS input methods expect the coordinates in Chrome OS screen, within
299 // device independent pixels. Two factors are involved for the conversion. 323 // device independent pixels. Two factors are involved for the conversion.
300 324
301 // Divide by the scale factor. To convert from physical pixels to DIP. 325 // Divide by the scale factor. To convert from physical pixels to DIP.
302 gfx::Rect converted = gfx::ScaleToEnclosingRect( 326 gfx::Rect converted = gfx::ScaleToEnclosingRect(
303 cursor_rect_, 1 / window->layer()->device_scale_factor()); 327 cursor_rect_, 1 / window->layer()->device_scale_factor());
304 328
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 base::i18n::TextDirection direction) { 393 base::i18n::TextDirection direction) {
370 return false; 394 return false;
371 } 395 }
372 396
373 bool ArcImeService::IsTextEditCommandEnabled( 397 bool ArcImeService::IsTextEditCommandEnabled(
374 ui::TextEditCommand command) const { 398 ui::TextEditCommand command) const {
375 return false; 399 return false;
376 } 400 }
377 401
378 } // namespace arc 402 } // namespace arc
OLDNEW
« components/arc/ime/arc_ime_service.h ('K') | « components/arc/ime/arc_ime_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698