Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / cloud_external_data_policy_observer.h
blob8c61b0e1f3b0aca6cd99ee5a6a288b871073e6f7
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
22 namespace policy {
24 // Helper for implementing policies referencing external data: This class
25 // observes a given |policy_| and fetches the external data that it references
26 // for all users on the device. Notifications are emitted when an external data
27 // reference is set, cleared or an external data fetch completes successfully.
29 // State is kept at runtime only: External data references that already exist
30 // when the class is instantiated are considered new, causing a notification to
31 // be emitted that an external data reference has been set and the referenced
32 // external data to be fetched.
33 class CloudExternalDataPolicyObserver
34 : public content::NotificationObserver,
35 public DeviceLocalAccountPolicyService::Observer {
36 public:
37 class Delegate {
38 public:
39 // Invoked when an external data reference is set for |user_id|.
40 virtual void OnExternalDataSet(const std::string& policy,
41 const std::string& user_id);
43 // Invoked when the external data reference is cleared for |user_id|.
44 virtual void OnExternalDataCleared(const std::string& policy,
45 const std::string& user_id);
47 // Invoked when the external data referenced for |user_id| has been fetched.
48 // Failed fetches are retried and the method is called only when a fetch
49 // eventually succeeds. If a fetch fails permanently (e.g. because the
50 // external data reference specifies an invalid URL), the method is not
51 // called at all.
52 virtual void OnExternalDataFetched(const std::string& policy,
53 const std::string& user_id,
54 scoped_ptr<std::string> data);
56 protected:
57 virtual ~Delegate();
60 CloudExternalDataPolicyObserver(
61 chromeos::CrosSettings* cros_settings,
62 DeviceLocalAccountPolicyService* device_local_account_policy_service,
63 const std::string& policy,
64 Delegate* delegate);
65 ~CloudExternalDataPolicyObserver() override;
67 void Init();
69 // content::NotificationObserver:
70 void Observe(int type,
71 const content::NotificationSource& source,
72 const content::NotificationDetails& details) override;
74 // DeviceLocalAccountPolicyService::Observer:
75 void OnPolicyUpdated(const std::string& user_id) override;
76 void OnDeviceLocalAccountsChanged() override;
78 private:
79 // Helper class that observes |policy_| for a logged-in user.
80 class PolicyServiceObserver;
82 void RetrieveDeviceLocalAccounts();
84 // Handles the new policy map |entry| for |user_id| by canceling any external
85 // data fetch currently in progress, emitting a notification that an external
86 // data reference has been cleared (if |entry| is NULL) or set (otherwise),
87 // starting a new external data fetch in the latter case.
88 void HandleExternalDataPolicyUpdate(const std::string& user_id,
89 const PolicyMap::Entry* entry);
91 void OnExternalDataFetched(const std::string& user_id,
92 scoped_ptr<std::string> data);
94 // A map from each device-local account user ID to its current policy map
95 // entry for |policy_|.
96 typedef std::map<std::string, PolicyMap::Entry> DeviceLocalAccountEntryMap;
97 DeviceLocalAccountEntryMap device_local_account_entries_;
99 // A map from each logged-in user to the helper that observes |policy_| in the
100 // user's PolicyService.
101 typedef std::map<std::string, linked_ptr<PolicyServiceObserver> >
102 LoggedInUserObserverMap;
103 LoggedInUserObserverMap logged_in_user_observers_;
105 chromeos::CrosSettings* cros_settings_;
106 DeviceLocalAccountPolicyService* device_local_account_policy_service_;
108 // The policy that |this| observes.
109 std::string policy_;
111 Delegate* delegate_;
113 content::NotificationRegistrar notification_registrar_;
114 scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
115 device_local_accounts_subscription_;
117 // A map from user ID to a base::WeakPtr for each external data fetch
118 // currently in progress. This allows fetches to be effectively be canceled by
119 // invalidating the pointers.
120 typedef base::WeakPtrFactory<CloudExternalDataPolicyObserver>
121 WeakPtrFactory;
122 typedef std::map<std::string, linked_ptr<WeakPtrFactory> > FetchWeakPtrMap;
123 FetchWeakPtrMap fetch_weak_ptrs_;
125 base::WeakPtrFactory<CloudExternalDataPolicyObserver> weak_factory_;
127 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserver);
130 } // namespace policy
132 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_H_