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

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

Issue 2436903003: Extract ArcSupportMessageHost. (Closed)
Patch Set: Fix tests 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 if (g_browser_process->local_state()) {
hidehiko 2016/10/20 18:36:17 Note: No service is running on unittest, if stmt i
Luis Héctor Chávez 2016/10/21 02:24:08 Can you add a TODO?
hidehiko 2016/10/21 07:40:27 Done.
106 display::Screen::GetScreen()->AddObserver(this); 95 pref_local_change_registrar_.Init(g_browser_process->local_state());
107 96 pref_local_change_registrar_.Add(
108 pref_local_change_registrar_.Init(g_browser_process->local_state()); 97 metrics::prefs::kMetricsReportingEnabled,
109 pref_local_change_registrar_.Add( 98 base::Bind(&ArcSupportHost::OnMetricsPreferenceChanged,
110 metrics::prefs::kMetricsReportingEnabled, 99 base::Unretained(this)));
111 base::Bind(&ArcSupportHost::OnMetricsPreferenceChanged, 100 }
112 base::Unretained(this)));
113 101
114 pref_change_registrar_.Init(arc_auth_service->profile()->GetPrefs()); 102 pref_change_registrar_.Init(arc_auth_service->profile()->GetPrefs());
115 pref_change_registrar_.Add( 103 pref_change_registrar_.Add(
116 prefs::kArcBackupRestoreEnabled, 104 prefs::kArcBackupRestoreEnabled,
117 base::Bind(&ArcSupportHost::OnBackupAndRestorePreferenceChanged, 105 base::Bind(&ArcSupportHost::OnBackupAndRestorePreferenceChanged,
118 base::Unretained(this))); 106 base::Unretained(this)));
119 pref_change_registrar_.Add( 107 pref_change_registrar_.Add(
120 prefs::kArcLocationServiceEnabled, 108 prefs::kArcLocationServiceEnabled,
121 base::Bind(&ArcSupportHost::OnLocationServicePreferenceChanged, 109 base::Bind(&ArcSupportHost::OnLocationServicePreferenceChanged,
122 base::Unretained(this))); 110 base::Unretained(this)));
123 } 111 }
124 112
125 ArcSupportHost::~ArcSupportHost() { 113 ArcSupportHost::~ArcSupportHost() {
126 display::Screen::GetScreen()->RemoveObserver(this); 114 if (message_host_)
127 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 115 DisconnectMessageHost();
128 if (arc_auth_service)
129 arc_auth_service->RemoveObserver(this);
130 } 116 }
131 117
132 void ArcSupportHost::Close() { 118 void ArcSupportHost::Close() {
133 if (!client_) 119 if (!message_host_)
134 return; 120 return;
135 121
136 close_requested_ = true; 122 base::DictionaryValue message;
137 base::DictionaryValue response; 123 message.SetString(kAction, kActionCloseWindow);
138 response.SetString(kAction, kActionCloseWindow); 124 message_host_->SendMessage(message);
139 std::string response_string; 125
140 base::JSONWriter::Write(response, &response_string); 126 // Disconnect immediately, so that onWindowClosed event will not be
141 client_->PostMessageFromNativeHost(response_string); 127 // delivered to here.
Luis Héctor Chávez 2016/10/21 02:24:08 nit: s/delivered to here/delivered here/
128 DisconnectMessageHost();
142 } 129 }
143 130
144 void ArcSupportHost::Start(Client* client) { 131 void ArcSupportHost::ShowPage(arc::ArcAuthService::UIPage page,
145 DCHECK(!client_); 132 const base::string16& status) {
146 client_ = client; 133 if (!message_host_)
134 return;
khmel 2016/10/21 03:34:49 IIUC this should never happen. May be LOG(ERROR)?
hidehiko 2016/10/21 07:40:27 This happens. Because ArcSupportMessageHost creati
khmel 2016/10/21 14:11:17 I have concern about losing data for user (not sho
hidehiko 2016/10/24 06:03:26 Currently, no data loss is ensured in the ArcAuthS
khmel 2016/10/24 15:05:36 Acknowledged.
135
136 base::DictionaryValue message;
137 message.SetString(kAction, kActionShowPage);
138 message.SetInteger(kPage, static_cast<int>(page));
139 message.SetString(kStatus, status);
140 message_host_->SendMessage(message);
141 }
142
143 void ArcSupportHost::SetMessageHost(arc::ArcSupportMessageHost* message_host) {
Luis Héctor Chávez 2016/10/21 02:24:07 if (message_host_ == message_host) return; for
hidehiko 2016/10/21 07:40:27 Done.
144 if (message_host_)
145 DisconnectMessageHost();
146 message_host_ = message_host;
147 message_host_->SetObserver(this);
148 display::Screen::GetScreen()->AddObserver(this);
147 149
148 if (!Initialize()) { 150 if (!Initialize()) {
149 Close(); 151 Close();
150 return; 152 return;
151 } 153 }
152 154
153 SendMetricsMode(); 155 SendMetricsMode();
154 SendBackupAndRestoreMode(); 156 SendBackupAndRestoreMode();
155 SendLocationServicesMode(); 157 SendLocationServicesMode();
156 158
157 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 159 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
158 DCHECK(arc_auth_service); 160 DCHECK(arc_auth_service);
159 OnOptInUIShowPage(arc_auth_service->ui_page(), 161 ShowPage(arc_auth_service->ui_page(), arc_auth_service->ui_page_status());
160 arc_auth_service->ui_page_status()); 162 }
163
164 void ArcSupportHost::UnsetMessageHost(
165 arc::ArcSupportMessageHost* message_host) {
166 if (message_host_ != message_host)
167 return;
168 DisconnectMessageHost();
169 }
170
171 void ArcSupportHost::DisconnectMessageHost() {
172 display::Screen::GetScreen()->RemoveObserver(this);
Luis Héctor Chávez 2016/10/21 02:24:07 DCHECK(message_host_); ?
hidehiko 2016/10/21 07:40:27 Done.
173 message_host_->SetObserver(nullptr);
174 message_host_ = nullptr;
161 } 175 }
162 176
163 bool ArcSupportHost::Initialize() { 177 bool ArcSupportHost::Initialize() {
164 DCHECK(client_); 178 DCHECK(message_host_);
165 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 179 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
166 if (!arc_auth_service->IsAllowed()) 180 if (!arc_auth_service->IsAllowed())
167 return false; 181 return false;
168 182
169 std::unique_ptr<base::DictionaryValue> loadtime_data( 183 std::unique_ptr<base::DictionaryValue> loadtime_data(
170 new base::DictionaryValue()); 184 new base::DictionaryValue());
171 base::string16 device_name = ash::GetChromeOSDeviceName(); 185 base::string16 device_name = ash::GetChromeOSDeviceName();
172 loadtime_data->SetString( 186 loadtime_data->SetString(
173 "greetingHeader", 187 "greetingHeader",
174 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name)); 188 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 260
247 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get()); 261 webui::SetLoadTimeDataDefaults(app_locale, loadtime_data.get());
248 DCHECK(arc_auth_service); 262 DCHECK(arc_auth_service);
249 const std::string device_id = user_manager::known_user::GetDeviceId( 263 const std::string device_id = user_manager::known_user::GetDeviceId(
250 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile())); 264 multi_user_util::GetAccountIdFromProfile(arc_auth_service->profile()));
251 DCHECK(!device_id.empty()); 265 DCHECK(!device_id.empty());
252 loadtime_data->SetBoolean( 266 loadtime_data->SetBoolean(
253 "isOwnerProfile", 267 "isOwnerProfile",
254 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile())); 268 chromeos::ProfileHelper::IsOwnerProfile(arc_auth_service->profile()));
255 269
256 base::DictionaryValue request; 270 base::DictionaryValue message;
257 std::string request_string; 271 message.SetString(kAction, kActionInitialize);
258 request.SetString(kAction, kActionInitialize); 272 message.Set(kData, std::move(loadtime_data));
259 request.Set(kData, std::move(loadtime_data)); 273 message.SetString(kDeviceId, device_id);
260 request.SetString(kDeviceId, device_id); 274 message_host_->SendMessage(message);
261 base::JSONWriter::Write(request, &request_string);
262 client_->PostMessageFromNativeHost(request_string);
263
264 return true; 275 return true;
265 } 276 }
266 277
267 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {} 278 void ArcSupportHost::OnDisplayAdded(const display::Display& new_display) {}
268 279
269 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {} 280 void ArcSupportHost::OnDisplayRemoved(const display::Display& old_display) {}
270 281
271 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display, 282 void ArcSupportHost::OnDisplayMetricsChanged(const display::Display& display,
272 uint32_t changed_metrics) { 283 uint32_t changed_metrics) {
273 base::DictionaryValue request; 284 if (!message_host_)
274 std::string request_string; 285 return;
275 request.SetString(kAction, kActionSetWindowBounds); 286
276 base::JSONWriter::Write(request, &request_string); 287 base::DictionaryValue message;
277 client_->PostMessageFromNativeHost(request_string); 288 message.SetString(kAction, kActionSetWindowBounds);
289 message_host_->SendMessage(message);
278 } 290 }
279 291
280 void ArcSupportHost::OnMetricsPreferenceChanged() { 292 void ArcSupportHost::OnMetricsPreferenceChanged() {
281 SendMetricsMode(); 293 SendMetricsMode();
282 } 294 }
283 295
284 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() { 296 void ArcSupportHost::OnBackupAndRestorePreferenceChanged() {
285 SendBackupAndRestoreMode(); 297 SendBackupAndRestoreMode();
286 } 298 }
287 299
(...skipping 22 matching lines...) Expand all
310 DCHECK(arc_auth_service); 322 DCHECK(arc_auth_service);
311 SendPreferenceUpdate( 323 SendPreferenceUpdate(
312 action_name, 324 action_name,
313 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name), 325 arc_auth_service->profile()->GetPrefs()->GetBoolean(pref_name),
314 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name)); 326 arc_auth_service->profile()->GetPrefs()->IsManagedPreference(pref_name));
315 } 327 }
316 328
317 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name, 329 void ArcSupportHost::SendPreferenceUpdate(const std::string& action_name,
318 bool is_enabled, 330 bool is_enabled,
319 bool is_managed) { 331 bool is_managed) {
320 base::DictionaryValue request; 332 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; 333 return;
337 334
338 base::DictionaryValue response; 335 base::DictionaryValue message;
339 response.SetString(kAction, kActionShowPage); 336 message.SetString(kAction, action_name);
340 response.SetInteger(kPage, static_cast<int>(page)); 337 message.SetBoolean(kEnabled, is_enabled);
341 response.SetString(kStatus, status); 338 message.SetBoolean(kManaged, is_managed);
342 std::string response_string; 339 message_host_->SendMessage(message);
343 base::JSONWriter::Write(response, &response_string);
344 client_->PostMessageFromNativeHost(response_string);
345 } 340 }
346 341
347 void ArcSupportHost::EnableMetrics(bool is_enabled) { 342 void ArcSupportHost::EnableMetrics(bool is_enabled) {
348 ChangeMetricsReportingState(is_enabled); 343 ChangeMetricsReportingState(is_enabled);
349 } 344 }
350 345
351 void ArcSupportHost::EnableBackupRestore(bool is_enabled) { 346 void ArcSupportHost::EnableBackupRestore(bool is_enabled) {
352 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 347 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
353 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); 348 DCHECK(arc_auth_service && arc_auth_service->IsAllowed());
354 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); 349 PrefService* pref_service = arc_auth_service->profile()->GetPrefs();
355 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled); 350 pref_service->SetBoolean(prefs::kArcBackupRestoreEnabled, is_enabled);
356 } 351 }
357 352
358 void ArcSupportHost::EnableLocationService(bool is_enabled) { 353 void ArcSupportHost::EnableLocationService(bool is_enabled) {
359 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 354 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
360 DCHECK(arc_auth_service && arc_auth_service->IsAllowed()); 355 DCHECK(arc_auth_service && arc_auth_service->IsAllowed());
361 PrefService* pref_service = arc_auth_service->profile()->GetPrefs(); 356 PrefService* pref_service = arc_auth_service->profile()->GetPrefs();
362 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled); 357 pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled);
363 } 358 }
364 359
365 void ArcSupportHost::OnMessage(const std::string& message_string) { 360 void ArcSupportHost::OnMessage(const base::DictionaryValue& message) {
366 std::unique_ptr<base::Value> message_value = 361 std::string event;
367 base::JSONReader::Read(message_string); 362 if (!message.GetString(kEvent, &event)) {
368 base::DictionaryValue* message;
369 if (!message_value || !message_value->GetAsDictionary(&message)) {
370 NOTREACHED(); 363 NOTREACHED();
371 return; 364 return;
372 } 365 }
373
374 std::string event;
375 if (!message->GetString(kEvent, &event)) {
376 NOTREACHED();
377 return;
378 }
379 366
380 // TODO(hidehiko): Replace by Observer. 367 // TODO(hidehiko): Replace by Observer.
381 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); 368 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
382 DCHECK(arc_auth_service); 369 DCHECK(arc_auth_service);
383 370
384 if (event == kEventOnWindowClosed) { 371 if (event == kEventOnWindowClosed) {
385 if (!close_requested_) 372 arc_auth_service->CancelAuthCode();
386 arc_auth_service->CancelAuthCode();
387 } else if (event == kEventOnAuthSuccedded) { 373 } else if (event == kEventOnAuthSuccedded) {
388 std::string code; 374 std::string code;
389 if (message->GetString(kCode, &code)) { 375 if (message.GetString(kCode, &code)) {
390 arc_auth_service->SetAuthCodeAndStartArc(code); 376 arc_auth_service->SetAuthCodeAndStartArc(code);
391 } else { 377 } else {
392 NOTREACHED(); 378 NOTREACHED();
393 } 379 }
394 } else if (event == kEventOnAgreed) { 380 } else if (event == kEventOnAgreed) {
395 bool is_metrics_enabled; 381 bool is_metrics_enabled;
396 bool is_backup_restore_enabled; 382 bool is_backup_restore_enabled;
397 bool is_location_service_enabled; 383 bool is_location_service_enabled;
398 if (message->GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) && 384 if (message.GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) &&
399 message->GetBoolean(kIsBackupRestoreEnabled, 385 message.GetBoolean(kIsBackupRestoreEnabled,
400 &is_backup_restore_enabled) && 386 &is_backup_restore_enabled) &&
401 message->GetBoolean(kIsLocationServiceEnabled, 387 message.GetBoolean(kIsLocationServiceEnabled,
402 &is_location_service_enabled)) { 388 &is_location_service_enabled)) {
403 EnableMetrics(is_metrics_enabled); 389 EnableMetrics(is_metrics_enabled);
404 EnableBackupRestore(is_backup_restore_enabled); 390 EnableBackupRestore(is_backup_restore_enabled);
405 EnableLocationService(is_location_service_enabled); 391 EnableLocationService(is_location_service_enabled);
406 arc_auth_service->StartLso(); 392 arc_auth_service->StartLso();
407 } else { 393 } else {
408 NOTREACHED(); 394 NOTREACHED();
409 } 395 }
410 } else if (event == kEventOnSendFeedbackClicked) { 396 } else if (event == kEventOnSendFeedbackClicked) {
411 chrome::OpenFeedbackDialog(nullptr); 397 chrome::OpenFeedbackDialog(nullptr);
412 } else { 398 } else {
413 LOG(ERROR) << "Unknown message: " << message_string; 399 LOG(ERROR) << "Unknown message: " << event;
414 NOTREACHED(); 400 NOTREACHED();
415 } 401 }
416 } 402 }
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