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/api/media_galleries_private/media_galleries_
private_api.h" | 5 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_api.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // by the |gallery_id|. Returns true and set |gallery_file_path| and | 56 // by the |gallery_id|. Returns true and set |gallery_file_path| and |
57 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise. | 57 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise. |
58 bool GetGalleryFilePathAndId(const std::string& gallery_id, | 58 bool GetGalleryFilePathAndId(const std::string& gallery_id, |
59 Profile* profile, | 59 Profile* profile, |
60 const Extension* extension, | 60 const Extension* extension, |
61 base::FilePath* gallery_file_path, | 61 base::FilePath* gallery_file_path, |
62 MediaGalleryPrefId* gallery_pref_id) { | 62 MediaGalleryPrefId* gallery_pref_id) { |
63 MediaGalleryPrefId pref_id; | 63 MediaGalleryPrefId pref_id; |
64 if (!base::StringToUint64(gallery_id, &pref_id)) | 64 if (!base::StringToUint64(gallery_id, &pref_id)) |
65 return false; | 65 return false; |
66 MediaFileSystemRegistry* registry = | 66 MediaGalleriesPreferences* preferences = |
67 g_browser_process->media_file_system_registry(); | 67 g_browser_process->media_file_system_registry()->GetPreferences(profile); |
68 base::FilePath file_path( | 68 base::FilePath file_path( |
69 registry->GetPreferences(profile)->LookUpGalleryPathForExtension( | 69 preferences->LookUpGalleryPathForExtension(pref_id, extension, false)); |
70 pref_id, extension, false)); | |
71 if (file_path.empty()) | 70 if (file_path.empty()) |
72 return false; | 71 return false; |
73 *gallery_pref_id = pref_id; | 72 *gallery_pref_id = pref_id; |
74 *gallery_file_path = file_path; | 73 *gallery_file_path = file_path; |
75 return true; | 74 return true; |
76 } | 75 } |
77 | 76 |
78 } // namespace | 77 } // namespace |
79 | 78 |
80 | 79 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 114 } |
116 | 115 |
117 // static | 116 // static |
118 MediaGalleriesPrivateAPI* MediaGalleriesPrivateAPI::Get(Profile* profile) { | 117 MediaGalleriesPrivateAPI* MediaGalleriesPrivateAPI::Get(Profile* profile) { |
119 return | 118 return |
120 ProfileKeyedAPIFactory<MediaGalleriesPrivateAPI>::GetForProfile(profile); | 119 ProfileKeyedAPIFactory<MediaGalleriesPrivateAPI>::GetForProfile(profile); |
121 } | 120 } |
122 | 121 |
123 void MediaGalleriesPrivateAPI::OnListenerAdded( | 122 void MediaGalleriesPrivateAPI::OnListenerAdded( |
124 const EventListenerInfo& details) { | 123 const EventListenerInfo& details) { |
125 // Start the StorageMonitor if it is not already initialized. After that, | 124 // Make sure MediaGalleriesPreferences is initialized. After that, |
126 // try to initialize the event router for the listener. | 125 // try to initialize the event router for the listener. |
127 // This method is called synchronously with the message handler for the | 126 // This method is called synchronously with the message handler for the |
128 // JS invocation. | 127 // JS invocation. |
129 | 128 |
130 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 129 MediaGalleriesPreferences* preferences = |
| 130 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
| 131 preferences->EnsureInitialized(base::Bind( |
131 &MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker, | 132 &MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker, |
132 weak_ptr_factory_.GetWeakPtr())); | 133 weak_ptr_factory_.GetWeakPtr())); |
133 } | 134 } |
134 | 135 |
135 MediaGalleriesPrivateEventRouter* MediaGalleriesPrivateAPI::GetEventRouter() { | 136 MediaGalleriesPrivateEventRouter* MediaGalleriesPrivateAPI::GetEventRouter() { |
136 MaybeInitializeEventRouterAndTracker(); | 137 MaybeInitializeEventRouterAndTracker(); |
137 return media_galleries_private_event_router_.get(); | 138 return media_galleries_private_event_router_.get(); |
138 } | 139 } |
139 | 140 |
140 GalleryWatchStateTracker* | 141 GalleryWatchStateTracker* |
141 MediaGalleriesPrivateAPI::GetGalleryWatchStateTracker() { | 142 MediaGalleriesPrivateAPI::GetGalleryWatchStateTracker() { |
142 MaybeInitializeEventRouterAndTracker(); | 143 MaybeInitializeEventRouterAndTracker(); |
143 return tracker_.get(); | 144 return tracker_.get(); |
144 } | 145 } |
145 | 146 |
146 void MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker() { | 147 void MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker() { |
147 if (media_galleries_private_event_router_.get()) | 148 if (media_galleries_private_event_router_.get()) |
148 return; | 149 return; |
149 media_galleries_private_event_router_.reset( | 150 media_galleries_private_event_router_.reset( |
150 new MediaGalleriesPrivateEventRouter(profile_)); | 151 new MediaGalleriesPrivateEventRouter(profile_)); |
151 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | 152 DCHECK(g_browser_process->media_file_system_registry()-> |
| 153 GetPreferences(profile_)->IsInitialized()); |
152 tracker_.reset( | 154 tracker_.reset( |
153 new GalleryWatchStateTracker(profile_)); | 155 new GalleryWatchStateTracker(profile_)); |
154 } | 156 } |
155 | 157 |
156 /////////////////////////////////////////////////////////////////////////////// | 158 /////////////////////////////////////////////////////////////////////////////// |
157 // MediaGalleriesPrivateAddGalleryWatchFunction // | 159 // MediaGalleriesPrivateAddGalleryWatchFunction // |
158 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
159 MediaGalleriesPrivateAddGalleryWatchFunction:: | 161 MediaGalleriesPrivateAddGalleryWatchFunction:: |
160 ~MediaGalleriesPrivateAddGalleryWatchFunction() { | 162 ~MediaGalleriesPrivateAddGalleryWatchFunction() { |
161 } | 163 } |
162 | 164 |
163 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | 165 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { |
164 DCHECK(profile_); | 166 DCHECK(profile_); |
165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
166 if (!render_view_host() || !render_view_host()->GetProcess()) | 168 if (!render_view_host() || !render_view_host()->GetProcess()) |
167 return false; | 169 return false; |
168 | 170 |
169 scoped_ptr<AddGalleryWatch::Params> params( | 171 scoped_ptr<AddGalleryWatch::Params> params( |
170 AddGalleryWatch::Params::Create(*args_)); | 172 AddGalleryWatch::Params::Create(*args_)); |
171 EXTENSION_FUNCTION_VALIDATE(params.get()); | 173 EXTENSION_FUNCTION_VALIDATE(params.get()); |
172 | 174 |
173 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 175 MediaGalleriesPreferences* preferences = |
174 &MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit, | 176 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
| 177 preferences->EnsureInitialized(base::Bind( |
| 178 &MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferencesInit, |
175 this, | 179 this, |
176 params->gallery_id)); | 180 params->gallery_id)); |
177 | 181 |
178 return true; | 182 return true; |
179 } | 183 } |
180 | 184 |
181 void MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit( | 185 void MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferencesInit( |
182 const std::string& pref_id) { | 186 const std::string& pref_id) { |
183 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | |
184 base::FilePath gallery_file_path; | 187 base::FilePath gallery_file_path; |
185 MediaGalleryPrefId gallery_pref_id = 0; | 188 MediaGalleryPrefId gallery_pref_id = 0; |
186 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), | 189 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), |
187 &gallery_file_path, &gallery_pref_id)) { | 190 &gallery_file_path, &gallery_pref_id)) { |
188 error_ = kInvalidGalleryIDError; | 191 error_ = kInvalidGalleryIDError; |
189 HandleResponse(gallery_pref_id, false); | 192 HandleResponse(gallery_pref_id, false); |
190 return; | 193 return; |
191 } | 194 } |
192 | 195 |
193 #if defined(OS_WIN) | 196 #if defined(OS_WIN) |
(...skipping 21 matching lines...) Expand all Loading... |
215 | 218 |
216 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 219 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
217 MediaGalleryPrefId gallery_id, | 220 MediaGalleryPrefId gallery_id, |
218 bool success) { | 221 bool success) { |
219 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 222 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
220 media_galleries_private::AddGalleryWatchResult result; | 223 media_galleries_private::AddGalleryWatchResult result; |
221 result.gallery_id = base::Uint64ToString(gallery_id); | 224 result.gallery_id = base::Uint64ToString(gallery_id); |
222 result.success = success; | 225 result.success = success; |
223 SetResult(result.ToValue().release()); | 226 SetResult(result.ToValue().release()); |
224 if (success) { | 227 if (success) { |
225 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | 228 DCHECK(g_browser_process->media_file_system_registry()-> |
| 229 GetPreferences(profile_)->IsInitialized()); |
226 GalleryWatchStateTracker* state_tracker = | 230 GalleryWatchStateTracker* state_tracker = |
227 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 231 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
228 state_tracker->OnGalleryWatchAdded(extension_id(), gallery_id); | 232 state_tracker->OnGalleryWatchAdded(extension_id(), gallery_id); |
229 } | 233 } |
230 SendResponse(true); | 234 SendResponse(true); |
231 } | 235 } |
232 | 236 |
233 | 237 |
234 /////////////////////////////////////////////////////////////////////////////// | 238 /////////////////////////////////////////////////////////////////////////////// |
235 // MediaGalleriesPrivateRemoveGalleryWatchFunction // | 239 // MediaGalleriesPrivateRemoveGalleryWatchFunction // |
236 /////////////////////////////////////////////////////////////////////////////// | 240 /////////////////////////////////////////////////////////////////////////////// |
237 | 241 |
238 MediaGalleriesPrivateRemoveGalleryWatchFunction:: | 242 MediaGalleriesPrivateRemoveGalleryWatchFunction:: |
239 ~MediaGalleriesPrivateRemoveGalleryWatchFunction() { | 243 ~MediaGalleriesPrivateRemoveGalleryWatchFunction() { |
240 } | 244 } |
241 | 245 |
242 bool MediaGalleriesPrivateRemoveGalleryWatchFunction::RunImpl() { | 246 bool MediaGalleriesPrivateRemoveGalleryWatchFunction::RunImpl() { |
243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 247 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
244 if (!render_view_host() || !render_view_host()->GetProcess()) | 248 if (!render_view_host() || !render_view_host()->GetProcess()) |
245 return false; | 249 return false; |
246 | 250 |
247 // Remove gallery watch operation is currently supported on windows platforms. | 251 // Remove gallery watch operation is currently supported on windows platforms. |
248 // Please refer to crbug.com/144491 for more details. | 252 // Please refer to crbug.com/144491 for more details. |
249 scoped_ptr<RemoveGalleryWatch::Params> params( | 253 scoped_ptr<RemoveGalleryWatch::Params> params( |
250 RemoveGalleryWatch::Params::Create(*args_)); | 254 RemoveGalleryWatch::Params::Create(*args_)); |
251 EXTENSION_FUNCTION_VALIDATE(params.get()); | 255 EXTENSION_FUNCTION_VALIDATE(params.get()); |
252 | 256 |
253 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 257 MediaGalleriesPreferences* preferences = |
254 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit, | 258 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
| 259 preferences->EnsureInitialized(base::Bind( |
| 260 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferencesInit, |
255 this, | 261 this, |
256 params->gallery_id)); | 262 params->gallery_id)); |
257 return true; | 263 return true; |
258 } | 264 } |
259 | 265 |
260 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit( | 266 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferencesInit( |
261 const std::string& pref_id) { | 267 const std::string& pref_id) { |
262 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | |
263 #if defined(OS_WIN) | 268 #if defined(OS_WIN) |
264 base::FilePath gallery_file_path; | 269 base::FilePath gallery_file_path; |
265 MediaGalleryPrefId gallery_pref_id = 0; | 270 MediaGalleryPrefId gallery_pref_id = 0; |
266 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), | 271 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), |
267 &gallery_file_path, &gallery_pref_id)) { | 272 &gallery_file_path, &gallery_pref_id)) { |
268 error_ = kInvalidGalleryIDError; | 273 error_ = kInvalidGalleryIDError; |
269 SendResponse(false); | 274 SendResponse(false); |
270 return; | 275 return; |
271 } | 276 } |
272 | 277 |
(...skipping 17 matching lines...) Expand all Loading... |
290 | 295 |
291 MediaGalleriesPrivateGetAllGalleryWatchFunction:: | 296 MediaGalleriesPrivateGetAllGalleryWatchFunction:: |
292 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { | 297 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { |
293 } | 298 } |
294 | 299 |
295 bool MediaGalleriesPrivateGetAllGalleryWatchFunction::RunImpl() { | 300 bool MediaGalleriesPrivateGetAllGalleryWatchFunction::RunImpl() { |
296 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
297 if (!render_view_host() || !render_view_host()->GetProcess()) | 302 if (!render_view_host() || !render_view_host()->GetProcess()) |
298 return false; | 303 return false; |
299 | 304 |
300 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 305 MediaGalleriesPreferences* preferences = |
301 &MediaGalleriesPrivateGetAllGalleryWatchFunction::OnStorageMonitorInit, | 306 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
| 307 preferences->EnsureInitialized(base::Bind( |
| 308 &MediaGalleriesPrivateGetAllGalleryWatchFunction::OnPreferencesInit, |
302 this)); | 309 this)); |
303 return true; | 310 return true; |
304 } | 311 } |
305 | 312 |
306 void MediaGalleriesPrivateGetAllGalleryWatchFunction::OnStorageMonitorInit() { | 313 void MediaGalleriesPrivateGetAllGalleryWatchFunction::OnPreferencesInit() { |
307 std::vector<std::string> result; | 314 std::vector<std::string> result; |
308 #if defined(OS_WIN) | 315 #if defined(OS_WIN) |
309 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | |
310 GalleryWatchStateTracker* state_tracker = | 316 GalleryWatchStateTracker* state_tracker = |
311 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 317 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
312 MediaGalleryPrefIdSet gallery_ids = | 318 MediaGalleryPrefIdSet gallery_ids = |
313 state_tracker->GetAllWatchedGalleryIDsForExtension(extension_id()); | 319 state_tracker->GetAllWatchedGalleryIDsForExtension(extension_id()); |
314 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin(); | 320 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin(); |
315 iter != gallery_ids.end(); ++iter) { | 321 iter != gallery_ids.end(); ++iter) { |
316 result.push_back(base::Uint64ToString(*iter)); | 322 result.push_back(base::Uint64ToString(*iter)); |
317 } | 323 } |
318 #endif | 324 #endif |
319 results_ = GetAllGalleryWatch::Results::Create(result); | 325 results_ = GetAllGalleryWatch::Results::Create(result); |
320 SendResponse(true); | 326 SendResponse(true); |
321 } | 327 } |
322 | 328 |
323 /////////////////////////////////////////////////////////////////////////////// | 329 /////////////////////////////////////////////////////////////////////////////// |
324 // MediaGalleriesPrivateRemoveAllGalleryWatchFunction // | 330 // MediaGalleriesPrivateRemoveAllGalleryWatchFunction // |
325 /////////////////////////////////////////////////////////////////////////////// | 331 /////////////////////////////////////////////////////////////////////////////// |
326 | 332 |
327 MediaGalleriesPrivateRemoveAllGalleryWatchFunction:: | 333 MediaGalleriesPrivateRemoveAllGalleryWatchFunction:: |
328 ~MediaGalleriesPrivateRemoveAllGalleryWatchFunction() { | 334 ~MediaGalleriesPrivateRemoveAllGalleryWatchFunction() { |
329 } | 335 } |
330 | 336 |
331 bool MediaGalleriesPrivateRemoveAllGalleryWatchFunction::RunImpl() { | 337 bool MediaGalleriesPrivateRemoveAllGalleryWatchFunction::RunImpl() { |
332 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 338 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
333 if (!render_view_host() || !render_view_host()->GetProcess()) | 339 if (!render_view_host() || !render_view_host()->GetProcess()) |
334 return false; | 340 return false; |
335 | 341 |
336 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 342 MediaGalleriesPreferences* preferences = |
337 &MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnStorageMonitorInit, | 343 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
| 344 preferences->EnsureInitialized(base::Bind( |
| 345 &MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnPreferencesInit, |
338 this)); | 346 this)); |
339 return true; | 347 return true; |
340 } | 348 } |
341 | 349 |
342 void | 350 void MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnPreferencesInit() { |
343 MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnStorageMonitorInit() { | |
344 #if defined(OS_WIN) | 351 #if defined(OS_WIN) |
345 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | 352 MediaGalleriesPreferences* preferences = |
346 MediaFileSystemRegistry* registry = | 353 g_browser_process->media_file_system_registry()->GetPreferences(profile_); |
347 g_browser_process->media_file_system_registry(); | |
348 MediaGalleriesPreferences* preferences = registry->GetPreferences(profile_); | |
349 GalleryWatchStateTracker* state_tracker = | 354 GalleryWatchStateTracker* state_tracker = |
350 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 355 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
351 state_tracker->RemoveAllGalleryWatchersForExtension( | 356 state_tracker->RemoveAllGalleryWatchersForExtension( |
352 extension_id(), preferences); | 357 extension_id(), preferences); |
353 #endif | 358 #endif |
354 SendResponse(true); | 359 SendResponse(true); |
355 } | 360 } |
356 | 361 |
357 /////////////////////////////////////////////////////////////////////////////// | 362 /////////////////////////////////////////////////////////////////////////////// |
358 // MediaGalleriesPrivateGetHandlersFunction // | 363 // MediaGalleriesPrivateGetHandlersFunction // |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } | 403 } |
399 } | 404 } |
400 | 405 |
401 SetResult(result_list); | 406 SetResult(result_list); |
402 SendResponse(true); | 407 SendResponse(true); |
403 | 408 |
404 return true; | 409 return true; |
405 } | 410 } |
406 | 411 |
407 } // namespace extensions | 412 } // namespace extensions |
OLD | NEW |