| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/input_method/browser_state_monitor.h" | 5 #include "chrome/browser/chromeos/input_method/browser_state_monitor.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 9 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 10 #include "chromeos/ime/input_method_delegate.h" | 10 #include "chromeos/ime/input_method_delegate.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace chromeos { |
| 14 namespace input_method { | 14 namespace input_method { |
| 15 | 15 |
| 16 BrowserStateMonitor::BrowserStateMonitor( | 16 BrowserStateMonitor::BrowserStateMonitor( |
| 17 const base::Callback<void(InputMethodManager::State)>& observer) | 17 const base::Callback<void(InputMethodManager::UISessionState)>& observer) |
| 18 : observer_(observer), | 18 : observer_(observer), ui_session_(InputMethodManager::STATE_LOGIN_SCREEN) { |
| 19 state_(InputMethodManager::STATE_LOGIN_SCREEN) { | |
| 20 notification_registrar_.Add(this, | 19 notification_registrar_.Add(this, |
| 21 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 20 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 22 content::NotificationService::AllSources()); | 21 content::NotificationService::AllSources()); |
| 23 notification_registrar_.Add(this, | 22 notification_registrar_.Add(this, |
| 24 chrome::NOTIFICATION_SESSION_STARTED, | 23 chrome::NOTIFICATION_SESSION_STARTED, |
| 25 content::NotificationService::AllSources()); | 24 content::NotificationService::AllSources()); |
| 26 notification_registrar_.Add(this, | 25 notification_registrar_.Add(this, |
| 27 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | 26 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, |
| 28 content::NotificationService::AllSources()); | 27 content::NotificationService::AllSources()); |
| 29 // We should not use ALL_BROWSERS_CLOSING here since logout might be cancelled | 28 // We should not use ALL_BROWSERS_CLOSING here since logout might be cancelled |
| 30 // by JavaScript after ALL_BROWSERS_CLOSING is sent (crosbug.com/11055). | 29 // by JavaScript after ALL_BROWSERS_CLOSING is sent (crosbug.com/11055). |
| 31 notification_registrar_.Add(this, | 30 notification_registrar_.Add(this, |
| 32 chrome::NOTIFICATION_APP_TERMINATING, | 31 chrome::NOTIFICATION_APP_TERMINATING, |
| 33 content::NotificationService::AllSources()); | 32 content::NotificationService::AllSources()); |
| 34 | 33 |
| 35 if (!observer_.is_null()) | 34 if (!observer_.is_null()) |
| 36 observer_.Run(state_); | 35 observer_.Run(ui_session_); |
| 37 } | 36 } |
| 38 | 37 |
| 39 BrowserStateMonitor::~BrowserStateMonitor() { | 38 BrowserStateMonitor::~BrowserStateMonitor() { |
| 40 } | 39 } |
| 41 | 40 |
| 42 void BrowserStateMonitor::Observe( | 41 void BrowserStateMonitor::Observe( |
| 43 int type, | 42 int type, |
| 44 const content::NotificationSource& source, | 43 const content::NotificationSource& source, |
| 45 const content::NotificationDetails& details) { | 44 const content::NotificationDetails& details) { |
| 46 const InputMethodManager::State old_state = state_; | 45 const InputMethodManager::UISessionState old_ui_session = ui_session_; |
| 47 switch (type) { | 46 switch (type) { |
| 48 case chrome::NOTIFICATION_APP_TERMINATING: { | 47 case chrome::NOTIFICATION_APP_TERMINATING: { |
| 49 state_ = InputMethodManager::STATE_TERMINATING; | 48 ui_session_ = InputMethodManager::STATE_TERMINATING; |
| 50 break; | 49 break; |
| 51 } | 50 } |
| 52 case chrome::NOTIFICATION_LOGIN_USER_CHANGED: { | 51 case chrome::NOTIFICATION_LOGIN_USER_CHANGED: { |
| 53 // The user logged in, but the browser window for user session is not yet | 52 // The user logged in, but the browser window for user session is not yet |
| 54 // ready. An initial input method hasn't been set to the manager. | 53 // ready. An initial input method hasn't been set to the manager. |
| 55 // Note that the notification is also sent when Chrome crashes/restarts | 54 // Note that the notification is also sent when Chrome crashes/restarts |
| 56 // as of writing, but it might be changed in the future (therefore we need | 55 // as of writing, but it might be changed in the future (therefore we need |
| 57 // to listen to NOTIFICATION_SESSION_STARTED as well.) | 56 // to listen to NOTIFICATION_SESSION_STARTED as well.) |
| 58 DVLOG(1) << "Received chrome::NOTIFICATION_LOGIN_USER_CHANGED"; | 57 DVLOG(1) << "Received chrome::NOTIFICATION_LOGIN_USER_CHANGED"; |
| 59 state_ = InputMethodManager::STATE_BROWSER_SCREEN; | 58 ui_session_ = InputMethodManager::STATE_BROWSER_SCREEN; |
| 60 break; | 59 break; |
| 61 } | 60 } |
| 62 case chrome::NOTIFICATION_SESSION_STARTED: { | 61 case chrome::NOTIFICATION_SESSION_STARTED: { |
| 63 // The user logged in, and the browser window for user session is ready. | 62 // The user logged in, and the browser window for user session is ready. |
| 64 // An initial input method has already been set. | 63 // An initial input method has already been set. |
| 65 // We should NOT call InitializePrefMembers() here since the notification | 64 // We should NOT call InitializePrefMembers() here since the notification |
| 66 // is sent in the PreProfileInit phase in case when Chrome crashes and | 65 // is sent in the PreProfileInit phase in case when Chrome crashes and |
| 67 // restarts. | 66 // restarts. |
| 68 DVLOG(1) << "Received chrome::NOTIFICATION_SESSION_STARTED"; | 67 DVLOG(1) << "Received chrome::NOTIFICATION_SESSION_STARTED"; |
| 69 state_ = InputMethodManager::STATE_BROWSER_SCREEN; | 68 ui_session_ = InputMethodManager::STATE_BROWSER_SCREEN; |
| 70 break; | 69 break; |
| 71 } | 70 } |
| 72 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED: { | 71 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED: { |
| 73 const bool is_screen_locked = *content::Details<bool>(details).ptr(); | 72 const bool is_screen_locked = *content::Details<bool>(details).ptr(); |
| 74 state_ = is_screen_locked ? InputMethodManager::STATE_LOCK_SCREEN : | 73 ui_session_ = is_screen_locked ? InputMethodManager::STATE_LOCK_SCREEN |
| 75 InputMethodManager::STATE_BROWSER_SCREEN; | 74 : InputMethodManager::STATE_BROWSER_SCREEN; |
| 76 break; | 75 break; |
| 77 } | 76 } |
| 78 default: { | 77 default: { |
| 79 NOTREACHED(); | 78 NOTREACHED(); |
| 80 break; | 79 break; |
| 81 } | 80 } |
| 82 } | 81 } |
| 83 | 82 |
| 84 if (old_state != state_ && !observer_.is_null()) | 83 if (old_ui_session != ui_session_ && !observer_.is_null()) |
| 85 observer_.Run(state_); | 84 observer_.Run(ui_session_); |
| 86 | 85 |
| 87 // Note: browser notifications are sent in the following order. | 86 // Note: browser notifications are sent in the following order. |
| 88 // | 87 // |
| 89 // Normal login: | 88 // Normal login: |
| 90 // 1. chrome::NOTIFICATION_LOGIN_USER_CHANGED is sent. | 89 // 1. chrome::NOTIFICATION_LOGIN_USER_CHANGED is sent. |
| 91 // 2. Preferences::NotifyPrefChanged() is called. preload_engines (which | 90 // 2. Preferences::NotifyPrefChanged() is called. preload_engines (which |
| 92 // might change the current input method) and current/previous input method | 91 // might change the current input method) and current/previous input method |
| 93 // are sent to the manager. | 92 // are sent to the manager. |
| 94 // 3. chrome::NOTIFICATION_SESSION_STARTED is sent. | 93 // 3. chrome::NOTIFICATION_SESSION_STARTED is sent. |
| 95 // | 94 // |
| 96 // Chrome crash/restart (after logging in): | 95 // Chrome crash/restart (after logging in): |
| 97 // 1. chrome::NOTIFICATION_LOGIN_USER_CHANGED might be sent. | 96 // 1. chrome::NOTIFICATION_LOGIN_USER_CHANGED might be sent. |
| 98 // 2. chrome::NOTIFICATION_SESSION_STARTED is sent. | 97 // 2. chrome::NOTIFICATION_SESSION_STARTED is sent. |
| 99 // 3. Preferences::NotifyPrefChanged() is called. The same things as above | 98 // 3. Preferences::NotifyPrefChanged() is called. The same things as above |
| 100 // happen. | 99 // happen. |
| 101 // | 100 // |
| 102 // We have to be careful not to overwrite both local and user prefs when | 101 // We have to be careful not to overwrite both local and user prefs when |
| 103 // preloaded engine is set. Note that it does not work to do nothing in | 102 // preloaded engine is set. Note that it does not work to do nothing in |
| 104 // InputMethodChanged() between chrome::NOTIFICATION_LOGIN_USER_CHANGED and | 103 // InputMethodChanged() between chrome::NOTIFICATION_LOGIN_USER_CHANGED and |
| 105 // chrome::NOTIFICATION_SESSION_STARTED because SESSION_STARTED is sent very | 104 // chrome::NOTIFICATION_SESSION_STARTED because SESSION_STARTED is sent very |
| 106 // early on Chrome crash/restart. | 105 // early on Chrome crash/restart. |
| 107 } | 106 } |
| 108 | 107 |
| 109 } // namespace input_method | 108 } // namespace input_method |
| 110 } // namespace chromeos | 109 } // namespace chromeos |
| OLD | NEW |