Update To 11.40.268.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 "base/scoped_observer.h"
13 #include "components/sync_driver/device_info_tracker.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/event_router.h"
16 #include "extensions/browser/extension_registry_observer.h"
17
18 class Profile;
19
20 namespace content {
21 class BrowserContext;
22 class NotificationDetails;
23 class NotificationObserver;
24 class NotificationRegistrar;
25 }  // namespace content
26
27 namespace extensions {
28 class BrowserContextKeyedAPI;
29 class ExtensionRegistry;
30
31 struct EventListenerInfo;
32
33 // An object of this class is created for each extension that has registered
34 // to be notified for device info change. The objects listen for notification
35 // from sync on device info change. On receiving the notification the
36 // new list of devices is constructed and passed back to the extension.
37 // The extension id is part of this object as it is needed to fill in the
38 // public ids for devices(public ids for a device, is not the same for
39 // all extensions).
40 class SignedInDevicesChangeObserver
41     : public sync_driver::DeviceInfoTracker::Observer {
42  public:
43   SignedInDevicesChangeObserver(const std::string& extension_id,
44                                 Profile* profile);
45   virtual ~SignedInDevicesChangeObserver();
46
47   void OnDeviceInfoChange() override;
48
49   const std::string& extension_id() {
50     return extension_id_;
51   }
52
53  private:
54   std::string extension_id_;
55   Profile* const profile_;
56   content::NotificationRegistrar registrar_;
57 };
58
59 class SignedInDevicesManager : public BrowserContextKeyedAPI,
60                                public ExtensionRegistryObserver,
61                                public EventRouter::Observer {
62  public:
63   // Default constructor used for testing.
64   SignedInDevicesManager();
65   explicit SignedInDevicesManager(content::BrowserContext* context);
66   ~SignedInDevicesManager() override;
67
68   // BrowserContextKeyedAPI implementation.
69   static BrowserContextKeyedAPIFactory<SignedInDevicesManager>*
70       GetFactoryInstance();
71
72   // ExtensionRegistryObserver implementation.
73   void OnExtensionUnloaded(content::BrowserContext* browser_context,
74                            const Extension* extension,
75                            UnloadedExtensionInfo::Reason reason) override;
76
77   // EventRouter::Observer:
78   void OnListenerAdded(const EventListenerInfo& details) override;
79   void OnListenerRemoved(const EventListenerInfo& details) override;
80
81  private:
82   friend class BrowserContextKeyedAPIFactory<SignedInDevicesManager>;
83
84   // BrowserContextKeyedAPI implementation.
85   static const char* service_name() {
86     return "SignedInDevicesManager";
87   }
88   static const bool kServiceHasOwnInstanceInIncognito = true;
89
90   void RemoveChangeObserverForExtension(const std::string& extension_id);
91
92   Profile* const profile_;
93   ScopedVector<SignedInDevicesChangeObserver> change_observers_;
94
95   // Listen to extension unloaded notification.
96   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
97       extension_registry_observer_;
98
99   FRIEND_TEST_ALL_PREFIXES(SignedInDevicesManager, UpdateListener);
100
101   DISALLOW_COPY_AND_ASSIGN(SignedInDevicesManager);
102 };
103
104 }  // namespace extensions
105
106 #endif  // CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__