OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/file_manager_util.h" | 4 #include "chrome/browser/extensions/file_manager_util.h" |
5 | 5 |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
33 #include "chrome/browser/chromeos/media/media_player.h" | 33 #include "chrome/browser/chromeos/media/media_player.h" |
34 #endif | 34 #endif |
35 | 35 |
36 using content::BrowserThread; | 36 using content::BrowserThread; |
37 | 37 |
38 #define FILEBROWSER_DOMAIN "hhaomjibdihmijegdhdafkllkbggdgoj" | 38 #define FILEBROWSER_DOMAIN "hhaomjibdihmijegdhdafkllkbggdgoj" |
39 const char kFileBrowserDomain[] = FILEBROWSER_DOMAIN; | 39 const char kFileBrowserDomain[] = FILEBROWSER_DOMAIN; |
40 | 40 |
| 41 namespace file_manager_util { |
41 namespace { | 42 namespace { |
42 | 43 |
43 #define FILEBROWSER_URL(PATH) \ | 44 #define FILEBROWSER_URL(PATH) \ |
44 ("chrome-extension://" FILEBROWSER_DOMAIN "/" PATH) | 45 ("chrome-extension://" FILEBROWSER_DOMAIN "/" PATH) |
45 // This is the "well known" url for the file manager extension from | 46 // This is the "well known" url for the file manager extension from |
46 // browser/resources/file_manager. In the future we may provide a way to swap | 47 // browser/resources/file_manager. In the future we may provide a way to swap |
47 // out this file manager for an aftermarket part, but not yet. | 48 // out this file manager for an aftermarket part, but not yet. |
48 const char kFileBrowserExtensionUrl[] = FILEBROWSER_URL(""); | 49 const char kFileBrowserExtensionUrl[] = FILEBROWSER_URL(""); |
49 const char kBaseFileBrowserUrl[] = FILEBROWSER_URL("main.html"); | 50 const char kBaseFileBrowserUrl[] = FILEBROWSER_URL("main.html"); |
50 const char kMediaPlayerUrl[] = FILEBROWSER_URL("mediaplayer.html"); | 51 const char kMediaPlayerUrl[] = FILEBROWSER_URL("mediaplayer.html"); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 const char** array, | 129 const char** array, |
129 size_t array_size) { | 130 size_t array_size) { |
130 for (size_t i = 0; i < array_size; i++) { | 131 for (size_t i = 0; i < array_size; i++) { |
131 if (base::strcasecmp(ext, array[i]) == 0) { | 132 if (base::strcasecmp(ext, array[i]) == 0) { |
132 return i; | 133 return i; |
133 } | 134 } |
134 } | 135 } |
135 return 0; | 136 return 0; |
136 } | 137 } |
137 | 138 |
| 139 // Convert numeric dialog type to a string. |
| 140 std::string GetDialogTypeAsString( |
| 141 SelectFileDialog::Type dialog_type) { |
| 142 std::string type_str; |
| 143 switch (dialog_type) { |
| 144 case SelectFileDialog::SELECT_NONE: |
| 145 type_str = "full-page"; |
| 146 break; |
| 147 |
| 148 case SelectFileDialog::SELECT_FOLDER: |
| 149 type_str = "folder"; |
| 150 break; |
| 151 |
| 152 case SelectFileDialog::SELECT_SAVEAS_FILE: |
| 153 type_str = "saveas-file"; |
| 154 break; |
| 155 |
| 156 case SelectFileDialog::SELECT_OPEN_FILE: |
| 157 type_str = "open-file"; |
| 158 break; |
| 159 |
| 160 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: |
| 161 type_str = "open-multi-file"; |
| 162 break; |
| 163 |
| 164 default: |
| 165 NOTREACHED(); |
| 166 } |
| 167 |
| 168 return type_str; |
| 169 } |
| 170 |
138 } // namespace | 171 } // namespace |
139 | 172 |
140 // static | 173 GURL GetFileBrowserExtensionUrl() { |
141 GURL FileManagerUtil::GetFileBrowserExtensionUrl() { | |
142 return GURL(kFileBrowserExtensionUrl); | 174 return GURL(kFileBrowserExtensionUrl); |
143 } | 175 } |
144 | 176 |
145 // static | 177 GURL GetFileBrowserUrl() { |
146 GURL FileManagerUtil::GetFileBrowserUrl() { | |
147 return GURL(kBaseFileBrowserUrl); | 178 return GURL(kBaseFileBrowserUrl); |
148 } | 179 } |
149 | 180 |
150 // static | 181 GURL GetMediaPlayerUrl() { |
151 GURL FileManagerUtil::GetMediaPlayerUrl() { | |
152 return GURL(kMediaPlayerUrl); | 182 return GURL(kMediaPlayerUrl); |
153 } | 183 } |
154 | 184 |
155 // static | 185 GURL GetMediaPlayerPlaylistUrl() { |
156 GURL FileManagerUtil::GetMediaPlayerPlaylistUrl() { | |
157 return GURL(kMediaPlayerPlaylistUrl); | 186 return GURL(kMediaPlayerPlaylistUrl); |
158 } | 187 } |
159 | 188 |
160 // static | 189 bool ConvertFileToFileSystemUrl( |
161 bool FileManagerUtil::ConvertFileToFileSystemUrl( | |
162 Profile* profile, const FilePath& full_file_path, const GURL& origin_url, | 190 Profile* profile, const FilePath& full_file_path, const GURL& origin_url, |
163 GURL* url) { | 191 GURL* url) { |
164 FilePath virtual_path; | 192 FilePath virtual_path; |
165 if (!ConvertFileToRelativeFileSystemPath(profile, full_file_path, | 193 if (!ConvertFileToRelativeFileSystemPath(profile, full_file_path, |
166 &virtual_path)) { | 194 &virtual_path)) { |
167 return false; | 195 return false; |
168 } | 196 } |
169 | 197 |
170 GURL base_url = fileapi::GetFileSystemRootURI(origin_url, | 198 GURL base_url = fileapi::GetFileSystemRootURI(origin_url, |
171 fileapi::kFileSystemTypeExternal); | 199 fileapi::kFileSystemTypeExternal); |
172 *url = GURL(base_url.spec() + virtual_path.value()); | 200 *url = GURL(base_url.spec() + virtual_path.value()); |
173 return true; | 201 return true; |
174 } | 202 } |
175 | 203 |
176 // static | 204 bool ConvertFileToRelativeFileSystemPath( |
177 bool FileManagerUtil::ConvertFileToRelativeFileSystemPath( | |
178 Profile* profile, const FilePath& full_file_path, FilePath* virtual_path) { | 205 Profile* profile, const FilePath& full_file_path, FilePath* virtual_path) { |
179 fileapi::FileSystemPathManager* path_manager = | 206 fileapi::FileSystemPathManager* path_manager = |
180 profile->GetFileSystemContext()->path_manager(); | 207 profile->GetFileSystemContext()->path_manager(); |
181 fileapi::ExternalFileSystemMountPointProvider* provider = | 208 fileapi::ExternalFileSystemMountPointProvider* provider = |
182 path_manager->external_provider(); | 209 path_manager->external_provider(); |
183 if (!provider) | 210 if (!provider) |
184 return false; | 211 return false; |
185 | 212 |
186 // Find if this file path is managed by the external provider. | 213 // Find if this file path is managed by the external provider. |
187 if (!provider->GetVirtualPath(full_file_path, virtual_path)) | 214 if (!provider->GetVirtualPath(full_file_path, virtual_path)) |
188 return false; | 215 return false; |
189 | 216 |
190 return true; | 217 return true; |
191 } | 218 } |
192 | 219 |
193 // static | 220 GURL GetFileBrowserUrlWithParams( |
194 GURL FileManagerUtil::GetFileBrowserUrlWithParams( | |
195 SelectFileDialog::Type type, | 221 SelectFileDialog::Type type, |
196 const string16& title, | 222 const string16& title, |
197 const FilePath& default_virtual_path, | 223 const FilePath& default_virtual_path, |
198 const SelectFileDialog::FileTypeInfo* file_types, | 224 const SelectFileDialog::FileTypeInfo* file_types, |
199 int file_type_index, | 225 int file_type_index, |
200 const FilePath::StringType& default_extension) { | 226 const FilePath::StringType& default_extension) { |
201 DictionaryValue arg_value; | 227 DictionaryValue arg_value; |
202 arg_value.SetString("type", GetDialogTypeAsString(type)); | 228 arg_value.SetString("type", GetDialogTypeAsString(type)); |
203 arg_value.SetString("title", title); | 229 arg_value.SetString("title", title); |
204 arg_value.SetString("defaultPath", default_virtual_path.value()); | 230 arg_value.SetString("defaultPath", default_virtual_path.value()); |
(...skipping 22 matching lines...) Expand all Loading... |
227 types_list->Set(i, dict); | 253 types_list->Set(i, dict); |
228 } | 254 } |
229 arg_value.Set("typeList", types_list); | 255 arg_value.Set("typeList", types_list); |
230 } | 256 } |
231 | 257 |
232 std::string json_args; | 258 std::string json_args; |
233 base::JSONWriter::Write(&arg_value, false, &json_args); | 259 base::JSONWriter::Write(&arg_value, false, &json_args); |
234 | 260 |
235 // kChromeUIFileManagerURL could not be used since query parameters are not | 261 // kChromeUIFileManagerURL could not be used since query parameters are not |
236 // supported for it. | 262 // supported for it. |
237 std::string url = FileManagerUtil::GetFileBrowserUrl().spec() + | 263 std::string url = GetFileBrowserUrl().spec() + |
238 '?' + net::EscapeUrlEncodedData(json_args, false); | 264 '?' + net::EscapeUrlEncodedData(json_args, false); |
239 return GURL(url); | 265 return GURL(url); |
240 } | 266 } |
241 | 267 |
242 // static | 268 void ViewFolder(const FilePath& dir) { |
243 void FileManagerUtil::ViewFolder(const FilePath& dir) { | |
244 Browser* browser = BrowserList::GetLastActive(); | 269 Browser* browser = BrowserList::GetLastActive(); |
245 if (!browser) | 270 if (!browser) |
246 return; | 271 return; |
247 | 272 |
248 FilePath virtual_path; | 273 FilePath virtual_path; |
249 if (!FileManagerUtil::ConvertFileToRelativeFileSystemPath(browser->profile(), | 274 if (!ConvertFileToRelativeFileSystemPath(browser->profile(), dir, |
250 dir, | 275 &virtual_path)) { |
251 &virtual_path)) { | |
252 return; | 276 return; |
253 } | 277 } |
254 | 278 |
255 std::string url = chrome::kChromeUIFileManagerURL; | 279 std::string url = chrome::kChromeUIFileManagerURL; |
256 url += "#/" + net::EscapeUrlEncodedData(virtual_path.value(), false); | 280 url += "#/" + net::EscapeUrlEncodedData(virtual_path.value(), false); |
257 | 281 |
258 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); | 282 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); |
259 browser->ShowSingletonTabRespectRef(GURL(url)); | 283 browser->ShowSingletonTabRespectRef(GURL(url)); |
260 } | 284 } |
261 | 285 |
262 // static | 286 void ViewItem(const FilePath& full_path, bool enqueue) { |
263 void FileManagerUtil::ViewItem(const FilePath& full_path, bool enqueue) { | |
264 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 287 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
265 bool result = BrowserThread::PostTask( | 288 bool result = BrowserThread::PostTask( |
266 BrowserThread::UI, FROM_HERE, | 289 BrowserThread::UI, FROM_HERE, |
267 base::Bind(&FileManagerUtil::ViewItem, full_path, enqueue)); | 290 base::Bind(&ViewItem, full_path, enqueue)); |
268 DCHECK(result); | 291 DCHECK(result); |
269 return; | 292 return; |
270 } | 293 } |
271 | 294 |
272 // There is nothing we can do if the browser is not present. | 295 // There is nothing we can do if the browser is not present. |
273 Browser* browser = BrowserList::GetLastActive(); | 296 Browser* browser = BrowserList::GetLastActive(); |
274 if (!browser) | 297 if (!browser) |
275 return; | 298 return; |
276 | 299 |
277 std::string ext = full_path.Extension(); | 300 std::string ext = full_path.Extension(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 337 |
315 browser::ShowErrorBox( | 338 browser::ShowErrorBox( |
316 browser->window()->GetNativeHandle(), | 339 browser->window()->GetNativeHandle(), |
317 l10n_util::GetStringFUTF16( | 340 l10n_util::GetStringFUTF16( |
318 IDS_FILEBROWSER_ERROR_VIEWING_FILE_TITLE, | 341 IDS_FILEBROWSER_ERROR_VIEWING_FILE_TITLE, |
319 UTF8ToUTF16(full_path.BaseName().value())), | 342 UTF8ToUTF16(full_path.BaseName().value())), |
320 l10n_util::GetStringUTF16( | 343 l10n_util::GetStringUTF16( |
321 IDS_FILEBROWSER_ERROR_VIEWING_FILE)); | 344 IDS_FILEBROWSER_ERROR_VIEWING_FILE)); |
322 } | 345 } |
323 | 346 |
324 // static | 347 } // namespace file_manager_util |
325 std::string FileManagerUtil::GetDialogTypeAsString( | |
326 SelectFileDialog::Type dialog_type) { | |
327 std::string type_str; | |
328 switch (dialog_type) { | |
329 case SelectFileDialog::SELECT_NONE: | |
330 type_str = "full-page"; | |
331 break; | |
332 | 348 |
333 case SelectFileDialog::SELECT_FOLDER: | |
334 type_str = "folder"; | |
335 break; | |
336 | |
337 case SelectFileDialog::SELECT_SAVEAS_FILE: | |
338 type_str = "saveas-file"; | |
339 break; | |
340 | |
341 case SelectFileDialog::SELECT_OPEN_FILE: | |
342 type_str = "open-file"; | |
343 break; | |
344 | |
345 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: | |
346 type_str = "open-multi-file"; | |
347 break; | |
348 | |
349 default: | |
350 NOTREACHED(); | |
351 } | |
352 | |
353 return type_str; | |
354 } | |
OLD | NEW |