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 = chrome::kInvalidMediaGalleryPrefId; |
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 == chrome::kInvalidMediaGalleryPrefId) { |
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( | |
295 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences, | |
296 this, pref_id)); | |
297 return true; | |
298 } | |
299 | |
300 void MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences( | |
301 chrome::MediaGalleryPrefId pref_id, | |
302 chrome::MediaGalleriesPreferences::Vendor* vendor) { | |
303 chrome::MediaGalleriesPreferences* preferences = | |
304 vendor->GetPreferences(profile_); | |
305 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( | |
306 pref_id, GetExtension(), false)); | |
307 | |
308 if (file_path.empty()) { | |
309 HandleResponse(pref_id, false); | |
vandebo (ex-Chrome)
2013/05/22 16:27:52
error_ = kInvalidGalleryIDError;
Greg Billock
2013/05/22 18:42:52
Done.
| |
310 return; | |
311 } | |
312 | |
315 #if defined(OS_WIN) | 313 #if defined(OS_WIN) |
316 MediaGalleriesPrivateEventRouter* router = | 314 MediaGalleriesPrivateEventRouter* router = |
317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 315 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
318 DCHECK(router); | 316 DCHECK(router); |
319 content::BrowserThread::PostTaskAndReplyWithResult( | 317 content::BrowserThread::PostTaskAndReplyWithResult( |
320 content::BrowserThread::FILE, | 318 content::BrowserThread::FILE, |
321 FROM_HERE, | 319 FROM_HERE, |
322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 320 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
323 profile_, | 321 profile_, |
324 gallery_pref_id, | 322 pref_id, |
325 gallery_file_path, | 323 file_path, |
326 extension_id(), | 324 extension_id(), |
327 router->AsWeakPtr()), | 325 router->AsWeakPtr()), |
328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | 326 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, |
329 this, | 327 this, |
330 gallery_pref_id)); | 328 pref_id)); |
331 #else | 329 #else |
332 // Recursive gallery watch operation is not currently supported on | 330 // Recursive gallery watch operation is not currently supported on |
333 // non-windows platforms. Please refer to crbug.com/144491 for more details. | 331 // non-windows platforms. Please refer to crbug.com/144491 for more details. |
334 HandleResponse(gallery_pref_id, false); | 332 HandleResponse(pref_id, false); |
335 #endif | 333 #endif |
336 return true; | |
337 } | 334 } |
338 | 335 |
339 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 336 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
340 chrome::MediaGalleryPrefId gallery_id, | 337 chrome::MediaGalleryPrefId gallery_id, |
341 bool success) { | 338 bool success) { |
342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 339 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
343 extensions::api::media_galleries_private::AddGalleryWatchResult result; | 340 extensions::api::media_galleries_private::AddGalleryWatchResult result; |
344 result.gallery_id = base::Uint64ToString(gallery_id); | 341 result.gallery_id = base::Uint64ToString(gallery_id); |
345 result.success = success; | 342 result.success = success; |
346 SetResult(result.ToValue().release()); | 343 SetResult(result.ToValue().release()); |
(...skipping 19 matching lines...) Expand all Loading... | |
366 #if defined(OS_WIN) | 363 #if defined(OS_WIN) |
367 if (!render_view_host() || !render_view_host()->GetProcess()) | 364 if (!render_view_host() || !render_view_host()->GetProcess()) |
368 return false; | 365 return false; |
369 | 366 |
370 // Remove gallery watch operation is currently supported on windows platforms. | 367 // Remove gallery watch operation is currently supported on windows platforms. |
371 // Please refer to crbug.com/144491 for more details. | 368 // Please refer to crbug.com/144491 for more details. |
372 scoped_ptr<RemoveGalleryWatch::Params> params( | 369 scoped_ptr<RemoveGalleryWatch::Params> params( |
373 RemoveGalleryWatch::Params::Create(*args_)); | 370 RemoveGalleryWatch::Params::Create(*args_)); |
374 EXTENSION_FUNCTION_VALIDATE(params.get()); | 371 EXTENSION_FUNCTION_VALIDATE(params.get()); |
375 | 372 |
376 base::FilePath gallery_file_path; | 373 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
377 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 374 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
378 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 375 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
379 &gallery_file_path, &gallery_pref_id)) { | |
380 error_ = kInvalidGalleryIDError; | 376 error_ = kInvalidGalleryIDError; |
381 return false; | 377 return false; |
382 } | 378 } |
383 | 379 |
380 chrome::MediaFileSystemRegistry* registry = | |
381 g_browser_process->media_file_system_registry(); | |
382 registry->GetPreferencesAsync(base::Bind( | |
383 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences, | |
384 base::Unretained(this), pref_id)); | |
385 return true; | |
386 } | |
387 | |
388 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences( | |
389 chrome::MediaGalleriesPreferences::Vendor* vendor, | |
390 chrome::MediaGalleryPrefId pref_id) { | |
391 chrome::MediaGalleriesPreferences* preferences = | |
392 vendor->GetPreferences(profile_); | |
393 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( | |
394 pref_id, GetExtension(), false)); | |
395 | |
396 if (file_path.empty) { | |
397 HandleResponse(pref_id, false); | |
vandebo (ex-Chrome)
2013/05/22 16:27:52
error_ = kInvalidGalleryIDError;
Greg Billock
2013/05/22 18:42:52
Done.
| |
398 return; | |
399 } | |
400 | |
401 GalleryWatchStateTracker* state_tracker = | |
402 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | |
403 state_tracker->SetPreferences(preferences); | |
vandebo (ex-Chrome)
2013/05/22 16:27:52
I thought we didn't have SetPreferences() any more
Greg Billock
2013/05/22 18:42:52
We don't. Fixing now.
| |
404 | |
384 content::BrowserThread::PostTask( | 405 content::BrowserThread::PostTask( |
385 content::BrowserThread::FILE, FROM_HERE, | 406 content::BrowserThread::FILE, FROM_HERE, |
386 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 407 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
387 profile_, | 408 profile_, |
388 gallery_file_path, | 409 preferences, |
410 file_path, | |
389 extension_id())); | 411 extension_id())); |
390 | 412 |
391 GalleryWatchStateTracker* state_tracker = | 413 GalleryWatchStateTracker* state_tracker = |
392 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 414 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
393 state_tracker->OnGalleryWatchRemoved(extension_id(), gallery_pref_id); | 415 state_tracker->OnGalleryWatchRemoved(extension_id(), pref_id); |
394 #endif | 416 #endif |
395 return true; | 417 return true; |
396 } | 418 } |
397 | 419 |
398 /////////////////////////////////////////////////////////////////////////////// | 420 /////////////////////////////////////////////////////////////////////////////// |
399 // MediaGalleriesPrivateGetAllGalleryWatchFunction // | 421 // MediaGalleriesPrivateGetAllGalleryWatchFunction // |
400 /////////////////////////////////////////////////////////////////////////////// | 422 /////////////////////////////////////////////////////////////////////////////// |
401 | 423 |
402 MediaGalleriesPrivateGetAllGalleryWatchFunction:: | 424 MediaGalleriesPrivateGetAllGalleryWatchFunction:: |
403 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { | 425 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 } | 566 } |
545 } | 567 } |
546 | 568 |
547 SetResult(result_list); | 569 SetResult(result_list); |
548 SendResponse(true); | 570 SendResponse(true); |
549 | 571 |
550 return true; | 572 return true; |
551 } | 573 } |
552 | 574 |
553 } // namespace extensions | 575 } // namespace extensions |
OLD | NEW |