Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / signed_in_devices / signed_in_devices_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_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__
7
8 #include <string>
9
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_vector.h"
12 #include "chrome/browser/sync/glue/synced_device_tracker.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "extensions/browser/event_router.h"
15
16 class Profile;
17
18 namespace content {
19 class BrowserContext;
20 class NotificationDetails;
21 class NotificationObserver;
22 class NotificationRegistrar;
23 }  // namespace content
24
25 namespace extensions {
26 class BrowserContextKeyedAPI;
27
28 struct EventListenerInfo;
29
30 // An object of this class is created for each extension that has registered
31 // to be notified for device info change. The objects listen for notification
32 // from sync on device info change. On receiving the notification the
33 // new list of devices is constructed and passed back to the extension.
34 // The extension id is part of this object as it is needed to fill in the
35 // public ids for devices(public ids for a device, is not the same for
36 // all extensions).
37 class SignedInDevicesChangeObserver
38     : public browser_sync::SyncedDeviceTracker::Observer {
39  public:
40   SignedInDevicesChangeObserver(const std::string& extension_id,
41                                 Profile* profile);
42   virtual ~SignedInDevicesChangeObserver();
43
44   virtual void OnDeviceInfoChange() OVERRIDE;
45
46   const std::string& extension_id() {
47     return extension_id_;
48   }
49
50  private:
51   std::string extension_id_;
52   Profile* const profile_;
53   content::NotificationRegistrar registrar_;
54 };
55
56 class SignedInDevicesManager : public BrowserContextKeyedAPI,
57                                public content::NotificationObserver,
58                                public EventRouter::Observer {
59  public:
60   // Default constructor used for testing.
61   SignedInDevicesManager();
62   explicit SignedInDevicesManager(content::BrowserContext* context);
63   virtual ~SignedInDevicesManager();
64
65   // BrowserContextKeyedAPI implementation.
66   static BrowserContextKeyedAPIFactory<SignedInDevicesManager>*
67       GetFactoryInstance();
68
69   // NotificationObserver:
70   virtual void Observe(int type,
71                        const content::NotificationSource& source,
72                        const content::NotificationDetails& details) OVERRIDE;
73
74   // EventRouter::Observer:
75   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
76   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
77
78  private:
79   friend class BrowserContextKeyedAPIFactory<SignedInDevicesManager>;
80
81   // BrowserContextKeyedAPI implementation.
82   static const char* service_name() {
83     return "SignedInDevicesManager";
84   }
85   static const bool kServiceHasOwnInstanceInIncognito = true;
86
87   void RemoveChangeObserverForExtension(const std::string& extension_id);
88
89   Profile* const profile_;
90   content::NotificationRegistrar registrar_;
91   ScopedVector<SignedInDevicesChangeObserver> change_observers_;
92
93   FRIEND_TEST_ALL_PREFIXES(SignedInDevicesManager, UpdateListener);
94
95   DISALLOW_COPY_AND_ASSIGN(SignedInDevicesManager);
96 };
97
98 }  // namespace extensions
99
100 #endif  // CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__