OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/state_store.h" | 5 #include "chrome/browser/extensions/state_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
10 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
11 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
| 13 #include "extensions/browser/extension_registry.h" |
12 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 // Delay, in seconds, before we should open the State Store database. We | 18 // Delay, in seconds, before we should open the State Store database. We |
17 // defer it to avoid slowing down startup. See http://crbug.com/161848 | 19 // defer it to avoid slowing down startup. See http://crbug.com/161848 |
18 const int kInitDelaySeconds = 1; | 20 const int kInitDelaySeconds = 1; |
19 | 21 |
20 std::string GetFullKey(const std::string& extension_id, | 22 std::string GetFullKey(const std::string& extension_id, |
21 const std::string& key) { | 23 const std::string& key) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 ready_ = true; | 61 ready_ = true; |
60 | 62 |
61 for (size_t i = 0; i < pending_tasks_.size(); ++i) | 63 for (size_t i = 0; i < pending_tasks_.size(); ++i) |
62 pending_tasks_[i].Run(); | 64 pending_tasks_[i].Run(); |
63 pending_tasks_.clear(); | 65 pending_tasks_.clear(); |
64 } | 66 } |
65 | 67 |
66 StateStore::StateStore(Profile* profile, | 68 StateStore::StateStore(Profile* profile, |
67 const base::FilePath& db_path, | 69 const base::FilePath& db_path, |
68 bool deferred_load) | 70 bool deferred_load) |
69 : db_path_(db_path), task_queue_(new DelayedTaskQueue()) { | 71 : db_path_(db_path), |
70 registrar_.Add(this, | 72 task_queue_(new DelayedTaskQueue()), |
71 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 73 extension_registry_observer_(this) { |
72 content::Source<Profile>(profile)); | 74 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); |
73 registrar_.Add(this, | |
74 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | |
75 content::Source<Profile>(profile)); | |
76 | 75 |
77 if (deferred_load) { | 76 if (deferred_load) { |
78 // Don't Init until the first page is loaded or the session restored. | 77 // Don't Init until the first page is loaded or the session restored. |
79 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 78 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
80 content::NotificationService:: | 79 content::NotificationService:: |
81 AllBrowserContextsAndSources()); | 80 AllBrowserContextsAndSources()); |
82 registrar_.Add(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE, | 81 registrar_.Add(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE, |
83 content::NotificationService:: | 82 content::NotificationService:: |
84 AllBrowserContextsAndSources()); | 83 AllBrowserContextsAndSources()); |
85 } else { | 84 } else { |
86 Init(); | 85 Init(); |
87 } | 86 } |
88 } | 87 } |
89 | 88 |
90 StateStore::StateStore(Profile* profile, scoped_ptr<ValueStore> value_store) | 89 StateStore::StateStore(Profile* profile, scoped_ptr<ValueStore> value_store) |
91 : store_(value_store.Pass()), task_queue_(new DelayedTaskQueue()) { | 90 : store_(value_store.Pass()), |
92 registrar_.Add(this, | 91 task_queue_(new DelayedTaskQueue()), |
93 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 92 extension_registry_observer_(this) { |
94 content::Source<Profile>(profile)); | 93 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); |
95 registrar_.Add(this, | |
96 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | |
97 content::Source<Profile>(profile)); | |
98 | 94 |
99 // This constructor is for testing. No need to delay Init. | 95 // This constructor is for testing. No need to delay Init. |
100 Init(); | 96 Init(); |
101 } | 97 } |
102 | 98 |
103 StateStore::~StateStore() { | 99 StateStore::~StateStore() { |
104 } | 100 } |
105 | 101 |
106 void StateStore::RegisterKey(const std::string& key) { | 102 void StateStore::RegisterKey(const std::string& key) { |
107 registered_keys_.insert(key); | 103 registered_keys_.insert(key); |
(...skipping 21 matching lines...) Expand all Loading... |
129 task_queue_->InvokeWhenReady( | 125 task_queue_->InvokeWhenReady( |
130 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), | 126 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
131 GetFullKey(extension_id, key))); | 127 GetFullKey(extension_id, key))); |
132 } | 128 } |
133 | 129 |
134 bool StateStore::IsInitialized() const { return task_queue_->ready(); } | 130 bool StateStore::IsInitialized() const { return task_queue_->ready(); } |
135 | 131 |
136 void StateStore::Observe(int type, | 132 void StateStore::Observe(int type, |
137 const content::NotificationSource& source, | 133 const content::NotificationSource& source, |
138 const content::NotificationDetails& details) { | 134 const content::NotificationDetails& details) { |
139 switch (type) { | 135 DCHECK(type == chrome::NOTIFICATION_SESSION_RESTORE_DONE || |
140 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: | 136 type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME); |
141 RemoveKeysForExtension( | 137 registrar_.RemoveAll(); |
142 content::Details<const InstalledExtensionInfo>(details)->extension-> | 138 |
143 id()); | 139 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
144 break; | 140 base::Bind(&StateStore::Init, AsWeakPtr()), |
145 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: | 141 base::TimeDelta::FromSeconds(kInitDelaySeconds)); |
146 RemoveKeysForExtension( | 142 } |
147 content::Details<const Extension>(details)->id()); | 143 |
148 break; | 144 void StateStore::OnExtensionWillBeInstalled( |
149 case chrome::NOTIFICATION_SESSION_RESTORE_DONE: | 145 content::BrowserContext* browser_context, |
150 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: | 146 const Extension* extension, |
151 registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 147 bool is_update, |
152 content::NotificationService::AllSources()); | 148 bool from_ephemeral, |
153 registrar_.Remove(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE, | 149 const std::string& old_name) { |
154 content::NotificationService::AllSources()); | 150 RemoveKeysForExtension(extension->id()); |
155 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 151 } |
156 base::Bind(&StateStore::Init, AsWeakPtr()), | 152 |
157 base::TimeDelta::FromSeconds(kInitDelaySeconds)); | 153 void StateStore::OnExtensionUninstalled( |
158 break; | 154 content::BrowserContext* browser_context, |
159 default: | 155 const Extension* extension) { |
160 NOTREACHED(); | 156 RemoveKeysForExtension(extension->id()); |
161 return; | |
162 } | |
163 } | 157 } |
164 | 158 |
165 void StateStore::Init() { | 159 void StateStore::Init() { |
166 if (!db_path_.empty()) | 160 if (!db_path_.empty()) |
167 store_.Init(db_path_); | 161 store_.Init(db_path_); |
168 task_queue_->SetReady(); | 162 task_queue_->SetReady(); |
169 } | 163 } |
170 | 164 |
171 void StateStore::RemoveKeysForExtension(const std::string& extension_id) { | 165 void StateStore::RemoveKeysForExtension(const std::string& extension_id) { |
172 for (std::set<std::string>::iterator key = registered_keys_.begin(); | 166 for (std::set<std::string>::iterator key = registered_keys_.begin(); |
173 key != registered_keys_.end(); ++key) { | 167 key != registered_keys_.end(); ++key) { |
174 task_queue_->InvokeWhenReady( | 168 task_queue_->InvokeWhenReady( |
175 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), | 169 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
176 GetFullKey(extension_id, *key))); | 170 GetFullKey(extension_id, *key))); |
177 } | 171 } |
178 } | 172 } |
179 | 173 |
180 } // namespace extensions | 174 } // namespace extensions |
OLD | NEW |