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

Side by Side Diff: chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc

Issue 2353213002: arc: Fix Android App resurrected by sync. (Closed)
Patch Set: Android App resurrected by sync. 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/ui/app_list/arc/arc_package_sync_data_type_controller.h " 5 #include "chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.h "
6 6
7 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
7 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" 9 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
9 #include "chrome/common/pref_names.h" 10 #include "chrome/common/pref_names.h"
10 #include "components/arc/common/app.mojom.h" 11 #include "components/arc/arc_bridge_service.h"
11 #include "components/arc/instance_holder.h"
12 #include "components/prefs/pref_service.h" 12 #include "components/prefs/pref_service.h"
13 #include "components/sync/driver/pref_names.h"
13 #include "components/sync/driver/sync_client.h" 14 #include "components/sync/driver/sync_client.h"
14 #include "components/sync/driver/sync_prefs.h" 15 #include "components/sync/driver/sync_prefs.h"
15 #include "components/sync/driver/sync_service.h" 16 #include "components/sync/driver/sync_service.h"
16 17
17 // ArcPackage sync service is controlled by apps checkbox in sync settings. Arc 18 // ArcPackage sync service is controlled by apps checkbox in sync settings. Arc
18 // apps and regular Chrome apps have same user control. 19 // apps and regular Chrome apps have same user control.
20 namespace {
21
22 bool ValidateEnableArcPackageSyncPref(Profile* profile) {
23 PrefService* pref_service = profile->GetPrefs();
pavely 2016/09/28 20:19:01 You can use SyncService::GetPreferredDataTypes() a
lgcheng 2016/09/28 21:40:36 Done.
24 // If device is set to sync everything, Arc package should be synced.
25 if (pref_service->GetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced))
26 return true;
27
28 const bool apps_sync_enable = pref_service->GetBoolean(
29 sync_driver::SyncPrefs::GetPrefNameForDataType(syncer::APPS));
30 // ArcPackage sync service is controlled by apps checkbox in sync settings.
31 // Update ArcPackage sync setting pref if it is different from apps sync
32 // setting pref.
33 const char* arc_sync_path =
34 sync_driver::SyncPrefs::GetPrefNameForDataType(syncer::ARC_PACKAGE);
35 if (apps_sync_enable != pref_service->GetBoolean(arc_sync_path)) {
36 pref_service->SetBoolean(arc_sync_path, apps_sync_enable);
pavely 2016/09/28 20:19:01 Why do you need to implement this logic? Is there
lgcheng 2016/09/28 21:40:36 There was a scenario that user chose what to sync(
37 }
38
39 return apps_sync_enable;
40 }
41
42 // Indicates whether ARC is enabled on this machine.
43 bool IsArcEnabled(Profile* profile) {
44 return arc::ArcAuthService::IsAllowedForProfile(profile) &&
45 profile->GetPrefs()->GetBoolean(prefs::kArcEnabled);
46 }
47
48 } // namespace
49
19 ArcPackageSyncDataTypeController::ArcPackageSyncDataTypeController( 50 ArcPackageSyncDataTypeController::ArcPackageSyncDataTypeController(
20 syncer::ModelType type, 51 syncer::ModelType type,
21 const base::Closure& dump_stack, 52 const base::Closure& dump_stack,
22 sync_driver::SyncClient* sync_client, 53 sync_driver::SyncClient* sync_client,
23 Profile* profile) 54 Profile* profile)
24 : sync_driver::UIDataTypeController(type, dump_stack, sync_client), 55 : sync_driver::UIDataTypeController(type, dump_stack, sync_client),
25 profile_(profile), 56 profile_(profile),
26 sync_client_(sync_client) { 57 sync_client_(sync_client) {
27 pref_registrar_.Init(profile_->GetPrefs()); 58 pref_registrar_.Init(profile_->GetPrefs());
28 pref_registrar_.Add( 59 pref_registrar_.Add(
29 sync_driver::SyncPrefs::GetPrefNameForDataType(type), 60 sync_driver::SyncPrefs::GetPrefNameForDataType(type),
30 base::Bind(&ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged, 61 base::Bind(&ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged,
31 base::Unretained(this))); 62 base::Unretained(this)));
32 pref_registrar_.Add( 63 pref_registrar_.Add(
33 prefs::kArcEnabled, 64 prefs::kArcEnabled,
34 base::Bind(&ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged, 65 base::Bind(&ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged,
35 base::Unretained(this))); 66 base::Unretained(this)));
67
68 arc::ArcBridgeService* arc_bridge_service = arc::ArcBridgeService::Get();
69 if (arc_bridge_service)
70 arc_bridge_service->app()->AddObserver(this);
36 } 71 }
37 72
38 ArcPackageSyncDataTypeController::~ArcPackageSyncDataTypeController() {} 73 ArcPackageSyncDataTypeController::~ArcPackageSyncDataTypeController() {
74 arc::ArcBridgeService* arc_bridge_service = arc::ArcBridgeService::Get();
75 if (arc_bridge_service)
76 arc_bridge_service->app()->RemoveObserver(this);
77 }
39 78
40 bool ArcPackageSyncDataTypeController::ReadyForStart() const { 79 bool ArcPackageSyncDataTypeController::ReadyForStart() const {
41 DCHECK(CalledOnValidThread()); 80 DCHECK(CalledOnValidThread());
81 return IsArcEnabled(profile_) && ValidateEnableArcPackageSyncPref(profile_);
82 }
83
84 void ArcPackageSyncDataTypeController::OnInstanceReady() {
85 // model_normal_start_ is true by default. Normally,
86 // ArcPackageSyncDataTypeController::StartModels() gets called before Arc is
87 // ready. But in integration test, the order can be either way. If
88 // OnInstanceReady comes before ArcPackageSyncDataTypeController
89 // ::StartModels(), this function is no-op and waits for StartModels() to be
90 // called.
91 if (model_normal_start_)
92 return;
93
94 model_normal_start_ = true;
95 OnModelLoaded();
96 }
97
98 bool ArcPackageSyncDataTypeController::StartModels() {
99 DCHECK_EQ(state(), MODEL_STARTING);
42 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_); 100 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_);
43 return profile_->GetPrefs()->GetBoolean( 101 model_normal_start_ = prefs && prefs->app_instance_holder()->has_instance();
44 sync_driver::SyncPrefs::GetPrefNameForDataType(type())) && 102 return model_normal_start_;
45 prefs && prefs->app_instance_holder()->has_instance();
46 } 103 }
47 104
48 void ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged() { 105 void ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged() {
49 DCHECK(CalledOnValidThread()); 106 DCHECK(CalledOnValidThread());
50 107
51 if (!ReadyForStart()) { 108 if (!ReadyForStart()) {
52 // If apps sync in advanced sync settings is turned off then generate an 109 // If apps sync in advanced sync settings is turned off then generate an
53 // unrecoverable error. 110 // unrecoverable error.
54 if (state() != NOT_RUNNING && state() != STOPPING) { 111 if (state() != NOT_RUNNING && state() != STOPPING) {
55 syncer::SyncError error( 112 syncer::SyncError error(
56 FROM_HERE, syncer::SyncError::DATATYPE_POLICY_ERROR, 113 FROM_HERE, syncer::SyncError::DATATYPE_POLICY_ERROR,
57 "Arc package sync is now disabled by user.", type()); 114 "Arc package sync is now disabled by user.", type());
58 CreateErrorHandler()->OnUnrecoverableError(error); 115 CreateErrorHandler()->OnUnrecoverableError(error);
59 } 116 }
60 return; 117 return;
61 } 118 }
62 sync_driver::SyncService* sync_service = sync_client_->GetSyncService(); 119 EnableDataType();
63 DCHECK(sync_service);
64 sync_service->ReenableDatatype(type());
65 } 120 }
66 121
67 void ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged() { 122 void ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged() {
68 DCHECK(CalledOnValidThread()); 123 DCHECK(CalledOnValidThread());
69 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { 124
125 if (!ReadyForStart()) {
70 // If enable Arc in settings is turned off then generate an unrecoverable 126 // If enable Arc in settings is turned off then generate an unrecoverable
71 // error. 127 // error.
72 if (state() != NOT_RUNNING && state() != STOPPING) { 128 if (state() != NOT_RUNNING && state() != STOPPING) {
73 syncer::SyncError error( 129 syncer::SyncError error(
74 FROM_HERE, syncer::SyncError::DATATYPE_POLICY_ERROR, 130 FROM_HERE, syncer::SyncError::DATATYPE_POLICY_ERROR,
75 "Arc package sync is now disabled because user disables Arc.", 131 "Arc package sync is now disabled because user disables Arc.",
76 type()); 132 type());
77 CreateErrorHandler()->OnUnrecoverableError(error); 133 CreateErrorHandler()->OnUnrecoverableError(error);
78 } 134 }
135 return;
79 } 136 }
137 EnableDataType();
80 } 138 }
139
140 void ArcPackageSyncDataTypeController::EnableDataType() {
141 sync_driver::SyncService* sync_service = sync_client_->GetSyncService();
142 DCHECK(sync_service);
143 sync_service->ReenableDatatype(type());
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698