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