OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/settings/settings_frontend.h" | 5 #include "chrome/browser/extensions/settings/settings_frontend.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "chrome/browser/extensions/extension_event_names.h" | 9 #include "chrome/browser/extensions/extension_event_names.h" |
10 #include "chrome/browser/extensions/extension_event_router.h" | 10 #include "chrome/browser/extensions/extension_event_router.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/settings/settings_backend.h" | 12 #include "chrome/browser/extensions/settings/settings_backend.h" |
| 13 #include "chrome/browser/extensions/settings/settings_leveldb_storage.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
16 | 17 |
17 namespace extensions { | 18 namespace extensions { |
18 | 19 |
19 using content::BrowserThread; | 20 using content::BrowserThread; |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 struct Backends { | 24 struct Backends { |
24 Backends( | 25 Backends( |
| 26 // Ownership taken. |
| 27 SettingsStorageFactory* storage_factory, |
25 const FilePath& profile_path, | 28 const FilePath& profile_path, |
26 const scoped_refptr<SettingsObserverList>& observers) | 29 const scoped_refptr<SettingsObserverList>& observers) |
27 : extensions_backend_( | 30 : storage_factory_(storage_factory), |
| 31 extensions_backend_( |
| 32 storage_factory, |
28 profile_path.AppendASCII( | 33 profile_path.AppendASCII( |
29 ExtensionService::kExtensionSettingsDirectoryName), | 34 ExtensionService::kExtensionSettingsDirectoryName), |
30 observers), | 35 observers), |
31 apps_backend_( | 36 apps_backend_( |
| 37 storage_factory, |
32 profile_path.AppendASCII( | 38 profile_path.AppendASCII( |
33 ExtensionService::kAppSettingsDirectoryName), | 39 ExtensionService::kAppSettingsDirectoryName), |
34 observers) {} | 40 observers) {} |
35 | 41 |
| 42 scoped_ptr<SettingsStorageFactory> storage_factory_; |
36 SettingsBackend extensions_backend_; | 43 SettingsBackend extensions_backend_; |
37 SettingsBackend apps_backend_; | 44 SettingsBackend apps_backend_; |
38 }; | 45 }; |
39 | 46 |
40 static void CallbackWithExtensionsBackend( | 47 static void CallbackWithExtensionsBackend( |
41 const SettingsFrontend::SyncableServiceCallback& callback, | 48 const SettingsFrontend::SyncableServiceCallback& callback, |
42 Backends* backends) { | 49 Backends* backends) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
44 callback.Run(&backends->extensions_backend_); | 51 callback.Run(&backends->extensions_backend_); |
45 } | 52 } |
(...skipping 29 matching lines...) Expand all Loading... |
75 | 82 |
76 void DeleteStorageOnFileThread( | 83 void DeleteStorageOnFileThread( |
77 const std::string& extension_id, Backends* backends) { | 84 const std::string& extension_id, Backends* backends) { |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
79 backends->extensions_backend_.DeleteStorage(extension_id); | 86 backends->extensions_backend_.DeleteStorage(extension_id); |
80 backends->apps_backend_.DeleteStorage(extension_id); | 87 backends->apps_backend_.DeleteStorage(extension_id); |
81 } | 88 } |
82 | 89 |
83 } // namespace | 90 } // namespace |
84 | 91 |
85 // DefaultObserver. | 92 // DefaultObserver |
86 | 93 |
87 SettingsFrontend::DefaultObserver::DefaultObserver(Profile* profile) | 94 SettingsFrontend::DefaultObserver::DefaultObserver(Profile* profile) |
88 : profile_(profile) {} | 95 : profile_(profile) {} |
89 | 96 |
90 SettingsFrontend::DefaultObserver::~DefaultObserver() {} | 97 SettingsFrontend::DefaultObserver::~DefaultObserver() {} |
91 | 98 |
92 void SettingsFrontend::DefaultObserver::OnSettingsChanged( | 99 void SettingsFrontend::DefaultObserver::OnSettingsChanged( |
93 const std::string& extension_id, const std::string& changes_json) { | 100 const std::string& extension_id, const std::string& changes_json) { |
94 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 101 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
95 extension_id, | 102 extension_id, |
96 extension_event_names::kOnSettingsChanged, | 103 extension_event_names::kOnSettingsChanged, |
97 // This is the list of function arguments to pass to the onChanged | 104 // This is the list of function arguments to pass to the onChanged |
98 // handler of extensions, a single argument with the list of changes. | 105 // handler of extensions, a single argument with the list of changes. |
99 std::string("[") + changes_json + "]", | 106 std::string("[") + changes_json + "]", |
100 NULL, | 107 NULL, |
101 GURL()); | 108 GURL()); |
102 } | 109 } |
103 | 110 |
| 111 // Core |
| 112 |
104 class SettingsFrontend::Core | 113 class SettingsFrontend::Core |
105 : public base::RefCountedThreadSafe<Core> { | 114 : public base::RefCountedThreadSafe<Core> { |
106 public: | 115 public: |
107 explicit Core( | 116 explicit Core( |
| 117 // Ownership taken. |
| 118 SettingsStorageFactory* storage_factory, |
108 const scoped_refptr<SettingsObserverList>& observers) | 119 const scoped_refptr<SettingsObserverList>& observers) |
109 : observers_(observers), backends_(NULL) { | 120 : storage_factory_(storage_factory), |
| 121 observers_(observers), |
| 122 backends_(NULL) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 } | 124 } |
112 | 125 |
113 typedef base::Callback<void(Backends*)> BackendsCallback; | 126 typedef base::Callback<void(Backends*)> BackendsCallback; |
114 | 127 |
115 // Does any FILE thread specific initialization, such as construction of | 128 // Does any FILE thread specific initialization, such as construction of |
116 // |backend_|. Must be called before any call to | 129 // |backend_|. Must be called before any call to |
117 // RunWithBackendOnFileThread(). | 130 // RunWithBackendOnFileThread(). |
118 void InitOnFileThread(const FilePath& profile_path) { | 131 void InitOnFileThread(const FilePath& profile_path) { |
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
120 DCHECK(!backends_); | 133 DCHECK(!backends_); |
121 backends_ = new Backends(profile_path, observers_); | 134 backends_ = |
| 135 new Backends( |
| 136 storage_factory_.release(), profile_path, observers_); |
122 } | 137 } |
123 | 138 |
124 // Runs |callback| with both the extensions and apps settings on the FILE | 139 // Runs |callback| with both the extensions and apps settings on the FILE |
125 // thread. | 140 // thread. |
126 void RunWithBackends(const BackendsCallback& callback) { | 141 void RunWithBackends(const BackendsCallback& callback) { |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
128 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
129 BrowserThread::FILE, | 144 BrowserThread::FILE, |
130 FROM_HERE, | 145 FROM_HERE, |
131 base::Bind( | 146 base::Bind( |
(...skipping 14 matching lines...) Expand all Loading... |
146 delete backends_; | 161 delete backends_; |
147 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 162 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
148 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backends_); | 163 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backends_); |
149 } else { | 164 } else { |
150 NOTREACHED(); | 165 NOTREACHED(); |
151 } | 166 } |
152 } | 167 } |
153 | 168 |
154 friend class base::RefCountedThreadSafe<Core>; | 169 friend class base::RefCountedThreadSafe<Core>; |
155 | 170 |
| 171 // Leveldb storage area factory. Ownership passed to Backends on Init. |
| 172 scoped_ptr<SettingsStorageFactory> storage_factory_; |
| 173 |
156 // Observers to settings changes (thread safe). | 174 // Observers to settings changes (thread safe). |
157 scoped_refptr<SettingsObserverList> observers_; | 175 scoped_refptr<SettingsObserverList> observers_; |
158 | 176 |
159 // Backends for extensions and apps settings. Lives on FILE thread. | 177 // Backends for extensions and apps settings. Lives on FILE thread. |
160 Backends* backends_; | 178 Backends* backends_; |
161 | 179 |
162 DISALLOW_COPY_AND_ASSIGN(Core); | 180 DISALLOW_COPY_AND_ASSIGN(Core); |
163 }; | 181 }; |
164 | 182 |
165 SettingsFrontend::SettingsFrontend(Profile* profile) | 183 // SettingsFrontend |
| 184 |
| 185 /* static */ |
| 186 SettingsFrontend* SettingsFrontend::Create(Profile* profile) { |
| 187 return new SettingsFrontend(new SettingsLeveldbStorage::Factory(), profile); |
| 188 } |
| 189 |
| 190 /* static */ |
| 191 SettingsFrontend* SettingsFrontend::Create( |
| 192 SettingsStorageFactory* storage_factory, Profile* profile) { |
| 193 return new SettingsFrontend(storage_factory, profile); |
| 194 } |
| 195 |
| 196 SettingsFrontend::SettingsFrontend( |
| 197 SettingsStorageFactory* storage_factory, Profile* profile) |
166 : profile_(profile), | 198 : profile_(profile), |
167 observers_(new SettingsObserverList()), | 199 observers_(new SettingsObserverList()), |
168 default_observer_(profile), | 200 default_observer_(profile), |
169 core_(new SettingsFrontend::Core(observers_)) { | 201 core_(new SettingsFrontend::Core(storage_factory, observers_)) { |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
171 DCHECK(!profile->IsOffTheRecord()); | 203 DCHECK(!profile->IsOffTheRecord()); |
172 | 204 |
173 observers_->AddObserver(&default_observer_); | 205 observers_->AddObserver(&default_observer_); |
174 | 206 |
175 BrowserThread::PostTask( | 207 BrowserThread::PostTask( |
176 BrowserThread::FILE, | 208 BrowserThread::FILE, |
177 FROM_HERE, | 209 FROM_HERE, |
178 base::Bind( | 210 base::Bind( |
179 &SettingsFrontend::Core::InitOnFileThread, | 211 &SettingsFrontend::Core::InitOnFileThread, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 core_->RunWithBackends(base::Bind(&DeleteStorageOnFileThread, extension_id)); | 265 core_->RunWithBackends(base::Bind(&DeleteStorageOnFileThread, extension_id)); |
234 } | 266 } |
235 | 267 |
236 scoped_refptr<SettingsObserverList> | 268 scoped_refptr<SettingsObserverList> |
237 SettingsFrontend::GetObservers() { | 269 SettingsFrontend::GetObservers() { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 return observers_; | 271 return observers_; |
240 } | 272 } |
241 | 273 |
242 } // namespace extensions | 274 } // namespace extensions |
OLD | NEW |