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/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/sync/profile_sync_service.h"
13 #include "chrome/browser/sync/profile_sync_service_factory.h"
14 #include "chrome/browser/ui/ash/app_sync_ui_state_factory.h"
15 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_source.h"
19 #include "extensions/browser/pending_extension_manager.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/user_manager.h"
27 // Max loading animation time in milliseconds.
28 const int kMaxSyncingTimeMs
= 60 * 1000;
33 AppSyncUIState
* AppSyncUIState::Get(Profile
* profile
) {
34 return AppSyncUIStateFactory::GetForProfile(profile
);
38 bool AppSyncUIState::ShouldObserveAppSyncForProfile(Profile
* profile
) {
39 #if defined(OS_CHROMEOS)
40 if (chromeos::UserManager::Get()->IsLoggedInAsGuest())
43 if (!profile
|| profile
->IsOffTheRecord())
46 if (!ProfileSyncServiceFactory::HasProfileSyncService(profile
))
49 return profile
->IsNewProfile();
55 AppSyncUIState::AppSyncUIState(Profile
* profile
)
58 status_(STATUS_NORMAL
) {
62 AppSyncUIState::~AppSyncUIState() {
66 void AppSyncUIState::AddObserver(AppSyncUIStateObserver
* observer
) {
67 observers_
.AddObserver(observer
);
70 void AppSyncUIState::RemoveObserver(AppSyncUIStateObserver
* observer
) {
71 observers_
.RemoveObserver(observer
);
74 void AppSyncUIState::StartObserving() {
75 DCHECK(ShouldObserveAppSyncForProfile(profile_
));
76 DCHECK(!sync_service_
);
79 chrome::NOTIFICATION_EXTENSION_LOADED
,
80 content::Source
<Profile
>(profile_
));
82 sync_service_
= ProfileSyncServiceFactory::GetForProfile(profile_
);
84 sync_service_
->AddObserver(this);
87 void AppSyncUIState::StopObserving() {
91 registrar_
.RemoveAll();
92 sync_service_
->RemoveObserver(this);
97 void AppSyncUIState::SetStatus(Status status
) {
98 if (status_
== status
)
104 max_syncing_status_timer_
.Start(
106 base::TimeDelta::FromMilliseconds(kMaxSyncingTimeMs
),
107 this, &AppSyncUIState::OnMaxSyncingTimer
);
110 case STATUS_TIMED_OUT
:
111 max_syncing_status_timer_
.Stop();
116 FOR_EACH_OBSERVER(AppSyncUIStateObserver
,
118 OnAppSyncUIStatusChanged());
121 void AppSyncUIState::CheckAppSync() {
122 if (!sync_service_
|| !sync_service_
->HasSyncSetupCompleted())
125 const bool synced
= sync_service_
->ShouldPushChanges();
126 const bool has_pending_extension
=
127 extensions::ExtensionSystem::Get(profile_
)->extension_service()->
128 pending_extension_manager()->HasPendingExtensionFromSync();
130 if (synced
&& !has_pending_extension
)
131 SetStatus(STATUS_NORMAL
);
133 SetStatus(STATUS_SYNCING
);
136 void AppSyncUIState::OnMaxSyncingTimer() {
137 SetStatus(STATUS_TIMED_OUT
);
140 void AppSyncUIState::Observe(int type
,
141 const content::NotificationSource
& source
,
142 const content::NotificationDetails
& details
) {
143 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_LOADED
, type
);
147 void AppSyncUIState::OnStateChanged() {
148 DCHECK(sync_service_
);