| 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" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; | 71 constexpr char kIsMetricsEnabled[] = "isMetricsEnabled"; |
| 72 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; | 72 constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled"; |
| 73 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; | 73 constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled"; |
| 74 | 74 |
| 75 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. | 75 // "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button. |
| 76 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; | 76 constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked"; |
| 77 | 77 |
| 78 } // namespace | 78 } // namespace |
| 79 | 79 |
| 80 // static | 80 // static |
| 81 const char ArcSupportHost::kHostName[] = "com.google.arc_support"; | |
| 82 | |
| 83 // static | |
| 84 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | 81 const char ArcSupportHost::kHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; |
| 85 | 82 |
| 86 // static | 83 // static |
| 87 const char ArcSupportHost::kStorageId[] = "arc_support"; | 84 const char ArcSupportHost::kStorageId[] = "arc_support"; |
| 88 | 85 |
| 89 // static | |
| 90 const char* const ArcSupportHost::kHostOrigin[] = { | |
| 91 "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"}; | |
| 92 | |
| 93 // static | |
| 94 std::unique_ptr<extensions::NativeMessageHost> ArcSupportHost::Create() { | |
| 95 return std::unique_ptr<NativeMessageHost>(new ArcSupportHost()); | |
| 96 } | |
| 97 | |
| 98 ArcSupportHost::ArcSupportHost() { | 86 ArcSupportHost::ArcSupportHost() { |
| 87 // TODO(hidehiko): Get rid of dependency to ArcAuthService. |
| 99 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 88 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 100 DCHECK(arc_auth_service); | 89 DCHECK(arc_auth_service); |
| 101 | 90 |
| 102 if (!arc_auth_service->IsAllowed()) | 91 if (!arc_auth_service->IsAllowed()) |
| 103 return; | 92 return; |
| 104 | 93 |
| 105 arc_auth_service->AddObserver(this); | 94 // TODO(hidehiko): This if statement is only for testing. Clean up this. |
| 106 display::Screen::GetScreen()->AddObserver(this); | 95 if (g_browser_process->local_state()) { |
| 107 | 96 pref_local_change_registrar_.Init(g_browser_process->local_state()); |
| 108 pref_local_change_registrar_.Init(g_browser_process->local_state()); | 97 pref_local_change_registrar_.Add( |
| 109 pref_local_change_registrar_.Add( | 98 metrics::prefs::kMetricsReportingEnabled, |
| 110 metrics::prefs::kMetricsReportingEnabled, | 99 base::Bind(&ArcSupportHost::OnMetricsPreferenceChanged, |
| 111 base::Bind(&ArcSupportHost::OnMetricsPreferenceChanged, | 100 base::Unretained(this))); |
| 112 base::Unretained(this))); | 101 } |
| 113 | 102 |
| 114 pref_change_registrar_.Init(arc_auth_service->profile()->GetPrefs()); | 103 pref_change_registrar_.Init(arc_auth_service->profile()->GetPrefs()); |
| 115 pref_change_registrar_.Add( | 104 pref_change_registrar_.Add( |
| 116 prefs::kArcBackupRestoreEnabled, | 105 prefs::kArcBackupRestoreEnabled, |
| 117 base::Bind(&ArcSupportHost::OnBackupAndRestorePreferenceChanged, | 106 base::Bind(&ArcSupportHost::OnBackupAndRestorePreferenceChanged, |
| 118 base::Unretained(this))); | 107 base::Unretained(this))); |
| 119 pref_change_registrar_.Add( | 108 pref_change_registrar_.Add( |
| 120 prefs::kArcLocationServiceEnabled, | 109 prefs::kArcLocationServiceEnabled, |
| 121 base::Bind(&ArcSupportHost::OnLocationServicePreferenceChanged, | 110 base::Bind(&ArcSupportHost::OnLocationServicePreferenceChanged, |
| 122 base::Unretained(this))); | 111 base::Unretained(this))); |
| 123 } | 112 } |
| 124 | 113 |
| 125 ArcSupportHost::~ArcSupportHost() { | 114 ArcSupportHost::~ArcSupportHost() { |
| 126 display::Screen::GetScreen()->RemoveObserver(this); | 115 if (message_host_) |
| 127 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 116 DisconnectMessageHost(); |
| 128 if (arc_auth_service) | |
| 129 arc_auth_service->RemoveObserver(this); | |
| 130 } | 117 } |
| 131 | 118 |
| 132 void ArcSupportHost::Close() { | 119 void ArcSupportHost::Close() { |
| 133 if (!client_) | 120 if (!message_host_) { |
| 121 VLOG(2) << "ArcSupportHost::Close() is called " |
| 122 << "but message_host_ is not available."; |
| 123 return; |
| 124 } |
| 125 |
| 126 base::DictionaryValue message; |
| 127 message.SetString(kAction, kActionCloseWindow); |
| 128 message_host_->SendMessage(message); |
| 129 |
| 130 // Disconnect immediately, so that onWindowClosed event will not be |
| 131 // delivered to here. |
| 132 DisconnectMessageHost(); |
| 133 } |
| 134 |
| 135 void ArcSupportHost::ShowPage(arc::ArcAuthService::UIPage page, |
| 136 const base::string16& status) { |
| 137 if (!message_host_) { |
| 138 VLOG(2) << "ArcSupportHost::ShowPage() is called " |
| 139 << "but message_host_ is not available."; |
| 140 return; |
| 141 } |
| 142 |
| 143 base::DictionaryValue message; |
| 144 message.SetString(kAction, kActionShowPage); |
| 145 message.SetInteger(kPage, static_cast<int>(page)); |
| 146 message.SetString(kStatus, status); |
| 147 message_host_->SendMessage(message); |
| 148 } |
| 149 |
| 150 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) { |
| 151 if (message_host_ == message_host) |
| 134 return; | 152 return; |
| 135 | 153 |
| 136 close_requested_ = true; | 154 if (message_host_) |
| 137 base::DictionaryValue response; | 155 DisconnectMessageHost(); |
| 138 response.SetString(kAction, kActionCloseWindow); | 156 message_host_ = message_host; |
| 139 std::string response_string; | 157 message_host_->SetObserver(this); |
| 140 base::JSONWriter::Write(response, &response_string); | 158 display::Screen::GetScreen()->AddObserver(this); |
| 141 client_->PostMessageFromNativeHost(response_string); | |
| 142 } | |
| 143 | |
| 144 void ArcSupportHost::Start(Client* client) { | |
| 145 DCHECK(!client_); | |
| 146 client_ = client; | |
| 147 | 159 |
| 148 if (!Initialize()) { | 160 if (!Initialize()) { |
| 149 Close(); | 161 Close(); |
| 150 return; | 162 return; |
| 151 } | 163 } |
| 152 | 164 |
| 153 SendMetricsMode(); | 165 SendMetricsMode(); |
| 154 SendBackupAndRestoreMode(); | 166 SendBackupAndRestoreMode(); |
| 155 SendLocationServicesMode(); | 167 SendLocationServicesMode(); |
| 156 | 168 |
| 157 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 169 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 158 DCHECK(arc_auth_service); | 170 DCHECK(arc_auth_service); |
| 159 OnOptInUIShowPage(arc_auth_service->ui_page(), | 171 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status()); |
| 160 arc_auth_service->ui_page_status()); | 172 } |
| 173 |
| 174 void ArcSupportHost::UnsetMessageHost( |
| 175 arc::ArcSupportMessageHost* message_host) { |
| 176 if (message_host_ != message_host) |
| 177 return; |
| 178 DisconnectMessageHost(); |
| 179 } |
| 180 |
| 181 void ArcSupportHost::DisconnectMessageHost() { |
| 182 DCHECK(message_host_); |
| 183 display::Screen::GetScreen()->RemoveObserver(this); |
| 184 message_host_->SetObserver(nullptr); |
| 185 message_host_ = nullptr; |
| 161 } | 186 } |
| 162 | 187 |
| 163 bool ArcSupportHost::Initialize() { | 188 bool ArcSupportHost::Initialize() { |
| 164 DCHECK(client_); | 189 DCHECK(message_host_); |
| 165 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 190 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 166 if (!arc_auth_service->IsAllowed()) | 191 if (!arc_auth_service->IsAllowed()) |
| 167 return false; | 192 return false; |
| 168 | 193 |
| 169 std::unique_ptr<base::DictionaryValue> loadtime_data( | 194 std::unique_ptr<base::DictionaryValue> loadtime_data( |
| 170 new base::DictionaryValue()); | 195 new base::DictionaryValue()); |
| 171 base::string16 device_name = ash::GetChromeOSDeviceName(); | 196 base::string16 device_name = ash::GetChromeOSDeviceName(); |
| 172 loadtime_data->SetString( | 197 loadtime_data->SetString( |
| 173 "greetingHeader", | 198 "greetingHeader", |
| 174 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name)); | 199 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 271 |
| 247 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get()); | 272 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get()); |
| 248 DCHECK(arc_auth_service); | 273 DCHECK(arc_auth_service); |
| 249 const std::string device_id = user_manager::known_user::GetDeviceId( | 274 const std::string device_id = user_manager::known_user::GetDeviceId( |
| 250 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile())); | 275 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile())); |
| 251 DCHECK(!device_id.empty()); | 276 DCHECK(!device_id.empty()); |
| 252 loadtime_data->SetBoolean( | 277 loadtime_data->SetBoolean( |
| 253 "isOwnerProfile", | 278 "isOwnerProfile", |
| 254 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile())); | 279 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile())); |
| 255 | 280 |
| 256 base::DictionaryValue request; | 281 base::DictionaryValue message; |
| 257 std::string request_string; | 282 message.SetString(kAction, kActionInitialize); |
| 258 request.SetString(kAction, kActionInitialize); | 283 message.Set(kData, std::move(loadtime_data)); |
| 259 request.Set(kData, std::move(loadtime_data)); | 284 message.SetString(kDeviceId, device_id); |
| 260 request.SetString(kDeviceId, device_id); | 285 message_host_->SendMessage(message); |
| 261 base::JSONWriter::Write(request, &request_string); | |
| 262 client_->PostMessageFromNativeHost(request_string); | |
| 263 | |
| 264 return true; | 286 return true; |
| 265 } | 287 } |
| 266 | 288 |
| 267 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {} | 289 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {} |
| 268 | 290 |
| 269 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {} | 291 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {} |
| 270 | 292 |
| 271 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, | 293 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, |
| 272 uint32_t changed_metrics) { | 294 uint32_t changed_metrics) { |
| 273 base::DictionaryValue request; | 295 if (!message_host_) |
| 274 std::string request_string; | 296 return; |
| 275 request.SetString(kAction, kActionSetWindowBounds); | 297 |
| 276 base::JSONWriter::Write(request, &request_string); | 298 base::DictionaryValue message; |
| 277 client_->PostMessageFromNativeHost(request_string); | 299 message.SetString(kAction, kActionSetWindowBounds); |
| 300 message_host_->SendMessage(message); |
| 278 } | 301 } |
| 279 | 302 |
| 280 void ArcSupportHost::OnMetricsPreferenceChanged() { | 303 void ArcSupportHost::OnMetricsPreferenceChanged() { |
| 281 SendMetricsMode(); | 304 SendMetricsMode(); |
| 282 } | 305 } |
| 283 | 306 |
| 284 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() { | 307 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() { |
| 285 SendBackupAndRestoreMode(); | 308 SendBackupAndRestoreMode(); |
| 286 } | 309 } |
| 287 | 310 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 310 DCHECK(arc_auth_service); | 333 DCHECK(arc_auth_service); |
| 311 SendPreferenceUpdate( | 334 SendPreferenceUpdate( |
| 312 action_name, | 335 action_name, |
| 313 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name), | 336 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name), |
| 314 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name)); | 337 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name)); |
| 315 } | 338 } |
| 316 | 339 |
| 317 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, | 340 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, |
| 318 bool is_enabled, | 341 bool is_enabled, |
| 319 bool is_managed) { | 342 bool is_managed) { |
| 320 base::DictionaryValue request; | 343 if (!message_host_) |
| 321 std::string request_string; | |
| 322 request.SetString(kAction, action_name); | |
| 323 request.SetBoolean(kEnabled, is_enabled); | |
| 324 request.SetBoolean(kManaged, is_managed); | |
| 325 base::JSONWriter::Write(request, &request_string); | |
| 326 client_->PostMessageFromNativeHost(request_string); | |
| 327 } | |
| 328 | |
| 329 void ArcSupportHost::OnOptInUIClose() { | |
| 330 Close(); | |
| 331 } | |
| 332 | |
| 333 void ArcSupportHost::OnOptInUIShowPage(arc::ArcAuthService::UIPage page, | |
| 334 const base::string16& status) { | |
| 335 if (!client_) | |
| 336 return; | 344 return; |
| 337 | 345 |
| 338 base::DictionaryValue response; | 346 base::DictionaryValue message; |
| 339 response.SetString(kAction, kActionShowPage); | 347 message.SetString(kAction, action_name); |
| 340 response.SetInteger(kPage, static_cast<int>(page)); | 348 message.SetBoolean(kEnabled, is_enabled); |
| 341 response.SetString(kStatus, status); | 349 message.SetBoolean(kManaged, is_managed); |
| 342 std::string response_string; | 350 message_host_->SendMessage(message); |
| 343 base::JSONWriter::Write(response, &response_string); | |
| 344 client_->PostMessageFromNativeHost(response_string); | |
| 345 } | 351 } |
| 346 | 352 |
| 347 void ArcSupportHost::EnableMetrics(bool is_enabled) { | 353 void ArcSupportHost::EnableMetrics(bool is_enabled) { |
| 348 ChangeMetricsReportingState(is_enabled); | 354 ChangeMetricsReportingState(is_enabled); |
| 349 } | 355 } |
| 350 | 356 |
| 351 void ArcSupportHost::EnableBackupRestore(bool is_enabled) { | 357 void ArcSupportHost::EnableBackupRestore(bool is_enabled) { |
| 352 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 358 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 353 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); | 359 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); |
| 354 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); | 360 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); |
| 355 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled); | 361 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled); |
| 356 } | 362 } |
| 357 | 363 |
| 358 void ArcSupportHost::EnableLocationService(bool is_enabled) { | 364 void ArcSupportHost::EnableLocationService(bool is_enabled) { |
| 359 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 365 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 360 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); | 366 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); |
| 361 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); | 367 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); |
| 362 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled); | 368 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled); |
| 363 } | 369 } |
| 364 | 370 |
| 365 void ArcSupportHost::OnMessage(const std::string& message_string) { | 371 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) { |
| 366 std::unique_ptr<base::Value> message_value = | 372 std::string event; |
| 367 base::JSONReader::Read(message_string); | 373 if (!message.GetString(kEvent, &event)) { |
| 368 base::DictionaryValue* message; | |
| 369 if (!message_value || !message_value->GetAsDictionary(&message)) { | |
| 370 NOTREACHED(); | 374 NOTREACHED(); |
| 371 return; | 375 return; |
| 372 } | 376 } |
| 373 | |
| 374 std::string event; | |
| 375 if (!message->GetString(kEvent, &event)) { | |
| 376 NOTREACHED(); | |
| 377 return; | |
| 378 } | |
| 379 | 377 |
| 380 // TODO(hidehiko): Replace by Observer. | 378 // TODO(hidehiko): Replace by Observer. |
| 381 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | 379 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); |
| 382 DCHECK(arc_auth_service); | 380 DCHECK(arc_auth_service); |
| 383 | 381 |
| 384 if (event == kEventOnWindowClosed) { | 382 if (event == kEventOnWindowClosed) { |
| 385 if (!close_requested_) | 383 arc_auth_service->CancelAuthCode(); |
| 386 arc_auth_service->CancelAuthCode(); | |
| 387 } else if (event == kEventOnAuthSuccedded) { | 384 } else if (event == kEventOnAuthSuccedded) { |
| 388 std::string code; | 385 std::string code; |
| 389 if (message->GetString(kCode, &code)) { | 386 if (message.GetString(kCode, &code)) { |
| 390 arc_auth_service->SetAuthCodeAndStartArc(code); | 387 arc_auth_service->SetAuthCodeAndStartArc(code); |
| 391 } else { | 388 } else { |
| 392 NOTREACHED(); | 389 NOTREACHED(); |
| 393 } | 390 } |
| 394 } else if (event == kEventOnAgreed) { | 391 } else if (event == kEventOnAgreed) { |
| 395 bool is_metrics_enabled; | 392 bool is_metrics_enabled; |
| 396 bool is_backup_restore_enabled; | 393 bool is_backup_restore_enabled; |
| 397 bool is_location_service_enabled; | 394 bool is_location_service_enabled; |
| 398 if (message->GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && | 395 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && |
| 399 message->GetBoolean(kIsBackupRestoreEnabled, | 396 message.GetBoolean(kIsBackupRestoreEnabled, |
| 400 &is_backup_restore_enabled) && | 397 &is_backup_restore_enabled) && |
| 401 message->GetBoolean(kIsLocationServiceEnabled, | 398 message.GetBoolean(kIsLocationServiceEnabled, |
| 402 &is_location_service_enabled)) { | 399 &is_location_service_enabled)) { |
| 403 EnableMetrics(is_metrics_enabled); | 400 EnableMetrics(is_metrics_enabled); |
| 404 EnableBackupRestore(is_backup_restore_enabled); | 401 EnableBackupRestore(is_backup_restore_enabled); |
| 405 EnableLocationService(is_location_service_enabled); | 402 EnableLocationService(is_location_service_enabled); |
| 406 arc_auth_service->StartLso(); | 403 arc_auth_service->StartLso(); |
| 407 } else { | 404 } else { |
| 408 NOTREACHED(); | 405 NOTREACHED(); |
| 409 } | 406 } |
| 410 } else if (event == kEventOnSendFeedbackClicked) { | 407 } else if (event == kEventOnSendFeedbackClicked) { |
| 411 chrome::OpenFeedbackDialog(nullptr); | 408 chrome::OpenFeedbackDialog(nullptr); |
| 412 } else { | 409 } else { |
| 413 LOG(ERROR) << "Unknown message: " << message_string; | 410 LOG(ERROR) << "Unknown message: " << event; |
| 414 NOTREACHED(); | 411 NOTREACHED(); |
| 415 } | 412 } |
| 416 } | 413 } |
| 417 | |
| 418 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportHost::task_runner() | |
| 419 const { | |
| 420 return base::ThreadTaskRunnerHandle::Get(); | |
| 421 } | |
| OLD | NEW |