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