Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / drive / drive_notification_manager.h
1 // Copyright 2013 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_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
6 #define CHROME_BROWSER_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
7
8 #include "base/memory/weak_ptr.h"
9 #include "base/observer_list.h"
10 #include "base/timer/timer.h"
11 #include "chrome/browser/drive/drive_notification_observer.h"
12 #include "components/invalidation/invalidation_handler.h"
13 #include "components/keyed_service/core/keyed_service.h"
14
15 class ProfileSyncService;
16
17 namespace invalidation {
18 class InvalidationService;
19 }
20
21 namespace drive {
22
23 // Informs observers when they should check Google Drive for updates.
24 // Conditions under which updates should be searched:
25 // 1. XMPP invalidation is received from Google Drive.
26 // 2. Polling timer counts down.
27 class DriveNotificationManager : public KeyedService,
28                                  public syncer::InvalidationHandler {
29  public:
30   explicit DriveNotificationManager(
31       invalidation::InvalidationService* invalidation_service);
32   ~DriveNotificationManager() override;
33
34   // KeyedService override.
35   void Shutdown() override;
36
37   // syncer::InvalidationHandler implementation.
38   void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
39   void OnIncomingInvalidation(
40       const syncer::ObjectIdInvalidationMap& invalidation_map) override;
41   std::string GetOwnerName() const override;
42
43   void AddObserver(DriveNotificationObserver* observer);
44   void RemoveObserver(DriveNotificationObserver* observer);
45
46   // True when XMPP notification is currently enabled.
47   bool push_notification_enabled() const {
48     return push_notification_enabled_;
49   }
50
51   // True when XMPP notification has been registered.
52   bool push_notification_registered() const {
53     return push_notification_registered_;
54   }
55
56  private:
57   enum NotificationSource {
58     NOTIFICATION_XMPP,
59     NOTIFICATION_POLLING,
60   };
61
62   // Restarts the polling timer. Used for polling-based notification.
63   void RestartPollingTimer();
64
65   // Notifies the observers that it's time to check for updates.
66   // |source| indicates where the notification comes from.
67   void NotifyObserversToUpdate(NotificationSource source);
68
69   // Registers for Google Drive invalidation notifications through XMPP.
70   void RegisterDriveNotifications();
71
72   // Returns a string representation of NotificationSource.
73   static std::string NotificationSourceToString(NotificationSource source);
74
75   invalidation::InvalidationService* invalidation_service_;
76   ObserverList<DriveNotificationObserver> observers_;
77
78   // True when Drive File Sync Service is registered for Drive notifications.
79   bool push_notification_registered_;
80   // True if the XMPP-based push notification is currently enabled.
81   bool push_notification_enabled_;
82   // True once observers are notified for the first time.
83   bool observers_notified_;
84
85   // The timer is used for polling based notification. XMPP should usually be
86   // used but notification is done per polling when XMPP is not working.
87   base::Timer polling_timer_;
88
89   // Note: This should remain the last member so it'll be destroyed and
90   // invalidate its weak pointers before any other members are destroyed.
91   base::WeakPtrFactory<DriveNotificationManager> weak_ptr_factory_;
92
93   DISALLOW_COPY_AND_ASSIGN(DriveNotificationManager);
94 };
95
96 }  // namespace drive
97
98 #endif  // CHROME_BROWSER_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_