1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/ash/app_sync_ui_state.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/pending_extension_manager.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "chrome/browser/sync/profile_sync_service_factory.h"
13 #include "chrome/browser/ui/ash/app_sync_ui_state_factory.h"
14 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
18 #if defined(OS_CHROMEOS)
19 #include "components/user_manager/user_manager.h"
24 // Max loading animation time in milliseconds.
25 const int kMaxSyncingTimeMs
= 60 * 1000;
30 AppSyncUIState
* AppSyncUIState::Get(Profile
* profile
) {
31 return AppSyncUIStateFactory::GetForProfile(profile
);
35 bool AppSyncUIState::ShouldObserveAppSyncForProfile(Profile
* profile
) {
36 #if defined(OS_CHROMEOS)
37 if (user_manager::UserManager::Get()->IsLoggedInAsGuest())
40 if (!profile
|| profile
->IsOffTheRecord())
43 if (!ProfileSyncServiceFactory::HasProfileSyncService(profile
))
46 return profile
->IsNewProfile();
52 AppSyncUIState::AppSyncUIState(Profile
* profile
)
55 status_(STATUS_NORMAL
),
56 extension_registry_(NULL
) {
60 AppSyncUIState::~AppSyncUIState() {
64 void AppSyncUIState::AddObserver(AppSyncUIStateObserver
* observer
) {
65 observers_
.AddObserver(observer
);
68 void AppSyncUIState::RemoveObserver(AppSyncUIStateObserver
* observer
) {
69 observers_
.RemoveObserver(observer
);
72 void AppSyncUIState::StartObserving() {
73 DCHECK(ShouldObserveAppSyncForProfile(profile_
));
74 DCHECK(!sync_service_
);
75 DCHECK(!extension_registry_
);
77 extension_registry_
= extensions::ExtensionRegistry::Get(profile_
);
78 extension_registry_
->AddObserver(this);
80 sync_service_
= ProfileSyncServiceFactory::GetForProfile(profile_
);
82 sync_service_
->AddObserver(this);
85 void AppSyncUIState::StopObserving() {
89 sync_service_
->RemoveObserver(this);
92 if (extension_registry_
)
93 extension_registry_
->RemoveObserver(this);
94 extension_registry_
= NULL
;
99 void AppSyncUIState::SetStatus(Status status
) {
100 if (status_
== status
)
106 max_syncing_status_timer_
.Start(
108 base::TimeDelta::FromMilliseconds(kMaxSyncingTimeMs
),
109 this, &AppSyncUIState::OnMaxSyncingTimer
);
112 case STATUS_TIMED_OUT
:
113 max_syncing_status_timer_
.Stop();
118 FOR_EACH_OBSERVER(AppSyncUIStateObserver
,
120 OnAppSyncUIStatusChanged());
123 void AppSyncUIState::CheckAppSync() {
124 if (!sync_service_
|| !sync_service_
->HasSyncSetupCompleted())
127 const bool synced
= sync_service_
->SyncActive();
128 const bool has_pending_extension
=
129 extensions::ExtensionSystem::Get(profile_
)->extension_service()->
130 pending_extension_manager()->HasPendingExtensionFromSync();
132 if (synced
&& !has_pending_extension
)
133 SetStatus(STATUS_NORMAL
);
135 SetStatus(STATUS_SYNCING
);
138 void AppSyncUIState::OnMaxSyncingTimer() {
139 SetStatus(STATUS_TIMED_OUT
);
142 void AppSyncUIState::OnStateChanged() {
143 DCHECK(sync_service_
);
147 void AppSyncUIState::OnExtensionLoaded(content::BrowserContext
* browser_context
,
148 const extensions::Extension
* extension
) {