| 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 25 matching lines...) Expand all Loading... |
| 56 // The key of the event type. | 55 // The key of the event type. |
| 57 constexpr char kEvent[] = "event"; | 56 constexpr char kEvent[] = "event"; |
| 58 | 57 |
| 59 // "onWindowClosed" is fired when the extension window is closed. | 58 // "onWindowClosed" is fired when the extension window is closed. |
| 60 // No data will be provided. | 59 // No data will be provided. |
| 61 constexpr char kEventOnWindowClosed[] = "onWindowClosed"; | 60 constexpr char kEventOnWindowClosed[] = "onWindowClosed"; |
| 62 | 61 |
| 63 // "onAuthSucceeded" is fired when successfully done to LSO authorization in | 62 // "onAuthSucceeded" is fired when successfully done to LSO authorization in |
| 64 // extension. | 63 // extension. |
| 65 // The auth token is passed via "code" field. | 64 // The auth token is passed via "code" field. |
| 66 constexpr char kEventOnAuthSuccedded[] = "onAuthSucceeded"; | 65 constexpr char kEventOnAuthSucceeded[] = "onAuthSucceeded"; |
| 67 constexpr char kCode[] = "code"; | 66 constexpr char kCode[] = "code"; |
| 68 | 67 |
| 69 // "onAgree" is fired when a user clicks "Agree" button. | 68 // "onAgree" is fired when a user clicks "Agree" button. |
| 70 // The message should have the following three fields: | 69 // The message should have the following three fields: |
| 71 // - isMetricsEnabled | 70 // - isMetricsEnabled |
| 72 // - isBackupRestoreEnabled | 71 // - isBackupRestoreEnabled |
| 73 // - isLocationServiceEnabled | 72 // - isLocationServiceEnabled |
| 74 constexpr char kEventOnAgreed[] = "onAgreed"; | 73 constexpr char kEventOnAgreed[] = "onAgreed"; |
| 75 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; | 74 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; |
| 76 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; | 75 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; |
| 77 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; | 76 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; |
| 78 | 77 |
| 79 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. | 78 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. |
| 80 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; | 79 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; |
| 81 | 80 |
| 82 } // namespace | 81 } // namespace |
| 83 | 82 |
| 84 // static | 83 // static |
| 85 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | 84 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; |
| 86 | 85 |
| 87 // static | 86 // static |
| 88 const char ArcSupportHost::kStorageId[] = "arc_support"; | 87 const char ArcSupportHost::kStorageId[] = "arc_support"; |
| 89 | 88 |
| 90 ArcSupportHost::ArcSupportHost() { | 89 ArcSupportHost::ArcSupportHost() = default; |
| 91 // TODO(hidehiko): Get rid of dependency to ArcAuthService. | |
| 92 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | |
| 93 DCHECK(arc_auth_service); | |
| 94 | |
| 95 if (!arc_auth_service->IsAllowed()) | |
| 96 return; | |
| 97 } | |
| 98 | 90 |
| 99 ArcSupportHost::~ArcSupportHost() { | 91 ArcSupportHost::~ArcSupportHost() { |
| 100 if (message_host_) | 92 if (message_host_) |
| 101 DisconnectMessageHost(); | 93 DisconnectMessageHost(); |
| 102 } | 94 } |
| 103 | 95 |
| 96 void ArcSupportHost::AddObserver(Observer* observer) { |
| 97 DCHECK(!observer_); |
| 98 observer_ = observer; |
| 99 } |
| 100 |
| 104 void ArcSupportHost::Close() { | 101 void ArcSupportHost::Close() { |
| 105 if (!message_host_) { | 102 if (!message_host_) { |
| 106 VLOG(2) << "ArcSupportHost::Close() is called " | 103 VLOG(2) << "ArcSupportHost::Close() is called " |
| 107 << "but message_host_ is not available."; | 104 << "but message_host_ is not available."; |
| 108 return; | 105 return; |
| 109 } | 106 } |
| 110 | 107 |
| 111 base::DictionaryValue message; | 108 base::DictionaryValue message; |
| 112 message.SetString(kAction, kActionCloseWindow); | 109 message.SetString(kAction, kActionCloseWindow); |
| 113 message_host_->SendMessage(message); | 110 message_host_->SendMessage(message); |
| 114 | 111 |
| 115 // Disconnect immediately, so that onWindowClosed event will not be | 112 // Disconnect immediately, so that onWindowClosed event will not be |
| 116 // delivered to here. | 113 // delivered to here. |
| 117 DisconnectMessageHost(); | 114 DisconnectMessageHost(); |
| 118 } | 115 } |
| 119 | 116 |
| 120 void ArcSupportHost::ShowPage(arc::ArcAuthService::UIPage page, | 117 void ArcSupportHost::ShowPage(UIPage page, const base::string16& status) { |
| 121 const base::string16& status) { | |
| 122 if (!message_host_) { | 118 if (!message_host_) { |
| 123 VLOG(2) << "ArcSupportHost::ShowPage() is called " | 119 VLOG(2) << "ArcSupportHost::ShowPage() is called " |
| 124 << "but message_host_ is not available."; | 120 << "but message_host_ is not available."; |
| 125 return; | 121 return; |
| 126 } | 122 } |
| 127 | 123 |
| 128 base::DictionaryValue message; | 124 base::DictionaryValue message; |
| 129 if (page == arc::ArcAuthService::UIPage::ERROR || | 125 if (page == UIPage::ERROR || page == UIPage::ERROR_WITH_FEEDBACK) { |
| 130 page == arc::ArcAuthService::UIPage::ERROR_WITH_FEEDBACK) { | |
| 131 message.SetString(kAction, kActionShowErrorPage); | 126 message.SetString(kAction, kActionShowErrorPage); |
| 132 message.SetString(kErrorMessage, status); | 127 message.SetString(kErrorMessage, status); |
| 133 message.SetBoolean( | 128 message.SetBoolean(kShouldShowSendFeedback, |
| 134 kShouldShowSendFeedback, | 129 page == UIPage::ERROR_WITH_FEEDBACK); |
| 135 page == arc::ArcAuthService::UIPage::ERROR_WITH_FEEDBACK); | |
| 136 } else { | 130 } else { |
| 137 message.SetString(kAction, kActionShowPage); | 131 message.SetString(kAction, kActionShowPage); |
| 138 switch (page) { | 132 switch (page) { |
| 139 case arc::ArcAuthService::UIPage::NO_PAGE: | 133 case UIPage::NO_PAGE: |
| 140 message.SetString(kPage, "none"); | 134 message.SetString(kPage, "none"); |
| 141 break; | 135 break; |
| 142 case arc::ArcAuthService::UIPage::TERMS: | 136 case UIPage::TERMS: |
| 143 message.SetString(kPage, "terms"); | 137 message.SetString(kPage, "terms"); |
| 144 break; | 138 break; |
| 145 case arc::ArcAuthService::UIPage::LSO_PROGRESS: | 139 case UIPage::LSO_PROGRESS: |
| 146 message.SetString(kPage, "lso-loading"); | 140 message.SetString(kPage, "lso-loading"); |
| 147 break; | 141 break; |
| 148 // Skip LSO. LSO and LSO_LOADING should be merged well. | 142 // Skip LSO. LSO and LSO_LOADING should be merged well. |
| 149 // TODO(hidehiko): Do it. | 143 // TODO(hidehiko): Do it. |
| 150 case arc::ArcAuthService::UIPage::START_PROGRESS: | 144 case UIPage::START_PROGRESS: |
| 151 message.SetString(kPage, "arc-loading"); | 145 message.SetString(kPage, "arc-loading"); |
| 152 break; | 146 break; |
| 153 default: | 147 default: |
| 154 NOTREACHED(); | 148 NOTREACHED(); |
| 155 return; | 149 return; |
| 156 } | 150 } |
| 157 } | 151 } |
| 158 message_host_->SendMessage(message); | 152 message_host_->SendMessage(message); |
| 159 } | 153 } |
| 160 | 154 |
| 155 void ArcSupportHost::SetMetricsPreferenceCheckbox(bool is_enabled, |
| 156 bool is_managed) { |
| 157 metrics_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 158 SendPreferenceCheckboxUpdate(kActionSetMetricsMode, metrics_checkbox_); |
| 159 } |
| 160 |
| 161 void ArcSupportHost::SetBackupAndRestorePreferenceCheckbox(bool is_enabled, |
| 162 bool is_managed) { |
| 163 backup_and_restore_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 164 SendPreferenceCheckboxUpdate(kActionBackupAndRestoreMode, |
| 165 backup_and_restore_checkbox_); |
| 166 } |
| 167 |
| 168 void ArcSupportHost::SetLocationServicesPreferenceCheckbox(bool is_enabled, |
| 169 bool is_managed) { |
| 170 location_services_checkbox_ = PreferenceCheckboxData(is_enabled, is_managed); |
| 171 SendPreferenceCheckboxUpdate(kActionLocationServiceMode, |
| 172 location_services_checkbox_); |
| 173 } |
| 174 |
| 175 void ArcSupportHost::SendPreferenceCheckboxUpdate( |
| 176 const std::string& action_name, |
| 177 const PreferenceCheckboxData& data) { |
| 178 if (!message_host_) |
| 179 return; |
| 180 |
| 181 base::DictionaryValue message; |
| 182 message.SetString(kAction, action_name); |
| 183 message.SetBoolean(kEnabled, data.is_enabled); |
| 184 message.SetBoolean(kManaged, data.is_managed); |
| 185 message_host_->SendMessage(message); |
| 186 } |
| 187 |
| 161 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) { | 188 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) { |
| 162 if (message_host_ == message_host) | 189 if (message_host_ == message_host) |
| 163 return; | 190 return; |
| 164 | 191 |
| 165 if (message_host_) | 192 if (message_host_) |
| 166 DisconnectMessageHost(); | 193 DisconnectMessageHost(); |
| 167 message_host_ = message_host; | 194 message_host_ = message_host; |
| 168 message_host_->SetObserver(this); | 195 message_host_->SetObserver(this); |
| 169 display::Screen::GetScreen()->AddObserver(this); | 196 display::Screen::GetScreen()->AddObserver(this); |
| 170 | 197 |
| 171 if (!Initialize()) { | 198 if (!Initialize()) { |
| 172 Close(); | 199 Close(); |
| 173 return; | 200 return; |
| 174 } | 201 } |
| 175 | 202 |
| 203 SendPreferenceCheckboxUpdate(kActionSetMetricsMode, metrics_checkbox_); |
| 204 SendPreferenceCheckboxUpdate(kActionBackupAndRestoreMode, |
| 205 backup_and_restore_checkbox_); |
| 206 SendPreferenceCheckboxUpdate(kActionLocationServiceMode, |
| 207 location_services_checkbox_); |
| 208 |
| 176 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 209 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 177 DCHECK(arc_auth_service); | 210 DCHECK(arc_auth_service); |
| 178 | |
| 179 preference_handler_ = base::MakeUnique<arc::ArcOptInPreferenceHandler>( | |
| 180 this, arc_auth_service->profile()->GetPrefs()); | |
| 181 // This automatically updates all preferences. | |
| 182 preference_handler_->Start(); | |
| 183 | |
| 184 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status()); | 211 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status()); |
| 185 } | 212 } |
| 186 | 213 |
| 187 void ArcSupportHost::UnsetMessageHost( | 214 void ArcSupportHost::UnsetMessageHost( |
| 188 arc::ArcSupportMessageHost* message_host) { | 215 arc::ArcSupportMessageHost* message_host) { |
| 189 if (message_host_ != message_host) | 216 if (message_host_ != message_host) |
| 190 return; | 217 return; |
| 191 DisconnectMessageHost(); | 218 DisconnectMessageHost(); |
| 192 } | 219 } |
| 193 | 220 |
| 194 void ArcSupportHost::DisconnectMessageHost() { | 221 void ArcSupportHost::DisconnectMessageHost() { |
| 195 DCHECK(message_host_); | 222 DCHECK(message_host_); |
| 196 preference_handler_.reset(); | |
| 197 display::Screen::GetScreen()->RemoveObserver(this); | 223 display::Screen::GetScreen()->RemoveObserver(this); |
| 198 message_host_->SetObserver(nullptr); | 224 message_host_->SetObserver(nullptr); |
| 199 message_host_ = nullptr; | 225 message_host_ = nullptr; |
| 200 } | 226 } |
| 201 | 227 |
| 202 bool ArcSupportHost::Initialize() { | 228 bool ArcSupportHost::Initialize() { |
| 203 DCHECK(message_host_); | 229 DCHECK(message_host_); |
| 204 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 230 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 205 if (!arc_auth_service->IsAllowed()) | 231 if (!arc_auth_service->IsAllowed()) |
| 206 return false; | 232 return false; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, | 333 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, |
| 308 uint32_t changed_metrics) { | 334 uint32_t changed_metrics) { |
| 309 if (!message_host_) | 335 if (!message_host_) |
| 310 return; | 336 return; |
| 311 | 337 |
| 312 base::DictionaryValue message; | 338 base::DictionaryValue message; |
| 313 message.SetString(kAction, kActionSetWindowBounds); | 339 message.SetString(kAction, kActionSetWindowBounds); |
| 314 message_host_->SendMessage(message); | 340 message_host_->SendMessage(message); |
| 315 } | 341 } |
| 316 | 342 |
| 317 void ArcSupportHost::OnMetricsModeChanged(bool enabled, bool managed) { | |
| 318 SendPreferenceUpdate(kActionSetMetricsMode, enabled, managed); | |
| 319 } | |
| 320 | |
| 321 void ArcSupportHost::OnBackupAndRestoreModeChanged(bool enabled, bool managed) { | |
| 322 SendPreferenceUpdate(kActionBackupAndRestoreMode, enabled, managed); | |
| 323 } | |
| 324 | |
| 325 void ArcSupportHost::OnLocationServicesModeChanged(bool enabled, bool managed) { | |
| 326 SendPreferenceUpdate(kActionLocationServiceMode, enabled, managed); | |
| 327 } | |
| 328 | |
| 329 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, | |
| 330 bool is_enabled, | |
| 331 bool is_managed) { | |
| 332 if (!message_host_) | |
| 333 return; | |
| 334 | |
| 335 base::DictionaryValue message; | |
| 336 message.SetString(kAction, action_name); | |
| 337 message.SetBoolean(kEnabled, is_enabled); | |
| 338 message.SetBoolean(kManaged, is_managed); | |
| 339 message_host_->SendMessage(message); | |
| 340 } | |
| 341 | |
| 342 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) { | 343 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) { |
| 343 std::string event; | 344 std::string event; |
| 344 if (!message.GetString(kEvent, &event)) { | 345 if (!message.GetString(kEvent, &event)) { |
| 345 NOTREACHED(); | 346 NOTREACHED(); |
| 346 return; | 347 return; |
| 347 } | 348 } |
| 348 | 349 |
| 349 // TODO(hidehiko): Replace by Observer. | 350 if (!observer_) { |
| 350 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 351 LOG(ERROR) << "No observer is found."; |
| 351 DCHECK(arc_auth_service); | 352 return; |
| 352 | 353 } |
| 353 DCHECK(preference_handler_); | |
| 354 | 354 |
| 355 if (event == kEventOnWindowClosed) { | 355 if (event == kEventOnWindowClosed) { |
| 356 arc_auth_service->CancelAuthCode(); | 356 observer_->OnWindowClosed(); |
| 357 } else if (event == kEventOnAuthSuccedded) { | 357 } else if (event == kEventOnAuthSucceeded) { |
| 358 std::string code; | 358 std::string code; |
| 359 if (message.GetString(kCode, &code)) { | 359 if (message.GetString(kCode, &code)) { |
| 360 arc_auth_service->SetAuthCodeAndStartArc(code); | 360 observer_->OnAuthSucceeded(code); |
| 361 } else { | 361 } else { |
| 362 NOTREACHED(); | 362 NOTREACHED(); |
| 363 } | 363 } |
| 364 } else if (event == kEventOnAgreed) { | 364 } else if (event == kEventOnAgreed) { |
| 365 bool is_metrics_enabled; | 365 bool is_metrics_enabled; |
| 366 bool is_backup_restore_enabled; | 366 bool is_backup_restore_enabled; |
| 367 bool is_location_service_enabled; | 367 bool is_location_service_enabled; |
| 368 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && | 368 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && |
| 369 message.GetBoolean(kIsBackupRestoreEnabled, | 369 message.GetBoolean(kIsBackupRestoreEnabled, |
| 370 &is_backup_restore_enabled) && | 370 &is_backup_restore_enabled) && |
| 371 message.GetBoolean(kIsLocationServiceEnabled, | 371 message.GetBoolean(kIsLocationServiceEnabled, |
| 372 &is_location_service_enabled)) { | 372 &is_location_service_enabled)) { |
| 373 preference_handler_->EnableMetrics(is_metrics_enabled); | 373 observer_->OnTermsAgreed(is_metrics_enabled, is_backup_restore_enabled, |
| 374 preference_handler_->EnableBackupRestore(is_backup_restore_enabled); | 374 is_location_service_enabled); |
| 375 preference_handler_->EnableLocationService(is_location_service_enabled); | |
| 376 arc_auth_service->StartLso(); | |
| 377 } else { | 375 } else { |
| 378 NOTREACHED(); | 376 NOTREACHED(); |
| 379 } | 377 } |
| 380 } else if (event == kEventOnSendFeedbackClicked) { | 378 } else if (event == kEventOnSendFeedbackClicked) { |
| 381 chrome::OpenFeedbackDialog(nullptr); | 379 observer_->OnSendFeedbackClicked(); |
| 382 } else { | 380 } else { |
| 383 LOG(ERROR) << "Unknown message: " << event; | 381 LOG(ERROR) << "Unknown message: " << event; |
| 384 NOTREACHED(); | 382 NOTREACHED(); |
| 385 } | 383 } |
| 386 } | 384 } |
| OLD | NEW |