- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / app_sync_ui_state.h
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.
4
5 #ifndef CHROME_BROWSER_UI_ASH_APP_SYNC_UI_STATE_H_
6 #define CHROME_BROWSER_UI_ASH_APP_SYNC_UI_STATE_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/observer_list.h"
11 #include "base/timer/timer.h"
12 #include "chrome/browser/sync/profile_sync_service_observer.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16
17 class AppSyncUIStateObserver;
18 class Profile;
19 class ProfileSyncService;
20
21 // AppSyncUIState watches app sync and installation and change its state
22 // accordingly. Its status is for UI display only. It only watches for new
23 // normal user profile (i.e. it does not watch for guest profile or exsiting
24 // user profile) and lasts for at the most 1 minute.
25 class AppSyncUIState : public BrowserContextKeyedService,
26                        public content::NotificationObserver,
27                        public ProfileSyncServiceObserver {
28  public:
29   enum Status {
30     STATUS_NORMAL,
31     STATUS_SYNCING,    // Syncing apps or installing synced apps.
32     STATUS_TIMED_OUT,  // Timed out when waiting for sync to finish.
33   };
34
35   // Returns the instance for the given |profile|. It's a convenience wrapper
36   // of AppSyncUIStateFactory::GetForProfile. Note this function returns
37   // NULL if ShouldObserveAppSyncForProfile returns false for |profile|.
38   static AppSyncUIState* Get(Profile* profile);
39
40   // Returns true if |profile| should be watched for app syncing.
41   static bool ShouldObserveAppSyncForProfile(Profile* profile);
42
43   explicit AppSyncUIState(Profile* profile);
44   virtual ~AppSyncUIState();
45
46   void AddObserver(AppSyncUIStateObserver* observer);
47   void RemoveObserver(AppSyncUIStateObserver* observer);
48
49   Status status() const { return status_; }
50
51  private:
52   void StartObserving();
53   void StopObserving();
54
55   void SetStatus(Status status);
56
57   // Checks and sets app sync status. If sync has not setup, do nothing. If sync
58   // is completed and there is no pending synced extension install, sets
59   // STATUS_SYNCING. Otherwise, sets STATUS_NORMAL.
60   void CheckAppSync();
61
62   // Invoked when |max_syncing_status_timer_| fires.
63   void OnMaxSyncingTimer();
64
65   // content::NotificationObserver overrides:
66   virtual void Observe(int type,
67                        const content::NotificationSource& source,
68                        const content::NotificationDetails& details) OVERRIDE;
69
70   // ProfileSyncServiceObserver overrides:
71   virtual void OnStateChanged() OVERRIDE;
72
73   content::NotificationRegistrar registrar_;
74
75   Profile* profile_;
76   ProfileSyncService* sync_service_;
77
78   // Timer to limit how much time STATUS_SYNCING is allowed.
79   base::OneShotTimer<AppSyncUIState> max_syncing_status_timer_;
80
81   Status status_;
82   ObserverList<AppSyncUIStateObserver> observers_;
83
84   DISALLOW_COPY_AND_ASSIGN(AppSyncUIState);
85 };
86
87 #endif  // CHROME_BROWSER_UI_ASH_APP_SYNC_UI_STATE_H_