summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/protected_prefs_watcher.h
blob: 46cf5ca0858d92278bb12be1e27697781c046d8d (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
// Copyright (c) 2012 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.

#ifndef CHROME_BROWSER_PROTECTOR_PROTECTED_PREFS_WATCHER_H_
#define CHROME_BROWSER_PROTECTOR_PROTECTED_PREFS_WATCHER_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "content/public/browser/notification_observer.h"

class PrefService;
class PrefSetObserver;
class Profile;

namespace base {
class Value;
}

namespace protector {

class ProtectedPrefsWatcher : public content::NotificationObserver {
 public:
  // Current backup version.
  static const int kCurrentVersionNumber;

  explicit ProtectedPrefsWatcher(Profile* profile);
  virtual ~ProtectedPrefsWatcher();

  // Registers prefs on a new Profile instance.
  static void RegisterUserPrefs(PrefService* prefs);

  // Returns true if pref named |path| has changed and the backup is valid.
  bool DidPrefChange(const std::string& path) const;

  // Returns the backup value for pref named |path| or |NULL| if the pref is not
  // protected, does not exist or the backup is invalid. The returned Value
  // instance is owned by the PrefService.
  const base::Value* GetBackupForPref(const std::string& path) const;

  // Forces a valid backup, matching actual preferences (overwriting any
  // previous data). Should only be when protector service is disabled.
  void ForceUpdateBackup();

  // True if the backup was valid at the profile load time.
  bool is_backup_valid() { return is_backup_valid_; }

 private:
  friend class ProtectedPrefsWatcherTest;

  // content::NotificationObserver overrides:
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // Makes sure that all protected prefs have been migrated before starting to
  // observe them.
  void EnsurePrefsMigration();

  // Updates cached prefs from their actual values and returns |true| if there
  // were any changes.
  bool UpdateCachedPrefs();

  // Returns |false| if profile does not have a backup yet (needs to be
  // initialized).
  bool HasBackup() const;

  // Creates initial backup entries.
  void InitBackup();

  // Migrates backup if it is an older version.
  void MigrateOldBackupIfNeeded();

  // Updates the backup entry for |path| and returns |true| if the backup has
  // changed.
  bool UpdateBackupEntry(const std::string& path);

  // Perform a check that backup is valid and settings have not been modified.
  void ValidateBackup();

  // Returns |true| if backup signature is valid.
  bool IsSignatureValid() const;

  // Updates the backup signature.
  void UpdateBackupSignature();

  // Returns data to be signed as string.
  std::string GetSignatureData(PrefService* prefs) const;

  // Cached set of extension IDs. They are not changed as frequently
  ExtensionPrefs::ExtensionIdSet cached_extension_ids_;

  scoped_ptr<PrefSetObserver> pref_observer_;

  // True if the backup was valid at the profile load time.
  bool is_backup_valid_;

  Profile* profile_;

  DISALLOW_COPY_AND_ASSIGN(ProtectedPrefsWatcher);
};

}  // namespace protector

#endif  // CHROME_BROWSER_PROTECTOR_PROTECTED_PREFS_WATCHER_H_