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

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: '' Created 8 years 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 "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api_factory.h" 7 #include "base/bind.h"
8 #include "base/file_path.h"
9 #include "base/location.h"
10 #include "base/string_number_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma nager.h"
14 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma nager_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"
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 "content/public/browser/browser_thread.h"
24 #include "content/public/browser/render_view_host.h"
12 25
13 namespace extensions { 26 namespace extensions {
14 27
28 namespace {
29
30 typedef base::Callback<void(bool success)> AddGalleryCallback;
31
32 // Returns the absolute file path of the gallery specified by the |gallery_id|.
33 // Returns an empty file path if the |gallery_id| is invalid.
34 FilePath GetGalleryAbsolutePath(chrome::MediaGalleryPrefId gallery_id,
35 const Profile* profile,
36 const Extension* extension) {
37 DCHECK(profile);
38 DCHECK(extension);
39 chrome::MediaFileSystemRegistry* registry =
40 g_browser_process->media_file_system_registry();
41 DCHECK(registry);
42 chrome::MediaGalleriesPreferences* preferences =
43 registry->GetPreferences(const_cast<Profile*>(profile));
44 if (!preferences)
45 return FilePath();
46
47 const chrome::MediaGalleryPrefIdSet permitted_galleries =
48 preferences->GalleriesForExtension(*extension);
49 if (permitted_galleries.empty() ||
50 permitted_galleries.find(gallery_id) == permitted_galleries.end())
51 return FilePath();
52
53 const chrome::MediaGalleriesPrefInfoMap& galleries_info =
54 preferences->known_galleries();
55 return chrome::MediaStorageUtil::FindDevicePathById(
56 galleries_info.find(gallery_id)->second.device_id);
57 }
58
59 // Handles the profile shutdown event on the file thread to clean up
60 // GalleryWatchManagerFactory.
61 void HandleProfileShutdownOnFileThread(const Profile* profile) {
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
63 GalleryWatchManagerFactory::GetInstance()->OnProfileShutdown(profile);
64 }
65
66 // Returns GalleryWatchManager associated with the specified |profile|. Set
67 // |create| to true, to create a GalleryWatchManager if it does not exists.
68 GalleryWatchManager* GetGalleryWatchManagerOnFileThread(const Profile* profile,
69 bool create) {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
71 if (!create &&
72 !GalleryWatchManagerFactory::GetInstance()->HasForProfile(profile))
73 return NULL;
74 return GalleryWatchManagerFactory::GetInstance()->GetForProfile(profile);
75 }
76
77 // Set up gallery watch on the file thread for the extension specified by the
78 // |extension_id|.
79 // |profile| specifies the extension profile.
80 // |gallery_id|specifies the gallery identifier.
81 // |gallery_file_path| specifies the absolute gallery path.
82 // |add_gallery_callback| specifies the callback that is called when the setup
83 // operation is complete. It notifies the extension about the watch request
84 // result.
85 void SetupGalleryWatchOnFileThread(
86 const Profile* profile,
87 const std::string& gallery_id,
88 const FilePath& gallery_file_path,
89 const std::string& extension_id,
90 const AddGalleryCallback& add_gallery_callback) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
92 bool success = false;
93 GalleryWatchManager* manager = GetGalleryWatchManagerOnFileThread(profile,
94 true);
95 if (manager) {
96 success = manager->StartGalleryWatch(gallery_id, gallery_file_path,
97 extension_id);
98 }
99 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
100 base::Bind(add_gallery_callback, success));
101 }
102
103 // Cancels the gallery watch for the extension specified by the |extension_id|
104 // on the file thread.
105 void RemoveGalleryWatchOnFileThread(const Profile* profile,
106 const FilePath& gallery_file_path,
107 const std::string& extension_id) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
109 GalleryWatchManager* manager = GetGalleryWatchManagerOnFileThread(profile,
110 false);
111 if (!manager)
112 return;
113 manager->StopGalleryWatch(gallery_file_path, extension_id);
114 }
115
116 } // namespace
117
118 ///////////////////////////////////////////////////////////////////////////////
119 // MediaGalleriesPrivateAPI //
120 ///////////////////////////////////////////////////////////////////////////////
121
15 MediaGalleriesPrivateAPI::MediaGalleriesPrivateAPI(Profile* profile) 122 MediaGalleriesPrivateAPI::MediaGalleriesPrivateAPI(Profile* profile)
16 : profile_(profile) { 123 : profile_(profile) {
124 DCHECK(profile_);
125 extension_notification_observer_.reset(
126 new MediaGalleryExtensionNotificationObserver(profile_));
17 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 127 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
18 this, event_names::kOnAttachEventName); 128 this, event_names::kOnAttachEventName);
19 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 129 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
20 this, event_names::kOnDetachEventName); 130 this, event_names::kOnDetachEventName);
21 131 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
132 this, event_names::kOnGalleryChangedEventName);
22 } 133 }
23 134
24 MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() { 135 MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() {
25 } 136 }
26 137
27 void MediaGalleriesPrivateAPI::Shutdown() { 138 void MediaGalleriesPrivateAPI::Shutdown() {
28 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 139 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
140 content::BrowserThread::PostTask(
141 content::BrowserThread::FILE, FROM_HERE,
142 base::Bind(&HandleProfileShutdownOnFileThread, profile_));
29 } 143 }
30 144
31 void MediaGalleriesPrivateAPI::OnListenerAdded( 145 void MediaGalleriesPrivateAPI::OnListenerAdded(
32 const extensions::EventListenerInfo& details) { 146 const EventListenerInfo& details) {
33 media_galleries_private_event_router_.reset( 147 media_galleries_private_event_router_.reset(
34 new MediaGalleriesPrivateEventRouter(profile_)); 148 new MediaGalleriesPrivateEventRouter(profile_));
35 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 149 }
150
151 ///////////////////////////////////////////////////////////////////////////////
152 // MediaGalleryWatchFunctionBase //
153 ///////////////////////////////////////////////////////////////////////////////
154
155 MediaGalleryWatchFunctionBase::MediaGalleryWatchFunctionBase() {
156 }
157
158 #if defined (OS_WIN)
159 bool MediaGalleryWatchFunctionBase::RunImpl() {
160 if (!render_view_host() || !render_view_host()->GetProcess())
161 return false;
162
163 // First param is the gallery identifier to watch.
164 std::string gallery_id;
165 if (!args_->GetString(0, &gallery_id) || (gallery_id.empty()))
166 return false;
167
168 chrome::MediaGalleryPrefId gallery_pref_id = 0;
169 if (!base::StringToUint64(gallery_id, &gallery_pref_id)) {
170 HandleResponse(gallery_id, false);
171 return true;
172 }
173
174 FilePath gallery_file_path(
175 GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension()));
176 if (gallery_file_path.empty()) {
177 HandleResponse(gallery_id, false);
178 return true;
179 }
180
181 PerformGalleryWatchOperation(gallery_id, gallery_file_path);
182 return true;
183 }
184 #else
185 bool MediaGalleryWatchFunctionBase::RunImpl() {
Lei Zhang 2012/12/15 02:00:29 You shouldn't have to write this function twice.
kmadhusu 2012/12/17 23:58:05 Done.
186 if (!render_view_host() || !render_view_host()->GetProcess())
187 return false;
188
189 // First param is the gallery identifier to watch.
190 std::string gallery_id;
191 if (!args_->GetString(0, &gallery_id) || (gallery_id.empty()))
192 return false;
193
194 // Gallery watch operations are not currently supported on non-windows
195 // platforms. Please refer to crbug.com/144491 for more details.
196 HandleResponse(gallery_id, false);
197 return true;
198 }
199 #endif
200
201
202 ///////////////////////////////////////////////////////////////////////////////
203 // MediaGalleriesPrivateAddGalleryWatchFunction //
204 ///////////////////////////////////////////////////////////////////////////////
205
206 void MediaGalleriesPrivateAddGalleryWatchFunction::PerformGalleryWatchOperation(
207 const std::string& gallery_id,
208 const FilePath& gallery_watch_path) {
209 DCHECK(profile_);
210 content::BrowserThread::PostTask(
Lei Zhang 2012/12/15 02:00:29 Can you PostTaskAndReplyWithResult() instead?
kmadhusu 2012/12/17 23:58:05 Done.
211 content::BrowserThread::FILE, FROM_HERE,
212 base::Bind(
213 &SetupGalleryWatchOnFileThread,
214 profile_,
215 gallery_id,
216 gallery_watch_path,
217 extension_id(),
218 base::Bind(
219 &MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse,
220 this,
221 gallery_id)));
222 }
223
224 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse(
225 const std::string& gallery_id,
226 bool success) {
227 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
Lei Zhang 2012/12/15 02:00:29 Look for an AddGalleryWatchResult class in out/Deb
kmadhusu 2012/12/17 23:58:05 Done.
228 result->SetString("galleryId", gallery_id);
229 result->SetBoolean("success", success);
230 SetResult(result.release());
231 SendResponse(true);
232 }
233
234
235 ///////////////////////////////////////////////////////////////////////////////
236 // MediaGalleriesPrivateRemoveGalleryWatchFunction //
237 ///////////////////////////////////////////////////////////////////////////////
238
239 void MediaGalleriesPrivateRemoveGalleryWatchFunction::
240 PerformGalleryWatchOperation(const std::string& gallery_id,
241 const FilePath& gallery_watch_path) {
242 content::BrowserThread::PostTask(
243 content::BrowserThread::FILE, FROM_HERE,
244 base::Bind(&RemoveGalleryWatchOnFileThread, profile_,
245 gallery_watch_path, extension_id()));
246 HandleResponse(gallery_id, true);
247 }
248
249 void MediaGalleriesPrivateRemoveGalleryWatchFunction::HandleResponse(
250 const std::string& gallery_id,
251 bool success) {
252 SendResponse(true);
36 } 253 }
37 254
38 } // namespace extensions 255 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698