Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698