Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_cache_delegate.h

Issue 49693003: Refactor RulesRegistryWithCache to RulesRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tests mostly work Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
6 #define CHROME_BROWSER_EXTENSIONS_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 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16
17 class Profile;
18
19 namespace extensions {
20
21 class 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. It gets created
25 // by the RulesRegistry, but right after that it changes owner to the
26 // RulesRegistryService, and is deleted by the service.
27 // If |log_storage_init_delay| is set, the delay caused by loading and
28 // registering rules on initialization will be logged with UMA.
29 class RulesCacheDelegate : public content::NotificationObserver {
30 public:
31 // |event_name| identifies the JavaScript event for which rules are
32 // registered. For example, for WebRequestRulesRegistry the name is
33 // "declarativeWebRequest.onRequest".
34 RulesCacheDelegate(Profile* profile,
35 const std::string& event_name,
36 content::BrowserThread::ID rules_registry_thread,
37 base::WeakPtr<RulesRegistry> registry,
38 bool log_storage_init_delay);
39
40 virtual ~RulesCacheDelegate();
41
42 // Returns a key for the state store. The associated preference is a boolean
43 // indicating whether there are some declarative rules stored in the rule
44 // store.
45 static std::string GetRulesStoredKey(const std::string& event_name,
46 bool incognito);
47
48 // Initialize the storage functionality.
49 void Init();
50
51 void WriteToStorage(const std::string& extension_id,
52 scoped_ptr<base::Value> value);
53
54 base::WeakPtr<RulesCacheDelegate> GetWeakPtr() {
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
56 return weak_ptr_factory_.GetWeakPtr();
57 }
58
59 private:
60 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest,
61 DeclarativeRulesStored);
62 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest,
63 RulesStoredFlagMultipleRegistries);
64
65 static const char kRulesStoredKey[];
66
67 // NotificationObserver
68 virtual void Observe(int type,
69 const content::NotificationSource& source,
70 const content::NotificationDetails& details) OVERRIDE;
71
72 // Check if we are done reading all data from storage on startup, and notify
73 // the RulesRegistry on its thread if so. The notification is delivered
74 // exactly once.
75 void CheckIfReady();
76
77 // Schedules retrieving rules for already loaded extensions where
78 // appropriate.
79 void ReadRulesForInstalledExtensions();
80
81 // Read/write a list of rules serialized to Values.
82 void ReadFromStorage(const std::string& extension_id);
83 void ReadFromStorageCallback(const std::string& extension_id,
84 scoped_ptr<base::Value> value);
85
86 // Check the preferences whether the extension with |extension_id| has some
87 // rules stored on disk. If this information is not in the preferences, true
88 // is returned as a safe default value.
89 bool GetDeclarativeRulesStored(const std::string& extension_id) const;
90 // Modify the preference to |rules_stored|.
91 void SetDeclarativeRulesStored(const std::string& extension_id,
92 bool rules_stored);
93
94 content::NotificationRegistrar registrar_;
95
96 Profile* profile_;
97
98 // The key under which rules are stored.
99 const std::string storage_key_;
100
101 // The key under which we store whether the rules have been stored.
102 const std::string rules_stored_key_;
103
104 // A set of extension IDs that have rules we are reading from storage.
105 std::set<std::string> waiting_for_extensions_;
106
107 // We measure the time spent on loading rules on init. The result is logged
108 // with UMA once per each RulesCacheDelegate instance, unless in Incognito.
109 base::Time storage_init_time_;
110 bool log_storage_init_delay_;
111
112 // Weak pointer to post tasks to the owning rules registry.
113 const base::WeakPtr<RulesRegistry> registry_;
114
115 // The thread |registry_| lives on.
116 const content::BrowserThread::ID rules_registry_thread_;
117
118 // We notified the RulesRegistry that the rules are loaded.
119 bool notified_registry_;
120
121 // Use this factory to generate weak pointers bound to the UI thread.
122 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_;
123 };
124
125 } // namespace extensions
126
127 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698