OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
7 | 7 |
8 #include <map> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include "base/bind.h" | 10 |
11 #include "base/callback.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/values.h" |
13 #include "base/stl_util.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "base/timer/timer.h" | |
15 #include "chrome/browser/extensions/api/image_writer_private/image_writer_privat e_api.h" | |
16 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | |
17 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | |
18 #include "chrome/browser/profiles/profile.h" | |
19 #include "chrome/common/extensions/api/image_writer_private.h" | |
20 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
22 #include "url/gurl.h" | |
23 | 16 |
24 namespace image_writer_api = extensions::api::image_writer_private; | 17 class Profile; |
25 | 18 |
26 namespace extensions { | 19 namespace extensions { |
27 namespace image_writer { | |
28 | 20 |
29 class Operation; | 21 class RulesRegistry; |
30 | 22 |
31 // Manages image writer operations for the current profile. Including clean-up | 23 // RulesCacheDelegate implements the part of the RulesRegistry which works on |
32 // and message routing. | 24 // the UI thread. It should only be used on the UI thread. It gets created |
33 class OperationManager | 25 // by the RulesRegistry, but right after that it changes owner to the |
34 : public ProfileKeyedAPI, | 26 // RulesRegistryService, and is deleted by the service. |
Jeffrey Yasskin
2013/10/30 22:30:48
Although note that if the tests use TestBrowserThr
Fady Samuel
2013/10/31 14:02:24
In a subsequent patch, RulesCacheDelegate will be
| |
35 public content::NotificationObserver, | 27 // If |log_storage_init_delay| is set, the delay caused by loading and |
36 public base::SupportsWeakPtr<OperationManager> { | 28 // registering rules on initialization will be logged with UMA. |
37 public: | 29 class RulesCacheDelegate : public content::NotificationObserver { |
38 typedef std::string ExtensionId; | 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 | 39 |
40 explicit OperationManager(Profile* profile); | 40 virtual ~RulesCacheDelegate(); |
41 virtual ~OperationManager(); | |
42 | 41 |
43 virtual void Shutdown() OVERRIDE; | 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); | |
44 | 47 |
45 // Starts a WriteFromUrl operation. | 48 // Initialize the storage functionality. |
46 void StartWriteFromUrl(const ExtensionId& extension_id, | 49 void Init(); |
47 GURL url, | |
48 content::RenderViewHost* rvh, | |
49 const std::string& hash, | |
50 bool saveImageAsDownload, | |
51 const std::string& storage_unit_id, | |
52 const Operation::StartWriteCallback& callback); | |
53 | 50 |
54 // Starts a WriteFromFile operation. | 51 void WriteToStorage(const std::string& extension_id, |
55 void StartWriteFromFile(const ExtensionId& extension_id, | 52 scoped_ptr<base::Value> value); |
56 const std::string& storage_unit_id, | |
57 const Operation::StartWriteCallback& callback); | |
58 | 53 |
59 // Cancels the extensions current operation if any. | 54 base::WeakPtr<RulesCacheDelegate> GetWeakPtr() { |
60 void CancelWrite(const ExtensionId& extension_id, | 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
61 const Operation::CancelWriteCallback& callback); | 56 return weak_ptr_factory_.GetWeakPtr(); |
57 } | |
62 | 58 |
63 // Callback for progress events. | 59 private: |
64 void OnProgress(const ExtensionId& extension_id, | 60 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
65 image_writer_api::Stage stage, | 61 DeclarativeRulesStored); |
66 int progress); | 62 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
67 // Callback for completion events. | 63 RulesStoredFlagMultipleRegistries); |
68 void OnComplete(const ExtensionId& extension_id); | |
69 | 64 |
70 // Callback for error events. | 65 static const char kRulesStoredKey[]; |
71 // TODO (haven): Add error codes. | |
72 void OnError(const ExtensionId& extension_id, | |
73 image_writer_api::Stage stage, | |
74 int progress, | |
75 const std::string& error_message); | |
76 | |
77 // ProfileKeyedAPI | |
78 static ProfileKeyedAPIFactory<OperationManager>* | |
79 GetFactoryInstance(); | |
80 static OperationManager* Get(Profile* profile); | |
81 | |
82 Profile* profile() { return profile_; } | |
83 | |
84 private: | |
85 | |
86 static const char* service_name() { | |
87 return "OperationManager"; | |
88 } | |
89 | 66 |
90 // NotificationObserver | 67 // NotificationObserver |
91 virtual void Observe(int type, | 68 virtual void Observe(int type, |
92 const content::NotificationSource& source, | 69 const content::NotificationSource& source, |
93 const content::NotificationDetails& details) OVERRIDE; | 70 const content::NotificationDetails& details) OVERRIDE; |
94 | 71 |
95 Operation* GetOperation(const ExtensionId& extension_id); | 72 // Check if we are done reading all data from storage on startup, and notify |
96 void DeleteOperation(const ExtensionId& extension_id); | 73 // the RulesRegistry on its thread if so. The notification is delivered |
74 // exactly once. | |
75 void CheckIfReady(); | |
97 | 76 |
98 friend class ProfileKeyedAPIFactory<OperationManager>; | 77 // Schedules retrieving rules for already loaded extensions where |
99 typedef std::map<ExtensionId, scoped_refptr<Operation> > OperationMap; | 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_; | |
100 | 95 |
101 Profile* profile_; | 96 Profile* profile_; |
102 OperationMap operations_; | |
103 content::NotificationRegistrar registrar_; | |
104 | 97 |
105 base::WeakPtrFactory<OperationManager> weak_factory_; | 98 // The key under which rules are stored. |
99 const std::string storage_key_; | |
106 | 100 |
107 DISALLOW_COPY_AND_ASSIGN(OperationManager); | 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_; | |
108 }; | 123 }; |
109 | 124 |
110 } // namespace image_writer | |
111 } // namespace extensions | 125 } // namespace extensions |
112 | 126 |
113 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_ H_ | 127 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
OLD | NEW |