1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file.
4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <string>
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/gtest_prod_util.h"
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/memory/scoped_vector.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/scoped_observer.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/sync_driver/device_info_tracker.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/browser_context_keyed_api_factory.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/event_router.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/browser/extension_registry_observer.h"
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class Profile;
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace content {
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BrowserContext;
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NotificationDetails;
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NotificationObserver;
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NotificationRegistrar;
25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}  // namespace content
26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace extensions {
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BrowserContextKeyedAPI;
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ExtensionRegistry;
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)struct EventListenerInfo;
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// An object of this class is created for each extension that has registered
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// to be notified for device info change. The objects listen for notification
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// from sync on device info change. On receiving the notification the
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// new list of devices is constructed and passed back to the extension.
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// The extension id is part of this object as it is needed to fill in the
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// public ids for devices(public ids for a device, is not the same for
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// all extensions).
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class SignedInDevicesChangeObserver
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : public sync_driver::DeviceInfoTracker::Observer {
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public:
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  SignedInDevicesChangeObserver(const std::string& extension_id,
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                Profile* profile);
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual ~SignedInDevicesChangeObserver();
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual void OnDeviceInfoChange() OVERRIDE;
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const std::string& extension_id() {
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return extension_id_;
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private:
54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::string extension_id_;
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* const profile_;
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::NotificationRegistrar registrar_;
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class SignedInDevicesManager : public BrowserContextKeyedAPI,
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                               public ExtensionRegistryObserver,
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                               public EventRouter::Observer {
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public:
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Default constructor used for testing.
64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  SignedInDevicesManager();
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit SignedInDevicesManager(content::BrowserContext* context);
66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual ~SignedInDevicesManager();
67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // BrowserContextKeyedAPI implementation.
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static BrowserContextKeyedAPIFactory<SignedInDevicesManager>*
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      GetFactoryInstance();
71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // ExtensionRegistryObserver implementation.
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnExtensionUnloaded(
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      content::BrowserContext* browser_context,
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const Extension* extension,
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      UnloadedExtensionInfo::Reason reason) OVERRIDE;
77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // EventRouter::Observer:
79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private:
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  friend class BrowserContextKeyedAPIFactory<SignedInDevicesManager>;
84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // BrowserContextKeyedAPI implementation.
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static const char* service_name() {
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return "SignedInDevicesManager";
88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static const bool kServiceHasOwnInstanceInIncognito = true;
90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void RemoveChangeObserverForExtension(const std::string& extension_id);
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* const profile_;
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ScopedVector<SignedInDevicesChangeObserver> change_observers_;
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Listen to extension unloaded notification.
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extension_registry_observer_;
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SignedInDevicesManager, UpdateListener);
101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SignedInDevicesManager);
103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}  // namespace extensions
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif  // CHROME_BROWSER_EXTENSIONS_API_SIGNED_IN_DEVICES_SIGNED_IN_DEVICES_MANAGER_H__
108