OLD | NEW |
---|---|
(Empty) | |
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_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_CHROM EOS_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_CHROM EOS_H_ | |
Joao da Silva
2013/11/27 14:33:59
The _chromeos part of the filename is a bit redund
bartfab (slow)
2013/11/27 20:10:15
I modeled this after things like device_cloud_poli
| |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
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" | |
21 | |
22 namespace chromeos { | |
23 class UserManager; | |
24 } | |
25 | |
26 namespace policy { | |
27 | |
28 // Helper for implementing policies referencing external data: This class | |
29 // observes a given |policy_| and fetches the external data that it references | |
30 // for all users on the device. Notifications are emitted when an external data | |
31 // reference is set, cleared or an external data fetch completes successfully. | |
32 // | |
33 // State is kept at runtime only: External data references that already exist | |
34 // when the class is instantiated are considered new, causing a notification to | |
35 // be emitted that an external data reference has been set and the referenced | |
36 // external data to be fetched. | |
37 class CloudExternalDataPolicyObserverChromeOS | |
38 : public content::NotificationObserver, | |
39 public DeviceLocalAccountPolicyService::Observer { | |
40 public: | |
41 CloudExternalDataPolicyObserverChromeOS( | |
42 chromeos::CrosSettings* cros_settings, | |
43 chromeos::UserManager* user_manager, | |
44 DeviceLocalAccountPolicyService* device_local_account_policy_service, | |
45 const std::string& policy); | |
46 virtual ~CloudExternalDataPolicyObserverChromeOS(); | |
47 | |
48 void Init(); | |
49 | |
50 virtual void Shutdown(); | |
Joao da Silva
2013/11/27 14:33:59
Who is going to own this? When is this going to be
bartfab (slow)
2013/11/27 20:10:15
No longer relevant - there is a Delegate interface
| |
51 | |
52 // Invoked when an external data reference is set for |user_id|. | |
53 virtual void OnExternalDataSet(const std::string& user_id) = 0; | |
54 | |
55 // Invoked when the external data reference is cleared for |user_id|. | |
56 virtual void OnExternalDataCleared(const std::string& user_id) = 0; | |
57 | |
58 // Invoked when the external data referenced for |user_id| has been fetched. | |
59 // Failed fetches are retried and the method is called only when a fetch | |
60 // eventually succeeds. If a fetch fails permanently (e.g. because the | |
61 // external data reference specifies an invalid URL), the method is not called | |
62 // at all. | |
63 virtual void OnExternalDataFetched(const std::string& user_id, | |
64 scoped_ptr<std::string> data) = 0; | |
Joao da Silva
2013/11/27 14:33:59
WDYT of putting these in a Delegate interface inst
bartfab (slow)
2013/11/27 20:10:15
Done.
| |
65 | |
66 // content::NotificationObserver: | |
67 virtual void Observe(int type, | |
68 const content::NotificationSource& source, | |
69 const content::NotificationDetails& details) OVERRIDE; | |
70 | |
71 // DeviceLocalAccountPolicyService: | |
72 virtual void OnPolicyUpdated(const std::string& user_id) OVERRIDE; | |
73 virtual void OnDeviceLocalAccountsChanged() OVERRIDE; | |
74 | |
75 private: | |
76 // Helper class that observes |policy_| for a logged-in user. | |
77 class PolicyServiceObserver; | |
78 | |
79 void RetrieveDeviceLocalAccounts(); | |
80 | |
81 // Handles the new policy map |entry| for |user_id| by canceling any external | |
82 // data fetch currently in progress, emitting a notification that an external | |
83 // data reference has been cleared (if |entry| is NULL) or set (otherwise), | |
84 // starting a new external data fetch in the latter case. | |
85 void HandleExternalDataPolicyUpdate(const std::string& user_id, | |
86 const PolicyMap::Entry* entry); | |
87 | |
88 // A map from each device-local account user ID to its current policy map | |
89 // entry for |policy_|. | |
90 typedef std::map<std::string, PolicyMap::Entry> DeviceLocalAccountEntryMap; | |
91 DeviceLocalAccountEntryMap device_local_account_entries_; | |
92 | |
93 // A map from each logged-in user to the helper that observes |policy_| in the | |
94 // user's PolicyService. | |
95 typedef std::map<std::string, linked_ptr<PolicyServiceObserver> > | |
Joao da Silva
2013/11/27 14:33:59
I've been asked to use maps of linked_ptrs before;
bartfab (slow)
2013/11/27 20:10:15
I prefer linked_ptrs: The overhead is negligible h
| |
96 LoggedInUserObserverMap; | |
97 LoggedInUserObserverMap logged_in_user_observers_; | |
98 | |
99 chromeos::CrosSettings* cros_settings_; | |
100 chromeos::UserManager* user_manager_; | |
101 DeviceLocalAccountPolicyService* device_local_account_policy_service_; | |
102 | |
103 // The policy that |this| observes. | |
104 std::string policy_; | |
105 | |
106 content::NotificationRegistrar notification_registrar_; | |
107 scoped_ptr<chromeos::CrosSettings::ObserverSubscription> | |
108 device_local_accounts_subscription_; | |
109 | |
110 // A map from user ID to a base::WeakPtr for each external data fetch | |
111 // currently in progress. This allows fetches to be effectively be canceled by | |
112 // invalidating the pointers. | |
113 typedef base::WeakPtrFactory<CloudExternalDataPolicyObserverChromeOS> | |
114 WeakPtrFactory; | |
115 typedef std::map<std::string, linked_ptr<WeakPtrFactory> > FetchWeakPtrMap; | |
116 FetchWeakPtrMap fetch_weak_ptrs_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverChromeOS); | |
119 }; | |
120 | |
121 } // namespace policy | |
122 | |
123 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_POLICY_OBSERVER_CH ROMEOS_H_ | |
OLD | NEW |