- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / device_local_account_policy_service.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_CHROMEOS_POLICY_DEVICE_LOCAL_ACCOUNT_POLICY_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_LOCAL_ACCOUNT_POLICY_SERVICE_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
21 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
22
23 namespace base {
24 class SequencedTaskRunner;
25 }
26
27 namespace chromeos {
28 class DeviceSettingsService;
29 class SessionManagerClient;
30 }
31
32 namespace policy {
33
34 struct DeviceLocalAccount;
35 class DeviceLocalAccountPolicyStore;
36 class DeviceManagementService;
37
38 // The main switching central that downloads, caches, refreshes, etc. policy for
39 // a single device-local account.
40 class DeviceLocalAccountPolicyBroker {
41  public:
42   // |task_runner| is the runner for policy refresh tasks.
43   DeviceLocalAccountPolicyBroker(
44       const DeviceLocalAccount& account,
45       scoped_ptr<DeviceLocalAccountPolicyStore> store,
46       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
47   ~DeviceLocalAccountPolicyBroker();
48
49   // Initialize the broker, loading its |store_|.
50   void Initialize();
51
52   // For the difference between |account_id| and |user_id|, see the
53   // documentation of DeviceLocalAccount.
54   const std::string& account_id() const { return account_id_; }
55   const std::string& user_id() const { return user_id_; }
56
57   scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader>
58       extension_loader() const { return extension_loader_; }
59
60   CloudPolicyCore* core() { return &core_; }
61   const CloudPolicyCore* core() const { return &core_; }
62
63   // Fire up the cloud connection for fetching policy for the account from the
64   // cloud if this is an enterprise-managed device.
65   void ConnectIfPossible(
66       chromeos::DeviceSettingsService* device_settings_service,
67       DeviceManagementService* device_management_service);
68
69   // Destroy the cloud connection, stopping policy refreshes.
70   void Disconnect();
71
72   // Reads the refresh delay from policy and configures the refresh scheduler.
73   void UpdateRefreshDelay();
74
75   // Retrieves the display name for the account as stored in policy. Returns an
76   // empty string if the policy is not present.
77   std::string GetDisplayName() const;
78
79  private:
80   const std::string account_id_;
81   const std::string user_id_;
82   const scoped_ptr<DeviceLocalAccountPolicyStore> store_;
83   scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader>
84       extension_loader_;
85   CloudPolicyCore core_;
86
87   DISALLOW_COPY_AND_ASSIGN(DeviceLocalAccountPolicyBroker);
88 };
89
90 // Manages user policy blobs for device-local accounts present on the device.
91 // The actual policy blobs are brokered by session_manager (to prevent file
92 // manipulation), and we're making signature checks on the policy blobs to
93 // ensure they're issued by the device owner.
94 class DeviceLocalAccountPolicyService : public CloudPolicyStore::Observer {
95  public:
96   // Interface for interested parties to observe policy changes.
97   class Observer {
98    public:
99     virtual ~Observer() {}
100
101     // Policy for the given |user_id| has changed.
102     virtual void OnPolicyUpdated(const std::string& user_id) = 0;
103
104     // The list of accounts has been updated.
105     virtual void OnDeviceLocalAccountsChanged() = 0;
106   };
107
108   DeviceLocalAccountPolicyService(
109       chromeos::SessionManagerClient* session_manager_client,
110       chromeos::DeviceSettingsService* device_settings_service,
111       chromeos::CrosSettings* cros_settings,
112       scoped_refptr<base::SequencedTaskRunner> store_background_task_runner,
113       scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner);
114   virtual ~DeviceLocalAccountPolicyService();
115
116   // Initializes the cloud policy service connection.
117   void Connect(DeviceManagementService* device_management_service);
118
119   // Prevents further policy fetches from the cloud.
120   void Disconnect();
121
122   // Get the policy broker for a given |user_id|. Returns NULL if that |user_id|
123   // does not belong to an existing device-local account.
124   DeviceLocalAccountPolicyBroker* GetBrokerForUser(const std::string& user_id);
125
126   // Indicates whether policy has been successfully fetched for the given
127   // |user_id|.
128   bool IsPolicyAvailableForUser(const std::string& user_id);
129
130   void AddObserver(Observer* observer);
131   void RemoveObserver(Observer* observer);
132
133   // CloudPolicyStore::Observer:
134   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
135   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
136
137  private:
138   typedef std::map<std::string, DeviceLocalAccountPolicyBroker*>
139       PolicyBrokerMap;
140
141   // Returns |true| if the directory in which force-installed extensions are
142   // cached for |account_id| is busy, either because a broker that was using
143   // this directory has not shut down completely yet or because the directory is
144   // being deleted.
145   bool IsExtensionCacheDirectoryBusy(const std::string& account_id);
146
147   // Starts any extension caches that are not running yet but can be started now
148   // because their cache directories are no longer busy.
149   void StartExtensionCachesIfPossible();
150
151   // Checks whether a broker exists for |account_id|. If so, starts the broker's
152   // extension cache and returns |true|. Otherwise, returns |false|.
153   bool StartExtensionCacheForAccountIfPresent(const std::string& account_id);
154
155   // Called back when any extension caches belonging to device-local accounts
156   // that no longer exist have been removed at start-up.
157   void OnOrphanedExtensionCachesDeleted();
158
159   // Called back when the extension cache for |account_id| has been shut down.
160   void OnObsoleteExtensionCacheShutdown(const std::string& account_id);
161
162   // Called back when the extension cache for |account_id| has been removed.
163   void OnObsoleteExtensionCacheDeleted(const std::string& account_id);
164
165   // Re-queries the list of defined device-local accounts from device settings
166   // and updates |policy_brokers_| to match that list.
167   void UpdateAccountList();
168
169   // Calls |UpdateAccountList| if there are no previous calls pending.
170   void UpdateAccountListIfNonePending();
171
172   // Deletes brokers in |map| and clears it.
173   void DeleteBrokers(PolicyBrokerMap* map);
174
175   // Find the broker for a given |store|. Returns NULL if |store| is unknown.
176   DeviceLocalAccountPolicyBroker* GetBrokerForStore(CloudPolicyStore* store);
177
178   chromeos::SessionManagerClient* session_manager_client_;
179   chromeos::DeviceSettingsService* device_settings_service_;
180   chromeos::CrosSettings* cros_settings_;
181
182   DeviceManagementService* device_management_service_;
183
184   // The device-local account policy brokers, keyed by user ID.
185   PolicyBrokerMap policy_brokers_;
186
187   // Whether a call to UpdateAccountList() is pending because |cros_settings_|
188   // are not trusted yet.
189   bool waiting_for_cros_settings_;
190
191   // Orphaned extension caches are removed at startup. This tracks the status of
192   // that process.
193   enum OrphanCacheDeletionState {
194     NOT_STARTED,
195     IN_PROGRESS,
196     DONE,
197   };
198   OrphanCacheDeletionState orphan_cache_deletion_state_;
199
200   // Account IDs whose extension cache directories are busy, either because a
201   // broker for the account has not shut down completely yet or because the
202   // directory is being deleted.
203   std::set<std::string> busy_extension_cache_directories_;
204
205   const scoped_refptr<base::SequencedTaskRunner> store_background_task_runner_;
206   const scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner_;
207
208   ObserverList<Observer, true> observers_;
209
210   const scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
211       local_accounts_subscription_;
212
213   base::WeakPtrFactory<DeviceLocalAccountPolicyService> weak_factory_;
214
215   DISALLOW_COPY_AND_ASSIGN(DeviceLocalAccountPolicyService);
216 };
217
218 }  // namespace policy
219
220 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_LOCAL_ACCOUNT_POLICY_SERVICE_H_