rules_cache_delegate.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
6#define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
7
8#include <set>
9#include <string>
10
11#include "base/gtest_prod_util.h"
12#include "base/values.h"
13#include "content/public/browser/browser_thread.h"
14
15namespace content {
16class BrowserContext;
17}
18
19namespace extensions {
20
21class RulesRegistry;
22
23// RulesCacheDelegate implements the part of the RulesRegistry which works on
24// the UI thread. It should only be used on the UI thread.
25// If |log_storage_init_delay| is set, the delay caused by loading and
26// registering rules on initialization will be logged with UMA.
27class RulesCacheDelegate {
28 public:
29
30  explicit RulesCacheDelegate(bool log_storage_init_delay);
31
32  virtual ~RulesCacheDelegate();
33
34  // Returns a key for the state store. The associated preference is a boolean
35  // indicating whether there are some declarative rules stored in the rule
36  // store.
37  static std::string GetRulesStoredKey(const std::string& event_name,
38                                       bool incognito);
39
40  // Initialize the storage functionality.
41  void Init(RulesRegistry* registry);
42
43  void WriteToStorage(const std::string& extension_id,
44                      scoped_ptr<base::Value> value);
45
46  base::WeakPtr<RulesCacheDelegate> GetWeakPtr() {
47    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
48    return weak_ptr_factory_.GetWeakPtr();
49  }
50
51 private:
52  FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest,
53                           DeclarativeRulesStored);
54  FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest,
55                           RulesStoredFlagMultipleRegistries);
56
57  static const char kRulesStoredKey[];
58
59  // Check if we are done reading all data from storage on startup, and notify
60  // the RulesRegistry on its thread if so. The notification is delivered
61  // exactly once.
62  void CheckIfReady();
63
64  // Schedules retrieving rules for already loaded extensions where
65  // appropriate.
66  void ReadRulesForInstalledExtensions();
67
68  // Read/write a list of rules serialized to Values.
69  void ReadFromStorage(const std::string& extension_id);
70  void ReadFromStorageCallback(const std::string& extension_id,
71                               scoped_ptr<base::Value> value);
72
73  // Check the preferences whether the extension with |extension_id| has some
74  // rules stored on disk. If this information is not in the preferences, true
75  // is returned as a safe default value.
76  bool GetDeclarativeRulesStored(const std::string& extension_id) const;
77  // Modify the preference to |rules_stored|.
78  void SetDeclarativeRulesStored(const std::string& extension_id,
79                                 bool rules_stored);
80
81  content::BrowserContext* browser_context_;
82
83  // The key under which rules are stored.
84  std::string storage_key_;
85
86  // The key under which we store whether the rules have been stored.
87  std::string rules_stored_key_;
88
89  // A set of extension IDs that have rules we are reading from storage.
90  std::set<std::string> waiting_for_extensions_;
91
92  // We measure the time spent on loading rules on init. The result is logged
93  // with UMA once per each RulesCacheDelegate instance, unless in Incognito.
94  base::Time storage_init_time_;
95  bool log_storage_init_delay_;
96
97  // Weak pointer to post tasks to the owning rules registry.
98  base::WeakPtr<RulesRegistry> registry_;
99
100  // The thread |registry_| lives on.
101  content::BrowserThread::ID rules_registry_thread_;
102
103  // We notified the RulesRegistry that the rules are loaded.
104  bool notified_registry_;
105
106  // Use this factory to generate weak pointers bound to the UI thread.
107  base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_;
108};
109
110}  // namespace extensions
111
112#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
113