Home | History | Annotate | Download | only in settings
      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_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_
      6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/callback_forward.h"
     15 #include "base/gtest_prod_util.h"
     16 #include "base/memory/weak_ptr.h"
     17 #include "base/prefs/pref_value_map.h"
     18 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
     19 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     20 #include "chromeos/network/network_state_handler_observer.h"
     21 #include "chromeos/settings/cros_settings_provider.h"
     22 
     23 namespace base {
     24 class Value;
     25 }
     26 
     27 namespace enterprise_management {
     28 class ChromeDeviceSettingsProto;
     29 }  // namespace enterprise_management
     30 
     31 namespace chromeos {
     32 
     33 // CrosSettingsProvider implementation that works with device settings.
     34 class DeviceSettingsProvider : public CrosSettingsProvider,
     35                                public DeviceSettingsService::Observer,
     36                                public NetworkStateHandlerObserver {
     37  public:
     38   DeviceSettingsProvider(const NotifyObserversCallback& notify_cb,
     39                          DeviceSettingsService* device_settings_service);
     40   virtual ~DeviceSettingsProvider();
     41 
     42   // Returns true if |path| is handled by this provider.
     43   static bool IsDeviceSetting(const std::string& name);
     44 
     45   // CrosSettingsProvider implementation.
     46   virtual const base::Value* Get(const std::string& path) const OVERRIDE;
     47   virtual TrustedStatus PrepareTrustedValues(
     48       const base::Closure& callback) OVERRIDE;
     49   virtual bool HandlesSetting(const std::string& path) const OVERRIDE;
     50 
     51   // NetworkStateHandlerObserver implementation.
     52   virtual void DeviceListChanged() OVERRIDE;
     53 
     54  private:
     55   // CrosSettingsProvider implementation:
     56   virtual void DoSet(const std::string& path,
     57                      const base::Value& value) OVERRIDE;
     58 
     59   // DeviceSettingsService::Observer implementation:
     60   virtual void OwnershipStatusChanged() OVERRIDE;
     61   virtual void DeviceSettingsUpdated() OVERRIDE;
     62 
     63   // Populates in-memory cache from the local_state cache that is used to store
     64   // device settings before the device is owned and to speed up policy
     65   // availability before the policy blob is fetched on boot.
     66   void RetrieveCachedData();
     67 
     68   // Stores a value from the |pending_changes_| queue in the device settings.
     69   // If the device is not owned yet the data ends up only in the local_state
     70   // cache and is serialized once ownership is acquired.
     71   void SetInPolicy();
     72 
     73   // Decode the various groups of policies.
     74   void DecodeLoginPolicies(
     75       const enterprise_management::ChromeDeviceSettingsProto& policy,
     76       PrefValueMap* new_values_cache) const;
     77   void DecodeKioskPolicies(
     78       const enterprise_management::ChromeDeviceSettingsProto& policy,
     79       PrefValueMap* new_values_cache) const;
     80   void DecodeNetworkPolicies(
     81       const enterprise_management::ChromeDeviceSettingsProto& policy,
     82       PrefValueMap* new_values_cache) const;
     83   void DecodeAutoUpdatePolicies(
     84       const enterprise_management::ChromeDeviceSettingsProto& policy,
     85       PrefValueMap* new_values_cache) const;
     86   void DecodeReportingPolicies(
     87       const enterprise_management::ChromeDeviceSettingsProto& policy,
     88       PrefValueMap* new_values_cache) const;
     89   void DecodeGenericPolicies(
     90       const enterprise_management::ChromeDeviceSettingsProto& policy,
     91       PrefValueMap* new_values_cache) const;
     92 
     93   // Parses the policy data and fills in |values_cache_|.
     94   void UpdateValuesCache(
     95       const enterprise_management::PolicyData& policy_data,
     96       const enterprise_management::ChromeDeviceSettingsProto& settings,
     97       TrustedStatus trusted_status);
     98 
     99   // Applies the metrics policy and if not set migrates the legacy file.
    100   void ApplyMetricsSetting(bool use_file, bool new_value);
    101 
    102   // Applies the data roaming policy.
    103   void ApplyRoamingSetting(bool new_value);
    104   void ApplyRoamingSettingFromProto(
    105       const enterprise_management::ChromeDeviceSettingsProto& settings);
    106 
    107   // Applies any changes of the policies that are not handled by the respective
    108   // subsystems.
    109   void ApplySideEffects(
    110       const enterprise_management::ChromeDeviceSettingsProto& settings);
    111 
    112   // In case of missing policy blob we should verify if this is upgrade of
    113   // machine owned from pre version 12 OS and the user never touched the device
    114   // settings. In this case revert to defaults and let people in until the owner
    115   // comes and changes that.
    116   bool MitigateMissingPolicy();
    117 
    118   // Checks if the current cache value can be trusted for being representative
    119   // for the disk cache.
    120   TrustedStatus RequestTrustedEntity();
    121 
    122   // Invokes UpdateFromService() to synchronize with |device_settings_service_|,
    123   // then triggers the next store operation if applicable.
    124   void UpdateAndProceedStoring();
    125 
    126   // Re-reads state from |device_settings_service_|, adjusts
    127   // |trusted_status_| and calls UpdateValuesCache() if applicable. Returns true
    128   // if new settings have been loaded.
    129   bool UpdateFromService();
    130 
    131   // Sends |device_settings_| to |device_settings_service_| for signing and
    132   // storage in session_manager.
    133   void StoreDeviceSettings();
    134 
    135   // Checks the current ownership status to see whether the device owner is
    136   // logged in and writes the data accumulated in |migration_values_| to proper
    137   // device settings.
    138   void AttemptMigration();
    139 
    140   // Pending callbacks that need to be invoked after settings verification.
    141   std::vector<base::Closure> callbacks_;
    142 
    143   DeviceSettingsService* device_settings_service_;
    144   mutable PrefValueMap migration_values_;
    145 
    146   TrustedStatus trusted_status_;
    147   DeviceSettingsService::OwnershipStatus ownership_status_;
    148 
    149   // The device settings as currently reported through the CrosSettingsProvider
    150   // interface. This may be different from the actual current device settings
    151   // (which can be obtained from |device_settings_service_|) in case the device
    152   // does not have an owner yet or there are pending changes that have not yet
    153   // been written to session_manager.
    154   enterprise_management::ChromeDeviceSettingsProto device_settings_;
    155 
    156   // A cache of values, indexed by the settings keys served through the
    157   // CrosSettingsProvider interface. This is always kept in sync with the raw
    158   // data found in |device_settings_|.
    159   PrefValueMap values_cache_;
    160 
    161   // This is a queue for set requests, because those need to be sequential.
    162   typedef std::pair<std::string, base::Value*> PendingQueueElement;
    163   std::deque<PendingQueueElement> pending_changes_;
    164 
    165   // Weak pointer factory for creating store operation callbacks.
    166   base::WeakPtrFactory<DeviceSettingsProvider> store_callback_factory_;
    167 
    168   friend class DeviceSettingsProviderTest;
    169   FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
    170                            InitializationTestUnowned);
    171   FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
    172                            PolicyFailedPermanentlyNotification);
    173   FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, PolicyLoadNotification);
    174   DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProvider);
    175 };
    176 
    177 }  // namespace chromeos
    178 
    179 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_
    180