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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 // Key for Media Gallery Permission Value. | 53 // Key for Media Gallery Permission Value. |
54 const char kMediaGalleryHasPermissionKey[] = "has_permission"; | 54 const char kMediaGalleryHasPermissionKey[] = "has_permission"; |
55 | 55 |
56 // Handles the profile shutdown event on the file thread to clean up | 56 // Handles the profile shutdown event on the file thread to clean up |
57 // GalleryWatchManager. | 57 // GalleryWatchManager. |
58 void HandleProfileShutdownOnFileThread(void* profile_id) { | 58 void HandleProfileShutdownOnFileThread(void* profile_id) { |
59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
60 GalleryWatchManager::OnProfileShutdown(profile_id); | 60 GalleryWatchManager::OnProfileShutdown(profile_id); |
61 } | 61 } |
62 | 62 |
63 // Gets the |gallery_file_path| and |gallery_pref_id| of the gallery specified | |
64 // by the |gallery_id|. Returns true and set |gallery_file_path| and | |
65 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise. | |
66 bool GetGalleryFilePathAndId(const std::string& gallery_id, | |
67 Profile* profile, | |
68 const Extension* extension, | |
69 base::FilePath* gallery_file_path, | |
70 chrome::MediaGalleryPrefId* gallery_pref_id) { | |
71 chrome::MediaGalleryPrefId pref_id; | |
72 if (!base::StringToUint64(gallery_id, &pref_id)) | |
73 return false; | |
74 chrome::MediaFileSystemRegistry* registry = | |
75 g_browser_process->media_file_system_registry(); | |
76 base::FilePath file_path( | |
77 registry->GetPreferences(profile)->LookUpGalleryPathForExtension( | |
78 pref_id, extension, false)); | |
79 if (file_path.empty()) | |
80 return false; | |
81 *gallery_pref_id = pref_id; | |
82 *gallery_file_path = file_path; | |
83 return true; | |
84 } | |
85 | |
86 bool GetMediaGalleryPermissionFromDictionary( | 63 bool GetMediaGalleryPermissionFromDictionary( |
87 const DictionaryValue* dict, | 64 const DictionaryValue* dict, |
88 chrome::MediaGalleryPermission* out_permission) { | 65 chrome::MediaGalleryPermission* out_permission) { |
89 std::string string_id; | 66 std::string string_id; |
90 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 67 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
91 base::StringToUint64(string_id, &out_permission->pref_id) && | 68 base::StringToUint64(string_id, &out_permission->pref_id) && |
92 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 69 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
93 &out_permission->has_permission)) { | 70 &out_permission->has_permission)) { |
94 return true; | 71 return true; |
95 } | 72 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 | 274 |
298 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | 275 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { |
299 DCHECK(profile_); | 276 DCHECK(profile_); |
300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
301 if (!render_view_host() || !render_view_host()->GetProcess()) | 278 if (!render_view_host() || !render_view_host()->GetProcess()) |
302 return false; | 279 return false; |
303 | 280 |
304 scoped_ptr<AddGalleryWatch::Params> params( | 281 scoped_ptr<AddGalleryWatch::Params> params( |
305 AddGalleryWatch::Params::Create(*args_)); | 282 AddGalleryWatch::Params::Create(*args_)); |
306 EXTENSION_FUNCTION_VALIDATE(params.get()); | 283 EXTENSION_FUNCTION_VALIDATE(params.get()); |
307 base::FilePath gallery_file_path; | 284 |
308 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 285 chrome::MediaGalleryPrefId pref_id = 0; |
Lei Zhang
2013/05/14 04:12:23
Use kInvalidMediaGalleryPrefId instead of 0?
Greg Billock
2013/05/14 21:27:13
Done.
| |
309 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 286 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
310 &gallery_file_path, &gallery_pref_id)) { | 287 pref_id == 0) { |
311 error_ = kInvalidGalleryIDError; | 288 error_ = kInvalidGalleryIDError; |
312 return false; | 289 return false; |
313 } | 290 } |
314 | 291 |
292 chrome::MediaFileSystemRegistry* registry = | |
293 g_browser_process->media_file_system_registry(); | |
294 registry->GetPreferencesAsync(profile_, | |
295 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences, | |
296 base::Unretained(this), pref_id)); | |
Lei Zhang
2013/05/14 04:12:23
MediaGalleriesPrivateAddGalleryWatchFunction is a
Greg Billock
2013/05/14 21:27:13
Good point. Done.
| |
297 return true; | |
298 } | |
299 | |
300 void MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences( | |
301 chrome::MediaGalleryPrefId pref_id, | |
302 chrome::MediaGalleriesPreferences* preferences) { | |
303 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( | |
Lei Zhang
2013/05/14 04:12:23
Handle the case of an empty |file_path| like befor
Greg Billock
2013/05/14 21:27:13
Oops! I thought I had maintained that, but it didn
| |
304 pref_id, GetExtension(), false)); | |
305 | |
306 GalleryWatchStateTracker* state_tracker = | |
307 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | |
308 state_tracker->SetPreferences(preferences); | |
309 | |
315 #if defined(OS_WIN) | 310 #if defined(OS_WIN) |
316 MediaGalleriesPrivateEventRouter* router = | 311 MediaGalleriesPrivateEventRouter* router = |
317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 312 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
318 DCHECK(router); | 313 DCHECK(router); |
319 content::BrowserThread::PostTaskAndReplyWithResult( | 314 content::BrowserThread::PostTaskAndReplyWithResult( |
320 content::BrowserThread::FILE, | 315 content::BrowserThread::FILE, |
321 FROM_HERE, | 316 FROM_HERE, |
322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 317 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
323 profile_, | 318 profile_, |
324 gallery_pref_id, | 319 pref_id, |
325 gallery_file_path, | 320 file_path, |
326 extension_id(), | 321 extension_id(), |
327 router->AsWeakPtr()), | 322 router->AsWeakPtr()), |
328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | 323 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, |
329 this, | 324 this, |
330 gallery_pref_id)); | 325 pref_id)); |
331 #else | 326 #else |
332 // Recursive gallery watch operation is not currently supported on | 327 // Recursive gallery watch operation is not currently supported on |
333 // non-windows platforms. Please refer to crbug.com/144491 for more details. | 328 // non-windows platforms. Please refer to crbug.com/144491 for more details. |
334 HandleResponse(gallery_pref_id, false); | 329 HandleResponse(pref_id, false); |
335 #endif | 330 #endif |
336 return true; | |
337 } | 331 } |
338 | 332 |
339 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 333 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
340 chrome::MediaGalleryPrefId gallery_id, | 334 chrome::MediaGalleryPrefId gallery_id, |
341 bool success) { | 335 bool success) { |
342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 336 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
343 extensions::api::media_galleries_private::AddGalleryWatchResult result; | 337 extensions::api::media_galleries_private::AddGalleryWatchResult result; |
344 result.gallery_id = base::Uint64ToString(gallery_id); | 338 result.gallery_id = base::Uint64ToString(gallery_id); |
345 result.success = success; | 339 result.success = success; |
346 SetResult(result.ToValue().release()); | 340 SetResult(result.ToValue().release()); |
(...skipping 19 matching lines...) Expand all Loading... | |
366 #if defined(OS_WIN) | 360 #if defined(OS_WIN) |
367 if (!render_view_host() || !render_view_host()->GetProcess()) | 361 if (!render_view_host() || !render_view_host()->GetProcess()) |
368 return false; | 362 return false; |
369 | 363 |
370 // Remove gallery watch operation is currently supported on windows platforms. | 364 // Remove gallery watch operation is currently supported on windows platforms. |
371 // Please refer to crbug.com/144491 for more details. | 365 // Please refer to crbug.com/144491 for more details. |
372 scoped_ptr<RemoveGalleryWatch::Params> params( | 366 scoped_ptr<RemoveGalleryWatch::Params> params( |
373 RemoveGalleryWatch::Params::Create(*args_)); | 367 RemoveGalleryWatch::Params::Create(*args_)); |
374 EXTENSION_FUNCTION_VALIDATE(params.get()); | 368 EXTENSION_FUNCTION_VALIDATE(params.get()); |
375 | 369 |
376 base::FilePath gallery_file_path; | 370 chrome::MediaGalleryPrefId pref_id; |
377 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 371 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
378 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 372 pref_id == 0) { |
379 &gallery_file_path, &gallery_pref_id)) { | |
380 error_ = kInvalidGalleryIDError; | 373 error_ = kInvalidGalleryIDError; |
381 return false; | 374 return false; |
382 } | 375 } |
383 | 376 |
377 chrome::MediaFileSystemRegistry* registry = | |
378 g_browser_process->media_file_system_registry(); | |
379 registry->GetPreferencesAsync(profile_, base::Bind( | |
380 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences, | |
381 base::Unretained(this), pref_id)); | |
382 return true; | |
383 } | |
384 | |
385 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences( | |
386 chrome::MediaGalleriesPreferences* preferences, | |
387 chrome::MediaGalleryPrefId pref_id) { | |
388 chrome::MediaGalleryPrefId gallery_pref_id = 0; | |
389 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( | |
Lei Zhang
2013/05/14 04:12:23
ditto
Greg Billock
2013/05/14 21:27:13
Done.
| |
390 pref_id, GetExtension(), false)); | |
391 | |
392 GalleryWatchStateTracker* state_tracker = | |
393 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | |
394 state_tracker->SetPreferences(preferences); | |
395 | |
384 content::BrowserThread::PostTask( | 396 content::BrowserThread::PostTask( |
385 content::BrowserThread::FILE, FROM_HERE, | 397 content::BrowserThread::FILE, FROM_HERE, |
386 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 398 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
387 profile_, | 399 profile_, |
388 gallery_file_path, | 400 preferences, |
401 file_path, | |
389 extension_id())); | 402 extension_id())); |
390 | 403 |
391 GalleryWatchStateTracker* state_tracker = | 404 GalleryWatchStateTracker* state_tracker = |
392 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 405 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
393 state_tracker->OnGalleryWatchRemoved(extension_id(), gallery_pref_id); | 406 state_tracker->OnGalleryWatchRemoved(extension_id(), pref_id); |
394 #endif | 407 #endif |
395 return true; | 408 return true; |
396 } | 409 } |
397 | 410 |
398 /////////////////////////////////////////////////////////////////////////////// | 411 /////////////////////////////////////////////////////////////////////////////// |
399 // MediaGalleriesPrivateGetAllGalleryWatchFunction // | 412 // MediaGalleriesPrivateGetAllGalleryWatchFunction // |
400 /////////////////////////////////////////////////////////////////////////////// | 413 /////////////////////////////////////////////////////////////////////////////// |
401 | 414 |
402 MediaGalleriesPrivateGetAllGalleryWatchFunction:: | 415 MediaGalleriesPrivateGetAllGalleryWatchFunction:: |
403 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { | 416 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 } | 557 } |
545 } | 558 } |
546 | 559 |
547 SetResult(result_list); | 560 SetResult(result_list); |
548 SendResponse(true); | 561 SendResponse(true); |
549 | 562 |
550 return true; | 563 return true; |
551 } | 564 } |
552 | 565 |
553 } // namespace extensions | 566 } // namespace extensions |
OLD | NEW |