summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prefs/profile_pref_store_manager.cc
blob: 6558c4afb65254a13ca360d3952e58c10579e2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/prefs/profile_pref_store_manager.h"

#include "base/bind.h"
#include "base/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/prefs/json_pref_store.h"
#include "base/prefs/persistent_pref_store.h"
#include "base/prefs/pref_registry_simple.h"
#include "chrome/browser/prefs/pref_hash_store_impl.h"
#include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h"
#include "chrome/browser/prefs/tracked/segregated_pref_store.h"
#include "chrome/browser/prefs/tracked/tracked_preferences_migration.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"

namespace {

// An adaptor that allows a PrefHashStoreImpl to access a preference store
// directly as a dictionary. Uses an equivalent layout to
// PrefStoreHashStoreContents.
class DictionaryHashStoreContents : public HashStoreContents {
 public:
  // Instantiates a HashStoreContents that is a copy of |to_copy|. The copy is
  // mutable but does not affect the original, nor is it persisted to disk in
  // any other way.
  explicit DictionaryHashStoreContents(const HashStoreContents& to_copy)
      : hash_store_id_(to_copy.hash_store_id()),
        super_mac_(to_copy.GetSuperMac()) {
    if (to_copy.IsInitialized())
      dictionary_.reset(to_copy.GetContents()->DeepCopy());
    int version = 0;
    if (to_copy.GetVersion(&version))
      version_.reset(new int(version));
  }

  // HashStoreContents implementation
  virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; }

  virtual void Reset() OVERRIDE {
    dictionary_.reset();
    super_mac_.clear();
    version_.reset();
  }

  virtual bool IsInitialized() const OVERRIDE {
    return dictionary_;
  }

  virtual const base::DictionaryValue* GetContents() const OVERRIDE{
    return dictionary_.get();
  }

  virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE {
    return scoped_ptr<MutableDictionary>(
        new SimpleMutableDictionary(this));
  }

  virtual std::string GetSuperMac() const OVERRIDE { return super_mac_; }

  virtual void SetSuperMac(const std::string& super_mac) OVERRIDE {
    super_mac_ = super_mac;
  }

  virtual bool GetVersion(int* version) const OVERRIDE {
    if (!version_)
      return false;
    *version = *version_;
    return true;
  }

  virtual void SetVersion(int version) OVERRIDE {
    version_.reset(new int(version));
  }

  virtual void CommitPendingWrite() OVERRIDE {}

 private:
  class SimpleMutableDictionary
      : public HashStoreContents::MutableDictionary {
   public:
    explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer)
        : outer_(outer) {}

    virtual ~SimpleMutableDictionary() {}

    // MutableDictionary implementation
    virtual base::DictionaryValue* operator->() OVERRIDE {
      if (!outer_->dictionary_)
        outer_->dictionary_.reset(new base::DictionaryValue);
      return outer_->dictionary_.get();
    }

   private:
    DictionaryHashStoreContents* outer_;

    DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary);
  };

  const std::string hash_store_id_;
  std::string super_mac_;
  scoped_ptr<int> version_;
  scoped_ptr<base::DictionaryValue> dictionary_;

  DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents);
};

// An in-memory PrefStore backed by an immutable DictionaryValue.
class DictionaryPrefStore : public PrefStore {
 public:
  explicit DictionaryPrefStore(const base::DictionaryValue* dictionary)
      : dictionary_(dictionary) {}

  virtual bool GetValue(const std::string& key,
                        const base::Value** result) const OVERRIDE {
    const base::Value* tmp = NULL;
    if (!dictionary_->Get(key, &tmp))
      return false;

    if (result)
      *result = tmp;
    return true;
  }

 private:
  virtual ~DictionaryPrefStore() {}

  const base::DictionaryValue* dictionary_;

  DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore);
};

// Waits for a PrefStore to be initialized and then initializes the
// corresponding PrefHashStore.
// The observer deletes itself when its work is completed.
class InitializeHashStoreObserver : public PrefStore::Observer {
 public:
  // Creates an observer that will initialize |pref_hash_store| with the
  // contents of |pref_store| when the latter is fully loaded.
  InitializeHashStoreObserver(
      const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
          tracking_configuration,
      size_t reporting_ids_count,
      const scoped_refptr<PrefStore>& pref_store,
      scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl)
      : tracking_configuration_(tracking_configuration),
        reporting_ids_count_(reporting_ids_count),
        pref_store_(pref_store),
        pref_hash_store_impl_(pref_hash_store_impl.Pass()) {}

  virtual ~InitializeHashStoreObserver();

  // PrefStore::Observer implementation.
  virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
  virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;

 private:
  const std::vector<PrefHashFilter::TrackedPreferenceMetadata>
      tracking_configuration_;
  const size_t reporting_ids_count_;
  scoped_refptr<PrefStore> pref_store_;
  scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl_;

  DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver);
};

InitializeHashStoreObserver::~InitializeHashStoreObserver() {}

void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {}

void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) {
  // If we successfully loaded the preferences _and_ the PrefHashStoreImpl
  // hasn't been initialized by someone else in the meantime, initialize it now.
  const PrefHashStoreImpl::StoreVersion pre_update_version =
      pref_hash_store_impl_->GetCurrentVersion();
  if (succeeded && pre_update_version < PrefHashStoreImpl::VERSION_LATEST) {
    PrefHashFilter(pref_hash_store_impl_.PassAs<PrefHashStore>(),
                   tracking_configuration_,
                   reporting_ids_count_).Initialize(*pref_store_);
    UMA_HISTOGRAM_ENUMERATION(
        "Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom",
        pre_update_version,
        PrefHashStoreImpl::VERSION_LATEST + 1);
  }
  pref_store_->RemoveObserver(this);
  delete this;
}

}  // namespace

// TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved
// out of Local State. This will resolve a race condition on Android and a
// privacy issue on ChromeOS. http://crbug.com/349158
const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking =
#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
    false;
#else
    true;
#endif

ProfilePrefStoreManager::ProfilePrefStoreManager(
    const base::FilePath& profile_path,
    const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
        tracking_configuration,
    size_t reporting_ids_count,
    const std::string& seed,
    const std::string& device_id,
    PrefService* local_state)
    : profile_path_(profile_path),
      tracking_configuration_(tracking_configuration),
      reporting_ids_count_(reporting_ids_count),
      seed_(seed),
      device_id_(device_id),
      local_state_(local_state) {}

ProfilePrefStoreManager::~ProfilePrefStoreManager() {}

// static
void ProfilePrefStoreManager::RegisterPrefs(PrefRegistrySimple* registry) {
  PrefServiceHashStoreContents::RegisterPrefs(registry);
}

// static
void ProfilePrefStoreManager::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  PrefHashFilter::RegisterProfilePrefs(registry);
}

// static
base::FilePath ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
    const base::FilePath& profile_path) {
  return profile_path.Append(chrome::kPreferencesFilename);
}

// static
void ProfilePrefStoreManager::ResetAllPrefHashStores(PrefService* local_state) {
  PrefServiceHashStoreContents::ResetAllPrefHashStores(local_state);
}

//  static
base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) {
  return PrefHashFilter::GetResetTime(pref_service);
}

// static
void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) {
  PrefHashFilter::ClearResetTime(pref_service);
}

void ProfilePrefStoreManager::ResetPrefHashStore() {
  if (kPlatformSupportsPreferenceTracking)
    GetPrefHashStoreImpl()->Reset();
}

PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore(
    const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
  scoped_ptr<PrefFilter> pref_filter;
  if (!kPlatformSupportsPreferenceTracking) {
    return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_),
                             io_task_runner,
                             scoped_ptr<PrefFilter>());
  }

  std::vector<PrefHashFilter::TrackedPreferenceMetadata>
      unprotected_configuration;
  std::vector<PrefHashFilter::TrackedPreferenceMetadata>
      protected_configuration;
  std::set<std::string> protected_pref_names;
  std::set<std::string> unprotected_pref_names;
  for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator
           it = tracking_configuration_.begin();
       it != tracking_configuration_.end();
       ++it) {
    if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) {
      protected_configuration.push_back(*it);
      protected_pref_names.insert(it->name);
    } else {
      unprotected_configuration.push_back(*it);
      unprotected_pref_names.insert(it->name);
    }
  }

  scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter(
      new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(),
                         unprotected_configuration,
                         reporting_ids_count_));
  scoped_ptr<PrefHashFilter> protected_pref_hash_filter(
      new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(),
                         protected_configuration,
                         reporting_ids_count_));

  PrefHashFilter* raw_unprotected_pref_hash_filter =
      unprotected_pref_hash_filter.get();
  PrefHashFilter* raw_protected_pref_hash_filter =
      protected_pref_hash_filter.get();

  scoped_refptr<JsonPrefStore> unprotected_pref_store(
      new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_),
                        io_task_runner,
                        unprotected_pref_hash_filter.PassAs<PrefFilter>()));
  scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore(
      profile_path_.Append(chrome::kProtectedPreferencesFilename),
      io_task_runner,
      protected_pref_hash_filter.PassAs<PrefFilter>()));

  SetupTrackedPreferencesMigration(
      unprotected_pref_names,
      protected_pref_names,
      base::Bind(&JsonPrefStore::RemoveValueSilently,
                 unprotected_pref_store->AsWeakPtr()),
      base::Bind(&JsonPrefStore::RemoveValueSilently,
                 protected_pref_store->AsWeakPtr()),
      base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback,
                 unprotected_pref_store->AsWeakPtr()),
      base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback,
                 protected_pref_store->AsWeakPtr()),
      raw_unprotected_pref_hash_filter,
      raw_protected_pref_hash_filter);

  return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store,
                                 protected_pref_names);
}

void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired(
    const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
  if (!kPlatformSupportsPreferenceTracking)
    return;
  scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl());
  const PrefHashStoreImpl::StoreVersion current_version =
      pref_hash_store_impl->GetCurrentVersion();
  UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion",
                            current_version,
                            PrefHashStoreImpl::VERSION_LATEST + 1);

  // Update the pref hash store if it's not at the latest version.
  if (current_version != PrefHashStoreImpl::VERSION_LATEST) {
    scoped_refptr<JsonPrefStore> pref_store =
        new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_),
                          io_task_runner,
                          scoped_ptr<PrefFilter>());
    pref_store->AddObserver(
        new InitializeHashStoreObserver(tracking_configuration_,
                                        reporting_ids_count_,
                                        pref_store,
                                        pref_hash_store_impl.Pass()));
    pref_store->ReadPrefsAsync(NULL);
  }
}

bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs(
    const base::DictionaryValue& master_prefs) {
  // Create the profile directory if it doesn't exist yet (very possible on
  // first run).
  if (!base::CreateDirectory(profile_path_))
    return false;

  // This will write out to a single combined file which will be immediately
  // migrated to two files on load.
  JSONFileValueSerializer serializer(
      GetPrefFilePathFromProfilePath(profile_path_));

  // Call Serialize (which does IO) on the main thread, which would _normally_
  // be verboten. In this case however, we require this IO to synchronously
  // complete before Chrome can start (as master preferences seed the Local
  // State and Preferences files). This won't trip ThreadIORestrictions as they
  // won't have kicked in yet on the main thread.
  bool success = serializer.Serialize(master_prefs);

  if (success && kPlatformSupportsPreferenceTracking) {
    scoped_refptr<const PrefStore> pref_store(
        new DictionaryPrefStore(&master_prefs));
    PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(),
                   tracking_configuration_,
                   reporting_ids_count_).Initialize(*pref_store);
  }

  UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success);
  return success;
}

PersistentPrefStore*
ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore(
    const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
  scoped_ptr<PrefFilter> pref_filter;
  if (kPlatformSupportsPreferenceTracking) {
    pref_filter.reset(
        new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(),
                           tracking_configuration_,
                           reporting_ids_count_));
  }
  return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_),
                           io_task_runner,
                           pref_filter.Pass());
}

scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() {
  DCHECK(kPlatformSupportsPreferenceTracking);

  return make_scoped_ptr(new PrefHashStoreImpl(
      seed_,
      device_id_,
      scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents(
          profile_path_.AsUTF8Unsafe(), local_state_))));
}