Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / ash / app_sync_ui_state.cc
blobd43909338e865e7eb92c82324e4ee6124b571419
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"
20 #endif
22 namespace {
24 // Max loading animation time in milliseconds.
25 const int kMaxSyncingTimeMs = 60 * 1000;
27 } // namespace
29 // static
30 AppSyncUIState* AppSyncUIState::Get(Profile* profile) {
31 return AppSyncUIStateFactory::GetForProfile(profile);
34 // static
35 bool AppSyncUIState::ShouldObserveAppSyncForProfile(Profile* profile) {
36 #if defined(OS_CHROMEOS)
37 if (user_manager::UserManager::Get()->IsLoggedInAsGuest())
38 return false;
40 if (!profile || profile->IsOffTheRecord())
41 return false;
43 if (!ProfileSyncServiceFactory::HasProfileSyncService(profile))
44 return false;
46 return profile->IsNewProfile();
47 #else
48 return false;
49 #endif
52 AppSyncUIState::AppSyncUIState(Profile* profile)
53 : profile_(profile),
54 sync_service_(NULL),
55 status_(STATUS_NORMAL),
56 extension_registry_(NULL) {
57 StartObserving();
60 AppSyncUIState::~AppSyncUIState() {
61 StopObserving();
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_);
81 CHECK(sync_service_);
82 sync_service_->AddObserver(this);
85 void AppSyncUIState::StopObserving() {
86 if (!sync_service_)
87 return;
89 sync_service_->RemoveObserver(this);
90 sync_service_ = NULL;
92 if (extension_registry_)
93 extension_registry_->RemoveObserver(this);
94 extension_registry_ = NULL;
96 profile_ = NULL;
99 void AppSyncUIState::SetStatus(Status status) {
100 if (status_ == status)
101 return;
103 status_ = status;
104 switch (status_) {
105 case STATUS_SYNCING:
106 max_syncing_status_timer_.Start(
107 FROM_HERE,
108 base::TimeDelta::FromMilliseconds(kMaxSyncingTimeMs),
109 this, &AppSyncUIState::OnMaxSyncingTimer);
110 break;
111 case STATUS_NORMAL:
112 case STATUS_TIMED_OUT:
113 max_syncing_status_timer_.Stop();
114 StopObserving();
115 break;
118 FOR_EACH_OBSERVER(AppSyncUIStateObserver,
119 observers_,
120 OnAppSyncUIStatusChanged());
123 void AppSyncUIState::CheckAppSync() {
124 if (!sync_service_ || !sync_service_->HasSyncSetupCompleted())
125 return;
127 const bool synced = sync_service_->IsSyncActive();
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);
134 else
135 SetStatus(STATUS_SYNCING);
138 void AppSyncUIState::OnMaxSyncingTimer() {
139 SetStatus(STATUS_TIMED_OUT);
142 void AppSyncUIState::OnStateChanged() {
143 DCHECK(sync_service_);
144 CheckAppSync();
147 void AppSyncUIState::OnExtensionLoaded(content::BrowserContext* browser_context,
148 const extensions::Extension* extension) {
149 CheckAppSync();