| OLD | NEW |
| 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 "chrome/browser/chromeos/arc/arc_support_host.h" | 5 #include "chrome/browser/chromeos/arc/arc_support_host.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "ash/common/system/chromeos/devicetype_utils.h" | 9 #include "ash/common/system/chromeos/devicetype_utils.h" |
| 10 #include "base/i18n/timezone.h" | 10 #include "base/i18n/timezone.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/chromeos/arc/optin/arc_optin_preference_handler.h" | 16 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 17 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 17 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 19 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 20 #include "chrome/browser/ui/browser_commands.h" | |
| 21 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 22 #include "components/user_manager/known_user.h" | 21 #include "components/user_manager/known_user.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/webui/web_ui_util.h" | 23 #include "ui/base/webui/web_ui_util.h" |
| 25 #include "ui/display/screen.h" | 24 #include "ui/display/screen.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 constexpr char kAction[] = "action"; | 27 constexpr char kAction[] = "action"; |
| 29 constexpr char kArcManaged[] = "arcManaged"; | 28 constexpr char kArcManaged[] = "arcManaged"; |
| 30 constexpr char kData[] = "data"; | 29 constexpr char kData[] = "data"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 48 // The key of the event type. | 47 // The key of the event type. |
| 49 constexpr char kEvent[] = "event"; | 48 constexpr char kEvent[] = "event"; |
| 50 | 49 |
| 51 // "onWindowClosed" is fired when the extension window is closed. | 50 // "onWindowClosed" is fired when the extension window is closed. |
| 52 // No data will be provided. | 51 // No data will be provided. |
| 53 constexpr char kEventOnWindowClosed[] = "onWindowClosed"; | 52 constexpr char kEventOnWindowClosed[] = "onWindowClosed"; |
| 54 | 53 |
| 55 // "onAuthSucceeded" is fired when successfully done to LSO authorization in | 54 // "onAuthSucceeded" is fired when successfully done to LSO authorization in |
| 56 // extension. | 55 // extension. |
| 57 // The auth token is passed via "code" field. | 56 // The auth token is passed via "code" field. |
| 58 constexpr char kEventOnAuthSuccedded[] = "onAuthSucceeded"; | 57 constexpr char kEventOnAuthSucceeded[] = "onAuthSucceeded"; |
| 59 constexpr char kCode[] = "code"; | 58 constexpr char kCode[] = "code"; |
| 60 | 59 |
| 61 // "onAgree" is fired when a user clicks "Agree" button. | 60 // "onAgree" is fired when a user clicks "Agree" button. |
| 62 // The message should have the following three fields: | 61 // The message should have the following three fields: |
| 63 // - isMetricsEnabled | 62 // - isMetricsEnabled |
| 64 // - isBackupRestoreEnabled | 63 // - isBackupRestoreEnabled |
| 65 // - isLocationServiceEnabled | 64 // - isLocationServiceEnabled |
| 66 constexpr char kEventOnAgreed[] = "onAgreed"; | 65 constexpr char kEventOnAgreed[] = "onAgreed"; |
| 67 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; | 66 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; |
| 68 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; | 67 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; |
| 69 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; | 68 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; |
| 70 | 69 |
| 71 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. | 70 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. |
| 72 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; | 71 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; |
| 73 | 72 |
| 74 } // namespace | 73 } // namespace |
| 75 | 74 |
| 76 // static | 75 // static |
| 77 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | 76 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; |
| 78 | 77 |
| 79 // static | 78 // static |
| 80 const char ArcSupportHost::kStorageId[] = "arc_support"; | 79 const char ArcSupportHost::kStorageId[] = "arc_support"; |
| 81 | 80 |
| 82 ArcSupportHost::ArcSupportHost() { | 81 ArcSupportHost::ArcSupportHost() = default; |
| 83 // TODO(hidehiko): Get rid of dependency to ArcAuthService. | |
| 84 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | |
| 85 DCHECK(arc_auth_service); | |
| 86 | |
| 87 if (!arc_auth_service->IsAllowed()) | |
| 88 return; | |
| 89 } | |
| 90 | 82 |
| 91 ArcSupportHost::~ArcSupportHost() { | 83 ArcSupportHost::~ArcSupportHost() { |
| 92 if (message_host_) | 84 if (message_host_) |
| 93 DisconnectMessageHost(); | 85 DisconnectMessageHost(); |
| 94 } | 86 } |
| 95 | 87 |
| 96 void ArcSupportHost::Close() { | 88 void ArcSupportHost::Close() { |
| 97 if (!message_host_) { | 89 if (!message_host_) { |
| 98 VLOG(2) << "ArcSupportHost::Close() is called " | 90 VLOG(2) << "ArcSupportHost::Close() is called " |
| 99 << "but message_host_ is not available."; | 91 << "but message_host_ is not available."; |
| 100 return; | 92 return; |
| 101 } | 93 } |
| 102 | 94 |
| 103 base::DictionaryValue message; | 95 base::DictionaryValue message; |
| 104 message.SetString(kAction, kActionCloseWindow); | 96 message.SetString(kAction, kActionCloseWindow); |
| 105 message_host_->SendMessage(message); | 97 message_host_->SendMessage(message); |
| 106 | 98 |
| 107 // Disconnect immediately, so that onWindowClosed event will not be | 99 // Disconnect immediately, so that onWindowClosed event will not be |
| 108 // delivered to here. | 100 // delivered to here. |
| 109 DisconnectMessageHost(); | 101 DisconnectMessageHost(); |
| 110 } | 102 } |
| 111 | 103 |
| 112 void ArcSupportHost::ShowPage(arc::ArcAuthService::UIPage page, | 104 void ArcSupportHost::ShowPage(UIPage page, const base::string16& status) { |
| 113 const base::string16& status) { | |
| 114 if (!message_host_) { | 105 if (!message_host_) { |
| 115 VLOG(2) << "ArcSupportHost::ShowPage() is called " | 106 VLOG(2) << "ArcSupportHost::ShowPage() is called " |
| 116 << "but message_host_ is not available."; | 107 << "but message_host_ is not available."; |
| 117 return; | 108 return; |
| 118 } | 109 } |
| 119 | 110 |
| 120 base::DictionaryValue message; | 111 base::DictionaryValue message; |
| 121 message.SetString(kAction, kActionShowPage); | 112 message.SetString(kAction, kActionShowPage); |
| 122 message.SetInteger(kPage, static_cast<int>(page)); | 113 message.SetInteger(kPage, static_cast<int>(page)); |
| 123 message.SetString(kStatus, status); | 114 message.SetString(kStatus, status); |
| 124 message_host_->SendMessage(message); | 115 message_host_->SendMessage(message); |
| 125 } | 116 } |
| 126 | 117 |
| 118 void ArcSupportHost::SetMetricsPreferenceCheckbox(bool is_enabled, |
| 119 bool is_managed) { |
| 120 metrics_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 121 SendPreferenceCheckboxUpdate(kActionSetMetricsMode, metrics_checkbox_); |
| 122 } |
| 123 |
| 124 void ArcSupportHost::SetBackupAndRestorePreferenceCheckbox(bool is_enabled, |
| 125 bool is_managed) { |
| 126 backup_and_restore_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 127 SendPreferenceCheckboxUpdate(kActionBackupAndRestoreMode, |
| 128 backup_and_restore_checkbox_); |
| 129 } |
| 130 |
| 131 void ArcSupportHost::SetLocationServicesPreferenceCheckbox(bool is_enabled, |
| 132 bool is_managed) { |
| 133 location_services_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 134 SendPreferenceCheckboxUpdate(kActionLocationServiceMode, |
| 135 location_services_checkbox_); |
| 136 } |
| 137 |
| 138 void ArcSupportHost::SendPreferenceCheckboxUpdate( |
| 139 const std::string& action_name, |
| 140 const PreferenceCheckboxData& data) { |
| 141 if (!message_host_) |
| 142 return; |
| 143 |
| 144 base::DictionaryValue message; |
| 145 message.SetString(kAction, action_name); |
| 146 message.SetBoolean(kEnabled, data.is_enabled); |
| 147 message.SetBoolean(kManaged, data.is_managed); |
| 148 message_host_->SendMessage(message); |
| 149 } |
| 150 |
| 127 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) { | 151 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) { |
| 128 if (message_host_ == message_host) | 152 if (message_host_ == message_host) |
| 129 return; | 153 return; |
| 130 | 154 |
| 131 if (message_host_) | 155 if (message_host_) |
| 132 DisconnectMessageHost(); | 156 DisconnectMessageHost(); |
| 133 message_host_ = message_host; | 157 message_host_ = message_host; |
| 134 message_host_->SetObserver(this); | 158 message_host_->SetObserver(this); |
| 135 display::Screen::GetScreen()->AddObserver(this); | 159 display::Screen::GetScreen()->AddObserver(this); |
| 136 | 160 |
| 137 if (!Initialize()) { | 161 if (!Initialize()) { |
| 138 Close(); | 162 Close(); |
| 139 return; | 163 return; |
| 140 } | 164 } |
| 141 | 165 |
| 166 SendPreferenceCheckboxUpdate(kActionSetMetricsMode, metrics_checkbox_); |
| 167 SendPreferenceCheckboxUpdate(kActionBackupAndRestoreMode, |
| 168 backup_and_restore_checkbox_); |
| 169 SendPreferenceCheckboxUpdate(kActionLocationServiceMode, |
| 170 location_services_checkbox_); |
| 171 |
| 142 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 172 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 143 DCHECK(arc_auth_service); | 173 DCHECK(arc_auth_service); |
| 144 | |
| 145 preference_handler_ = base::MakeUnique<arc::ArcOptInPreferenceHandler>( | |
| 146 this, arc_auth_service->profile()->GetPrefs()); | |
| 147 // This automatically updates all preferences. | |
| 148 preference_handler_->Start(); | |
| 149 | |
| 150 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status()); | 174 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status()); |
| 151 } | 175 } |
| 152 | 176 |
| 153 void ArcSupportHost::UnsetMessageHost( | 177 void ArcSupportHost::UnsetMessageHost( |
| 154 arc::ArcSupportMessageHost* message_host) { | 178 arc::ArcSupportMessageHost* message_host) { |
| 155 if (message_host_ != message_host) | 179 if (message_host_ != message_host) |
| 156 return; | 180 return; |
| 157 DisconnectMessageHost(); | 181 DisconnectMessageHost(); |
| 158 } | 182 } |
| 159 | 183 |
| 160 void ArcSupportHost::DisconnectMessageHost() { | 184 void ArcSupportHost::DisconnectMessageHost() { |
| 161 DCHECK(message_host_); | 185 DCHECK(message_host_); |
| 162 preference_handler_.reset(); | |
| 163 display::Screen::GetScreen()->RemoveObserver(this); | 186 display::Screen::GetScreen()->RemoveObserver(this); |
| 164 message_host_->SetObserver(nullptr); | 187 message_host_->SetObserver(nullptr); |
| 165 message_host_ = nullptr; | 188 message_host_ = nullptr; |
| 166 } | 189 } |
| 167 | 190 |
| 168 bool ArcSupportHost::Initialize() { | 191 bool ArcSupportHost::Initialize() { |
| 169 DCHECK(message_host_); | 192 DCHECK(message_host_); |
| 170 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 193 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 171 if (!arc_auth_service->IsAllowed()) | 194 if (!arc_auth_service->IsAllowed()) |
| 172 return false; | 195 return false; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, | 296 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, |
| 274 uint32_t changed_metrics) { | 297 uint32_t changed_metrics) { |
| 275 if (!message_host_) | 298 if (!message_host_) |
| 276 return; | 299 return; |
| 277 | 300 |
| 278 base::DictionaryValue message; | 301 base::DictionaryValue message; |
| 279 message.SetString(kAction, kActionSetWindowBounds); | 302 message.SetString(kAction, kActionSetWindowBounds); |
| 280 message_host_->SendMessage(message); | 303 message_host_->SendMessage(message); |
| 281 } | 304 } |
| 282 | 305 |
| 283 void ArcSupportHost::OnMetricsModeChanged(bool enabled, bool managed) { | |
| 284 SendPreferenceUpdate(kActionSetMetricsMode, enabled, managed); | |
| 285 } | |
| 286 | |
| 287 void ArcSupportHost::OnBackupAndRestoreModeChanged(bool enabled, bool managed) { | |
| 288 SendPreferenceUpdate(kActionBackupAndRestoreMode, enabled, managed); | |
| 289 } | |
| 290 | |
| 291 void ArcSupportHost::OnLocationServicesModeChanged(bool enabled, bool managed) { | |
| 292 SendPreferenceUpdate(kActionLocationServiceMode, enabled, managed); | |
| 293 } | |
| 294 | |
| 295 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, | |
| 296 bool is_enabled, | |
| 297 bool is_managed) { | |
| 298 if (!message_host_) | |
| 299 return; | |
| 300 | |
| 301 base::DictionaryValue message; | |
| 302 message.SetString(kAction, action_name); | |
| 303 message.SetBoolean(kEnabled, is_enabled); | |
| 304 message.SetBoolean(kManaged, is_managed); | |
| 305 message_host_->SendMessage(message); | |
| 306 } | |
| 307 | |
| 308 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) { | 306 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) { |
| 309 std::string event; | 307 std::string event; |
| 310 if (!message.GetString(kEvent, &event)) { | 308 if (!message.GetString(kEvent, &event)) { |
| 311 NOTREACHED(); | 309 NOTREACHED(); |
| 312 return; | 310 return; |
| 313 } | 311 } |
| 314 | 312 |
| 315 // TODO(hidehiko): Replace by Observer. | 313 if (!observer_) { |
| 316 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 314 LOG(ERROR) << "No observer is found."; |
| 317 DCHECK(arc_auth_service); | 315 return; |
| 318 | 316 } |
| 319 DCHECK(preference_handler_); | |
| 320 | 317 |
| 321 if (event == kEventOnWindowClosed) { | 318 if (event == kEventOnWindowClosed) { |
| 322 arc_auth_service->CancelAuthCode(); | 319 observer_->OnWindowClosed(); |
| 323 } else if (event == kEventOnAuthSuccedded) { | 320 } else if (event == kEventOnAuthSucceeded) { |
| 324 std::string code; | 321 std::string code; |
| 325 if (message.GetString(kCode, &code)) { | 322 if (message.GetString(kCode, &code)) { |
| 326 arc_auth_service->SetAuthCodeAndStartArc(code); | 323 observer_->OnAuthSucceeded(code); |
| 327 } else { | 324 } else { |
| 328 NOTREACHED(); | 325 NOTREACHED(); |
| 329 } | 326 } |
| 330 } else if (event == kEventOnAgreed) { | 327 } else if (event == kEventOnAgreed) { |
| 331 bool is_metrics_enabled; | 328 bool is_metrics_enabled; |
| 332 bool is_backup_restore_enabled; | 329 bool is_backup_restore_enabled; |
| 333 bool is_location_service_enabled; | 330 bool is_location_service_enabled; |
| 334 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && | 331 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && |
| 335 message.GetBoolean(kIsBackupRestoreEnabled, | 332 message.GetBoolean(kIsBackupRestoreEnabled, |
| 336 &is_backup_restore_enabled) && | 333 &is_backup_restore_enabled) && |
| 337 message.GetBoolean(kIsLocationServiceEnabled, | 334 message.GetBoolean(kIsLocationServiceEnabled, |
| 338 &is_location_service_enabled)) { | 335 &is_location_service_enabled)) { |
| 339 preference_handler_->EnableMetrics(is_metrics_enabled); | 336 observer_->OnTermsAgreed(is_metrics_enabled, is_backup_restore_enabled, |
| 340 preference_handler_->EnableBackupRestore(is_backup_restore_enabled); | 337 is_location_service_enabled); |
| 341 preference_handler_->EnableLocationService(is_location_service_enabled); | |
| 342 arc_auth_service->StartLso(); | |
| 343 } else { | 338 } else { |
| 344 NOTREACHED(); | 339 NOTREACHED(); |
| 345 } | 340 } |
| 346 } else if (event == kEventOnSendFeedbackClicked) { | 341 } else if (event == kEventOnSendFeedbackClicked) { |
| 347 chrome::OpenFeedbackDialog(nullptr); | 342 observer_->OnSendFeedbackClicked(); |
| 348 } else { | 343 } else { |
| 349 LOG(ERROR) << "Unknown message: " << event; | 344 LOG(ERROR) << "Unknown message: " << event; |
| 350 NOTREACHED(); | 345 NOTREACHED(); |
| 351 } | 346 } |
| 352 } | 347 } |
| OLD | NEW |