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" | |
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) { |
33 media_galleries_private_event_router_.reset( | 134 event_router(); |
Lei Zhang
2013/01/04 22:56:28
This looks a bit weird. Can you add a comment to e
kmadhusu
2013/01/04 23:41:02
Added a comment and a private function "MaybeIniti
| |
34 new MediaGalleriesPrivateEventRouter(profile_)); | 135 } |
35 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 136 |
137 MediaGalleriesPrivateEventRouter* MediaGalleriesPrivateAPI::event_router() { | |
138 if (!media_galleries_private_event_router_.get()) { | |
139 media_galleries_private_event_router_.reset( | |
140 new MediaGalleriesPrivateEventRouter(profile_)); | |
141 } | |
142 return media_galleries_private_event_router_.get(); | |
143 } | |
144 | |
145 /////////////////////////////////////////////////////////////////////////////// | |
146 // MediaGalleriesPrivateAddGalleryWatchFunction // | |
147 /////////////////////////////////////////////////////////////////////////////// | |
148 MediaGalleriesPrivateAddGalleryWatchFunction:: | |
149 ~MediaGalleriesPrivateAddGalleryWatchFunction() { | |
150 } | |
151 | |
152 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | |
153 DCHECK(profile_); | |
154 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
155 if (!render_view_host() || !render_view_host()->GetProcess()) | |
156 return false; | |
157 | |
158 scoped_ptr<AddGalleryWatch::Params> params( | |
159 AddGalleryWatch::Params::Create(*args_)); | |
160 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
161 | |
162 if (params->gallery_id < 0) { | |
163 error_ = kInvalidGalleryIDError; | |
164 return false; | |
165 } | |
166 | |
167 // First param is the gallery identifier to watch. | |
168 chrome::MediaGalleryPrefId gallery_pref_id = | |
169 static_cast<uint64>(params->gallery_id); | |
170 FilePath gallery_file_path( | |
171 GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension())); | |
172 if (gallery_file_path.empty()) { | |
173 error_ = kInvalidGalleryIDError; | |
174 return false; | |
175 } | |
176 | |
177 #if defined(OS_WIN) | |
178 MediaGalleriesPrivateEventRouter* router = | |
179 MediaGalleriesPrivateAPIFactory::GetForProfile(profile_)->event_router(); | |
180 DCHECK(router); | |
181 content::BrowserThread::PostTaskAndReplyWithResult( | |
182 content::BrowserThread::FILE, | |
183 FROM_HERE, | |
184 base::Bind(&SetupGalleryWatchOnFileThread, | |
185 profile_, | |
186 gallery_pref_id, | |
187 gallery_file_path, | |
188 extension_id(), | |
189 router->AsWeakPtr()), | |
190 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | |
191 this, | |
192 gallery_pref_id)); | |
193 #else | |
194 // Recursive gallery watch operation is not currently supported on | |
195 // non-windows platforms. Please refer to crbug.com/144491 for more details. | |
196 HandleResponse(gallery_pref_id, false); | |
197 #endif | |
198 return true; | |
199 } | |
200 | |
201 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | |
202 chrome::MediaGalleryPrefId gallery_id, | |
203 bool success) { | |
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
205 extensions::api::media_galleries_private::AddGalleryWatchResult result; | |
206 result.gallery_id = gallery_id; | |
207 result.success = success; | |
208 SetResult(result.ToValue().release()); | |
209 SendResponse(true); | |
210 } | |
211 | |
212 | |
213 /////////////////////////////////////////////////////////////////////////////// | |
214 // MediaGalleriesPrivateRemoveGalleryWatchFunction // | |
215 /////////////////////////////////////////////////////////////////////////////// | |
216 | |
217 MediaGalleriesPrivateRemoveGalleryWatchFunction:: | |
218 ~MediaGalleriesPrivateRemoveGalleryWatchFunction() { | |
219 } | |
220 | |
221 bool MediaGalleriesPrivateRemoveGalleryWatchFunction::RunImpl() { | |
222 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
223 #if defined(OS_WIN) | |
224 if (!render_view_host() || !render_view_host()->GetProcess()) | |
225 return false; | |
226 | |
227 // Remove gallery watch operation is currently supported on windows platforms. | |
228 // Please refer to crbug.com/144491 for more details. | |
229 scoped_ptr<RemoveGalleryWatch::Params> params( | |
230 RemoveGalleryWatch::Params::Create(*args_)); | |
231 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
232 | |
233 if (params->gallery_id < 0) { | |
234 error_ = kInvalidGalleryIDError; | |
235 return false; | |
236 } | |
237 | |
238 // First param is the gallery identifier. | |
239 chrome::MediaGalleryPrefId gallery_pref_id = | |
240 static_cast<uint64>(params->gallery_id); | |
241 | |
242 FilePath gallery_file_path( | |
243 GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension())); | |
244 if (gallery_file_path.empty()) { | |
245 error_ = kInvalidGalleryIDError; | |
246 return false; | |
247 } | |
248 | |
249 content::BrowserThread::PostTask( | |
250 content::BrowserThread::FILE, FROM_HERE, | |
251 base::Bind(&RemoveGalleryWatchOnFileThread, | |
252 profile_, | |
253 gallery_file_path, | |
254 extension_id())); | |
255 #endif | |
256 return true; | |
36 } | 257 } |
37 | 258 |
38 } // namespace extensions | 259 } // namespace extensions |
OLD | NEW |