Chromium Code Reviews| 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 "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api_factory.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/string_number_conversions.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma nager.h" | |
| 8 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_event_router.h" | 14 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_event_router.h" |
| 15 #include "chrome/browser/extensions/api/media_galleries_private/media_gallery_ex tension_notification_observer.h" | |
| 9 #include "chrome/browser/extensions/event_names.h" | 16 #include "chrome/browser/extensions/event_names.h" |
| 10 #include "chrome/browser/extensions/event_router.h" | 17 #include "chrome/browser/extensions/event_router.h" |
| 18 #include "chrome/browser/extensions/extension_function.h" | |
| 11 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
| 20 #include "chrome/browser/media_gallery/media_file_system_registry.h" | |
| 21 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | |
| 22 #include "chrome/browser/system_monitor/media_storage_util.h" | |
| 23 #include "chrome/common/extensions/api/media_galleries_private.h" | |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "content/public/browser/render_view_host.h" | |
| 12 | 26 |
| 13 namespace extensions { | 27 namespace extensions { |
| 14 | 28 |
| 29 namespace AddGalleryWatch = | |
| 30 extensions::api::media_galleries_private::AddGalleryWatch; | |
| 31 namespace RemoveGalleryWatch = | |
| 32 extensions::api::media_galleries_private::RemoveGalleryWatch; | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 const char kInvalidGalleryIDError[] = "Specified invalid gallery identifier"; | |
|
Lei Zhang
2012/12/19 22:35:50
Just say "Invalid gallery ID"
kmadhusu
2012/12/19 23:13:17
Done.
| |
| 37 | |
| 38 // Returns the absolute file path of the gallery specified by the |gallery_id|. | |
| 39 // Returns an empty file path if the |gallery_id| is invalid. | |
| 40 FilePath GetGalleryAbsolutePath(chrome::MediaGalleryPrefId gallery_id, | |
| 41 Profile* profile, | |
| 42 const Extension* extension) { | |
| 43 DCHECK(profile); | |
| 44 DCHECK(extension); | |
| 45 chrome::MediaFileSystemRegistry* registry = | |
| 46 g_browser_process->media_file_system_registry(); | |
| 47 DCHECK(registry); | |
| 48 chrome::MediaGalleriesPreferences* preferences = | |
| 49 registry->GetPreferences(profile); | |
| 50 if (!preferences) | |
| 51 return FilePath(); | |
| 52 | |
| 53 const chrome::MediaGalleryPrefIdSet permitted_galleries = | |
| 54 preferences->GalleriesForExtension(*extension); | |
| 55 if (permitted_galleries.empty() || | |
| 56 permitted_galleries.find(gallery_id) == permitted_galleries.end()) | |
| 57 return FilePath(); | |
| 58 | |
| 59 const chrome::MediaGalleriesPrefInfoMap& galleries_info = | |
| 60 preferences->known_galleries(); | |
| 61 return chrome::MediaStorageUtil::FindDevicePathById( | |
| 62 galleries_info.find(gallery_id)->second.device_id); | |
| 63 } | |
| 64 | |
| 65 // Handles the profile shutdown event on the file thread to clean up | |
| 66 // GalleryWatchManager. | |
| 67 void HandleProfileShutdownOnFileThread(Profile* profile) { | |
| 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 69 GalleryWatchManager::OnProfileShutdown(profile); | |
| 70 } | |
| 71 | |
| 72 // Sets up a gallery watch on the file thread for the extension specified by the | |
| 73 // |extension_id|. | |
| 74 // |profile| specifies the extension profile. | |
| 75 // |gallery_id| specifies the gallery identifier. | |
| 76 // |gallery_file_path| specifies the absolute gallery path. | |
| 77 // Returns true, if the watch setup operation was successful. | |
| 78 bool SetupGalleryWatchOnFileThread(Profile* profile, | |
| 79 chrome::MediaGalleryPrefId gallery_id, | |
| 80 const FilePath& gallery_file_path, | |
| 81 const std::string& extension_id) { | |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 83 return GalleryWatchManager::GetForProfile(profile)->StartGalleryWatch( | |
| 84 gallery_id, gallery_file_path, extension_id); | |
| 85 } | |
| 86 | |
| 87 // Cancels the gallery watch for the extension specified by the |extension_id| | |
| 88 // on the file thread. | |
| 89 void RemoveGalleryWatchOnFileThread(Profile* profile, | |
| 90 const FilePath& gallery_file_path, | |
| 91 const std::string& extension_id) { | |
| 92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 93 if (!GalleryWatchManager::HasForProfile(profile)) | |
| 94 return; | |
| 95 GalleryWatchManager::GetForProfile(profile)->StopGalleryWatch( | |
| 96 gallery_file_path, extension_id); | |
| 97 } | |
| 98 | |
| 99 } // namespace | |
| 100 | |
| 101 | |
| 102 /////////////////////////////////////////////////////////////////////////////// | |
| 103 // MediaGalleriesPrivateAPI // | |
| 104 /////////////////////////////////////////////////////////////////////////////// | |
| 105 | |
| 15 MediaGalleriesPrivateAPI::MediaGalleriesPrivateAPI(Profile* profile) | 106 MediaGalleriesPrivateAPI::MediaGalleriesPrivateAPI(Profile* profile) |
| 16 : profile_(profile) { | 107 : profile_(profile) { |
| 108 DCHECK(profile_); | |
| 109 extension_notification_observer_.reset( | |
| 110 new MediaGalleryExtensionNotificationObserver(profile_)); | |
| 17 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 111 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 18 this, event_names::kOnAttachEventName); | 112 this, event_names::kOnAttachEventName); |
| 19 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 113 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 20 this, event_names::kOnDetachEventName); | 114 this, event_names::kOnDetachEventName); |
| 21 | 115 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 116 this, event_names::kOnGalleryChangedEventName); | |
| 22 } | 117 } |
| 23 | 118 |
| 24 MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() { | 119 MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() { |
| 25 } | 120 } |
| 26 | 121 |
| 27 void MediaGalleriesPrivateAPI::Shutdown() { | 122 void MediaGalleriesPrivateAPI::Shutdown() { |
| 28 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 123 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 124 content::BrowserThread::PostTask( | |
| 125 content::BrowserThread::FILE, FROM_HERE, | |
| 126 base::Bind(&HandleProfileShutdownOnFileThread, profile_)); | |
| 29 } | 127 } |
| 30 | 128 |
| 31 void MediaGalleriesPrivateAPI::OnListenerAdded( | 129 void MediaGalleriesPrivateAPI::OnListenerAdded( |
| 32 const extensions::EventListenerInfo& details) { | 130 const EventListenerInfo& details) { |
| 33 media_galleries_private_event_router_.reset( | 131 media_galleries_private_event_router_.reset( |
| 34 new MediaGalleriesPrivateEventRouter(profile_)); | 132 new MediaGalleriesPrivateEventRouter(profile_)); |
| 35 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 133 } |
| 134 | |
| 135 /////////////////////////////////////////////////////////////////////////////// | |
| 136 // MediaGalleriesPrivateAddGalleryWatchFunction // | |
| 137 /////////////////////////////////////////////////////////////////////////////// | |
| 138 MediaGalleriesPrivateAddGalleryWatchFunction:: | |
| 139 ~MediaGalleriesPrivateAddGalleryWatchFunction() { | |
| 140 } | |
| 141 | |
| 142 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | |
| 143 DCHECK(profile_); | |
| 144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 145 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 146 return false; | |
| 147 | |
| 148 scoped_ptr<AddGalleryWatch::Params> params( | |
| 149 AddGalleryWatch::Params::Create(*args_)); | |
| 150 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 151 | |
| 152 if (params->gallery_id < 0) { | |
| 153 error_ = kInvalidGalleryIDError; | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 // First param is the gallery identifier to watch. | |
| 158 chrome::MediaGalleryPrefId gallery_pref_id = | |
| 159 static_cast<uint64>(params->gallery_id); | |
| 160 FilePath gallery_file_path( | |
| 161 GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension())); | |
| 162 if (gallery_file_path.empty()) { | |
| 163 error_ = kInvalidGalleryIDError; | |
| 164 return false; | |
| 165 } | |
| 166 | |
| 167 #if defined(OS_WIN) | |
| 168 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 169 content::BrowserThread::FILE, | |
| 170 FROM_HERE, | |
| 171 base::Bind(&SetupGalleryWatchOnFileThread, | |
| 172 profile_, | |
| 173 gallery_pref_id, | |
| 174 gallery_file_path, | |
| 175 extension_id()), | |
| 176 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | |
| 177 this, | |
| 178 gallery_pref_id)); | |
| 179 #else | |
| 180 // Recursive gallery watch operation is not currently supported on | |
| 181 // non-windows platforms. Please refer to crbug.com/144491 for more details. | |
| 182 HandleResponse(gallery_pref_id, false); | |
| 183 #endif | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | |
| 188 chrome::MediaGalleryPrefId gallery_id, | |
| 189 bool success) { | |
| 190 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 191 extensions::api::media_galleries_private::AddGalleryWatchResult result; | |
| 192 result.gallery_id = gallery_id; | |
| 193 result.success = success; | |
| 194 SetResult(result.ToValue().release()); | |
| 195 SendResponse(true); | |
| 196 } | |
| 197 | |
| 198 | |
| 199 /////////////////////////////////////////////////////////////////////////////// | |
| 200 // MediaGalleriesPrivateRemoveGalleryWatchFunction // | |
| 201 /////////////////////////////////////////////////////////////////////////////// | |
| 202 | |
| 203 MediaGalleriesPrivateRemoveGalleryWatchFunction:: | |
| 204 ~MediaGalleriesPrivateRemoveGalleryWatchFunction() { | |
| 205 } | |
| 206 | |
| 207 bool MediaGalleriesPrivateRemoveGalleryWatchFunction::RunImpl() { | |
| 208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 209 #if defined(OS_WIN) | |
| 210 if (!render_view_host() || !render_view_host()->GetProcess()) | |
| 211 return false; | |
| 212 | |
| 213 // Remove gallery watch operation is currently supported on windows platforms. | |
| 214 // Please refer to crbug.com/144491 for more details. | |
| 215 scoped_ptr<RemoveGalleryWatch::Params> params( | |
| 216 RemoveGalleryWatch::Params::Create(*args_)); | |
| 217 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 218 | |
| 219 if (params->gallery_id < 0) { | |
| 220 error_ = kInvalidGalleryIDError; | |
| 221 return false; | |
| 222 } | |
| 223 | |
| 224 // First param is the gallery identifier. | |
| 225 chrome::MediaGalleryPrefId gallery_pref_id = | |
| 226 static_cast<uint64>(params->gallery_id); | |
| 227 | |
| 228 FilePath gallery_file_path( | |
| 229 GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension())); | |
| 230 if (gallery_file_path.empty()) { | |
| 231 error_ = kInvalidGalleryIDError; | |
| 232 return false; | |
| 233 } | |
| 234 | |
| 235 content::BrowserThread::PostTask( | |
| 236 content::BrowserThread::FILE, FROM_HERE, | |
| 237 base::Bind(&RemoveGalleryWatchOnFileThread, | |
| 238 profile_, | |
| 239 gallery_file_path, | |
| 240 extension_id())); | |
| 241 #endif | |
| 242 return true; | |
| 36 } | 243 } |
| 37 | 244 |
| 38 } // namespace extensions | 245 } // namespace extensions |
| OLD | NEW |