Chromium Code Reviews| Index: chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc |
| diff --git a/chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc b/chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc |
| index 601e410fada7a33851c065d254708d578ef59518..c2a4d8ca83d1ca956fd122bff5f04fbc34a0530f 100644 |
| --- a/chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc |
| +++ b/chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.cc |
| @@ -4,18 +4,49 @@ |
| #include "chrome/browser/ui/app_list/arc/arc_package_sync_data_type_controller.h" |
| +#include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| #include "chrome/common/pref_names.h" |
| -#include "components/arc/common/app.mojom.h" |
| -#include "components/arc/instance_holder.h" |
| +#include "components/arc/arc_bridge_service.h" |
| #include "components/prefs/pref_service.h" |
| +#include "components/sync/driver/pref_names.h" |
| #include "components/sync/driver/sync_client.h" |
| #include "components/sync/driver/sync_prefs.h" |
| #include "components/sync/driver/sync_service.h" |
| // ArcPackage sync service is controlled by apps checkbox in sync settings. Arc |
| // apps and regular Chrome apps have same user control. |
| +namespace { |
| + |
| +bool ValidateEnableArcPackageSyncPref(Profile* profile) { |
| + 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.
|
| + // If device is set to sync everything, Arc package should be synced. |
| + if (pref_service->GetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced)) |
| + return true; |
| + |
| + const bool apps_sync_enable = pref_service->GetBoolean( |
| + sync_driver::SyncPrefs::GetPrefNameForDataType(syncer::APPS)); |
| + // ArcPackage sync service is controlled by apps checkbox in sync settings. |
| + // Update ArcPackage sync setting pref if it is different from apps sync |
| + // setting pref. |
| + const char* arc_sync_path = |
| + sync_driver::SyncPrefs::GetPrefNameForDataType(syncer::ARC_PACKAGE); |
| + if (apps_sync_enable != pref_service->GetBoolean(arc_sync_path)) { |
| + 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(
|
| + } |
| + |
| + return apps_sync_enable; |
| +} |
| + |
| +// Indicates whether ARC is enabled on this machine. |
| +bool IsArcEnabled(Profile* profile) { |
| + return arc::ArcAuthService::IsAllowedForProfile(profile) && |
| + profile->GetPrefs()->GetBoolean(prefs::kArcEnabled); |
| +} |
| + |
| +} // namespace |
| + |
| ArcPackageSyncDataTypeController::ArcPackageSyncDataTypeController( |
| syncer::ModelType type, |
| const base::Closure& dump_stack, |
| @@ -33,16 +64,42 @@ ArcPackageSyncDataTypeController::ArcPackageSyncDataTypeController( |
| prefs::kArcEnabled, |
| base::Bind(&ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged, |
| base::Unretained(this))); |
| + |
| + arc::ArcBridgeService* arc_bridge_service = arc::ArcBridgeService::Get(); |
| + if (arc_bridge_service) |
| + arc_bridge_service->app()->AddObserver(this); |
| } |
| -ArcPackageSyncDataTypeController::~ArcPackageSyncDataTypeController() {} |
| +ArcPackageSyncDataTypeController::~ArcPackageSyncDataTypeController() { |
| + arc::ArcBridgeService* arc_bridge_service = arc::ArcBridgeService::Get(); |
| + if (arc_bridge_service) |
| + arc_bridge_service->app()->RemoveObserver(this); |
| +} |
| bool ArcPackageSyncDataTypeController::ReadyForStart() const { |
| DCHECK(CalledOnValidThread()); |
| + return IsArcEnabled(profile_) && ValidateEnableArcPackageSyncPref(profile_); |
| +} |
| + |
| +void ArcPackageSyncDataTypeController::OnInstanceReady() { |
| + // model_normal_start_ is true by default. Normally, |
| + // ArcPackageSyncDataTypeController::StartModels() gets called before Arc is |
| + // ready. But in integration test, the order can be either way. If |
| + // OnInstanceReady comes before ArcPackageSyncDataTypeController |
| + // ::StartModels(), this function is no-op and waits for StartModels() to be |
| + // called. |
| + if (model_normal_start_) |
| + return; |
| + |
| + model_normal_start_ = true; |
| + OnModelLoaded(); |
| +} |
| + |
| +bool ArcPackageSyncDataTypeController::StartModels() { |
| + DCHECK_EQ(state(), MODEL_STARTING); |
| ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_); |
| - return profile_->GetPrefs()->GetBoolean( |
| - sync_driver::SyncPrefs::GetPrefNameForDataType(type())) && |
| - prefs && prefs->app_instance_holder()->has_instance(); |
| + model_normal_start_ = prefs && prefs->app_instance_holder()->has_instance(); |
| + return model_normal_start_; |
| } |
| void ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged() { |
| @@ -59,14 +116,13 @@ void ArcPackageSyncDataTypeController::OnArcAppsSyncPrefChanged() { |
| } |
| return; |
| } |
| - sync_driver::SyncService* sync_service = sync_client_->GetSyncService(); |
| - DCHECK(sync_service); |
| - sync_service->ReenableDatatype(type()); |
| + EnableDataType(); |
| } |
| void ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged() { |
| DCHECK(CalledOnValidThread()); |
| - if (!profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { |
| + |
| + if (!ReadyForStart()) { |
| // If enable Arc in settings is turned off then generate an unrecoverable |
| // error. |
| if (state() != NOT_RUNNING && state() != STOPPING) { |
| @@ -76,5 +132,13 @@ void ArcPackageSyncDataTypeController::OnArcEnabledPrefChanged() { |
| type()); |
| CreateErrorHandler()->OnUnrecoverableError(error); |
| } |
| + return; |
| } |
| + EnableDataType(); |
| +} |
| + |
| +void ArcPackageSyncDataTypeController::EnableDataType() { |
| + sync_driver::SyncService* sync_service = sync_client_->GetSyncService(); |
| + DCHECK(sync_service); |
| + sync_service->ReenableDatatype(type()); |
| } |