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

Side by Side Diff: chrome/browser/chromeos/arc/arc_support_host.cc

Issue 2436903003: Extract ArcSupportMessageHost. (Closed)
Patch Set: Address comments. Created 4 years, 2 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 "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
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_)
134 return; 121 return;
135 122
136 close_requested_ = true; 123 base::DictionaryValue message;
137 base::DictionaryValue response; 124 message.SetString(kAction, kActionCloseWindow);
138 response.SetString(kAction, kActionCloseWindow); 125 message_host_->SendMessage(message);
139 std::string response_string; 126
140 base::JSONWriter::Write(response, &response_string); 127 // Disconnect immediately, so that onWindowClosed event will not be
141 client_->PostMessageFromNativeHost(response_string); 128 // delivered to here.
129 DisconnectMessageHost();
142 } 130 }
143 131
144 void ArcSupportHost::Start(Client* client) { 132 void ArcSupportHost::ShowPage(arc::ArcAuthService::UIPage page,
145 DCHECK(!client_); 133 const base::string16& status) {
146 client_ = client; 134 if (!message_host_)
135 return;
136
137 base::DictionaryValue message;
138 message.SetString(kAction, kActionShowPage);
139 message.SetInteger(kPage, static_cast<int>(page));
140 message.SetString(kStatus, status);
141 message_host_->SendMessage(message);
142 }
143
144 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) {
145 if (message_host_ == message_host)
146 return;
147
148 if (message_host_)
149 DisconnectMessageHost();
150 message_host_ = message_host;
151 message_host_->SetObserver(this);
152 display::Screen::GetScreen()->AddObserver(this);
147 153
148 if (!Initialize()) { 154 if (!Initialize()) {
149 Close(); 155 Close();
150 return; 156 return;
151 } 157 }
152 158
153 SendMetricsMode(); 159 SendMetricsMode();
154 SendBackupAndRestoreMode(); 160 SendBackupAndRestoreMode();
155 SendLocationServicesMode(); 161 SendLocationServicesMode();
156 162
157 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 163 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
158 DCHECK(arc_auth_service); 164 DCHECK(arc_auth_service);
159 OnOptInUIShowPage(arc_auth_service->ui_page(), 165 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status());
160 arc_auth_service->ui_page_status()); 166 }
167
168 void ArcSupportHost::UnsetMessageHost(
169 arc::ArcSupportMessageHost* message_host) {
170 if (message_host_ != message_host)
171 return;
172 DisconnectMessageHost();
173 }
174
175 void ArcSupportHost::DisconnectMessageHost() {
176 DCHECK(message_host_);
177 display::Screen::GetScreen()->RemoveObserver(this);
178 message_host_->SetObserver(nullptr);
179 message_host_ = nullptr;
161 } 180 }
162 181
163 bool ArcSupportHost::Initialize() { 182 bool ArcSupportHost::Initialize() {
164 DCHECK(client_); 183 DCHECK(message_host_);
165 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 184 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
166 if (!arc_auth_service->IsAllowed()) 185 if (!arc_auth_service->IsAllowed())
167 return false; 186 return false;
168 187
169 std::unique_ptr<base::DictionaryValue> loadtime_data( 188 std::unique_ptr<base::DictionaryValue> loadtime_data(
170 new base::DictionaryValue()); 189 new base::DictionaryValue());
171 base::string16 device_name = ash::GetChromeOSDeviceName(); 190 base::string16 device_name = ash::GetChromeOSDeviceName();
172 loadtime_data->SetString( 191 loadtime_data->SetString(
173 "greetingHeader", 192 "greetingHeader",
174 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name)); 193 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 265
247 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get()); 266 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get());
248 DCHECK(arc_auth_service); 267 DCHECK(arc_auth_service);
249 const std::string device_id = user_manager::known_user::GetDeviceId( 268 const std::string device_id = user_manager::known_user::GetDeviceId(
250 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile())); 269 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile()));
251 DCHECK(!device_id.empty()); 270 DCHECK(!device_id.empty());
252 loadtime_data->SetBoolean( 271 loadtime_data->SetBoolean(
253 "isOwnerProfile", 272 "isOwnerProfile",
254 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile())); 273 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile()));
255 274
256 base::DictionaryValue request; 275 base::DictionaryValue message;
257 std::string request_string; 276 message.SetString(kAction, kActionInitialize);
258 request.SetString(kAction, kActionInitialize); 277 message.Set(kData, std::move(loadtime_data));
259 request.Set(kData, std::move(loadtime_data)); 278 message.SetString(kDeviceId, device_id);
260 request.SetString(kDeviceId, device_id); 279 message_host_->SendMessage(message);
261 base::JSONWriter::Write(request, &request_string);
262 client_->PostMessageFromNativeHost(request_string);
263
264 return true; 280 return true;
265 } 281 }
266 282
267 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {} 283 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {}
268 284
269 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {} 285 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {}
270 286
271 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, 287 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display,
272 uint32_t changed_metrics) { 288 uint32_t changed_metrics) {
273 base::DictionaryValue request; 289 if (!message_host_)
274 std::string request_string; 290 return;
275 request.SetString(kAction, kActionSetWindowBounds); 291
276 base::JSONWriter::Write(request, &request_string); 292 base::DictionaryValue message;
277 client_->PostMessageFromNativeHost(request_string); 293 message.SetString(kAction, kActionSetWindowBounds);
294 message_host_->SendMessage(message);
278 } 295 }
279 296
280 void ArcSupportHost::OnMetricsPreferenceChanged() { 297 void ArcSupportHost::OnMetricsPreferenceChanged() {
281 SendMetricsMode(); 298 SendMetricsMode();
282 } 299 }
283 300
284 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() { 301 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() {
285 SendBackupAndRestoreMode(); 302 SendBackupAndRestoreMode();
286 } 303 }
287 304
(...skipping 22 matching lines...) Expand all
310 DCHECK(arc_auth_service); 327 DCHECK(arc_auth_service);
311 SendPreferenceUpdate( 328 SendPreferenceUpdate(
312 action_name, 329 action_name,
313 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name), 330 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name),
314 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name)); 331 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name));
315 } 332 }
316 333
317 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, 334 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name,
318 bool is_enabled, 335 bool is_enabled,
319 bool is_managed) { 336 bool is_managed) {
320 base::DictionaryValue request; 337 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; 338 return;
337 339
338 base::DictionaryValue response; 340 base::DictionaryValue message;
339 response.SetString(kAction, kActionShowPage); 341 message.SetString(kAction, action_name);
340 response.SetInteger(kPage, static_cast<int>(page)); 342 message.SetBoolean(kEnabled, is_enabled);
341 response.SetString(kStatus, status); 343 message.SetBoolean(kManaged, is_managed);
342 std::string response_string; 344 message_host_->SendMessage(message);
343 base::JSONWriter::Write(response, &response_string);
344 client_->PostMessageFromNativeHost(response_string);
345 } 345 }
346 346
347 void ArcSupportHost::EnableMetrics(bool is_enabled) { 347 void ArcSupportHost::EnableMetrics(bool is_enabled) {
348 ChangeMetricsReportingState(is_enabled); 348 ChangeMetricsReportingState(is_enabled);
349 } 349 }
350 350
351 void ArcSupportHost::EnableBackupRestore(bool is_enabled) { 351 void ArcSupportHost::EnableBackupRestore(bool is_enabled) {
352 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 352 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
353 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); 353 DCHECK(arc_auth_service && arc_auth_service->IsAllowed());
354 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); 354 PrefService* pref_service = arc_auth_service->profile()->GetPrefs();
355 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled); 355 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled);
356 } 356 }
357 357
358 void ArcSupportHost::EnableLocationService(bool is_enabled) { 358 void ArcSupportHost::EnableLocationService(bool is_enabled) {
359 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 359 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
360 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); 360 DCHECK(arc_auth_service && arc_auth_service->IsAllowed());
361 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); 361 PrefService* pref_service = arc_auth_service->profile()->GetPrefs();
362 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled); 362 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled);
363 } 363 }
364 364
365 void ArcSupportHost::OnMessage(const std::string& message_string) { 365 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) {
366 std::unique_ptr<base::Value> message_value = 366 std::string event;
367 base::JSONReader::Read(message_string); 367 if (!message.GetString(kEvent, &event)) {
368 base::DictionaryValue* message;
369 if (!message_value || !message_value->GetAsDictionary(&message)) {
370 NOTREACHED(); 368 NOTREACHED();
371 return; 369 return;
372 } 370 }
373
374 std::string event;
375 if (!message->GetString(kEvent, &event)) {
376 NOTREACHED();
377 return;
378 }
379 371
380 // TODO(hidehiko): Replace by Observer. 372 // TODO(hidehiko): Replace by Observer.
381 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 373 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
382 DCHECK(arc_auth_service); 374 DCHECK(arc_auth_service);
383 375
384 if (event == kEventOnWindowClosed) { 376 if (event == kEventOnWindowClosed) {
385 if (!close_requested_) 377 arc_auth_service->CancelAuthCode();
386 arc_auth_service->CancelAuthCode();
387 } else if (event == kEventOnAuthSuccedded) { 378 } else if (event == kEventOnAuthSuccedded) {
388 std::string code; 379 std::string code;
389 if (message->GetString(kCode, &code)) { 380 if (message.GetString(kCode, &code)) {
390 arc_auth_service->SetAuthCodeAndStartArc(code); 381 arc_auth_service->SetAuthCodeAndStartArc(code);
391 } else { 382 } else {
392 NOTREACHED(); 383 NOTREACHED();
393 } 384 }
394 } else if (event == kEventOnAgreed) { 385 } else if (event == kEventOnAgreed) {
395 bool is_metrics_enabled; 386 bool is_metrics_enabled;
396 bool is_backup_restore_enabled; 387 bool is_backup_restore_enabled;
397 bool is_location_service_enabled; 388 bool is_location_service_enabled;
398 if (message->GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && 389 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) &&
399 message->GetBoolean(kIsBackupRestoreEnabled, 390 message.GetBoolean(kIsBackupRestoreEnabled,
400 &is_backup_restore_enabled) && 391 &is_backup_restore_enabled) &&
401 message->GetBoolean(kIsLocationServiceEnabled, 392 message.GetBoolean(kIsLocationServiceEnabled,
402 &is_location_service_enabled)) { 393 &is_location_service_enabled)) {
403 EnableMetrics(is_metrics_enabled); 394 EnableMetrics(is_metrics_enabled);
404 EnableBackupRestore(is_backup_restore_enabled); 395 EnableBackupRestore(is_backup_restore_enabled);
405 EnableLocationService(is_location_service_enabled); 396 EnableLocationService(is_location_service_enabled);
406 arc_auth_service->StartLso(); 397 arc_auth_service->StartLso();
407 } else { 398 } else {
408 NOTREACHED(); 399 NOTREACHED();
409 } 400 }
410 } else if (event == kEventOnSendFeedbackClicked) { 401 } else if (event == kEventOnSendFeedbackClicked) {
411 chrome::OpenFeedbackDialog(nullptr); 402 chrome::OpenFeedbackDialog(nullptr);
412 } else { 403 } else {
413 LOG(ERROR) << "Unknown message: " << message_string; 404 LOG(ERROR) << "Unknown message: " << event;
414 NOTREACHED(); 405 NOTREACHED();
415 } 406 }
416 } 407 }
417
418 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportHost::task_runner()
419 const {
420 return base::ThreadTaskRunnerHandle::Get();
421 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698