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/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 } | 235 } |
236 | 236 |
237 // Handles showing a dialog to the user to ask for the filename for a file to | 237 // Handles showing a dialog to the user to ask for the filename for a file to |
238 // save or open. | 238 // save or open. |
239 class FileSystemChooseFileFunction::FilePicker | 239 class FileSystemChooseFileFunction::FilePicker |
240 : public SelectFileDialog::Listener { | 240 : public SelectFileDialog::Listener { |
241 public: | 241 public: |
242 FilePicker(FileSystemChooseFileFunction* function, | 242 FilePicker(FileSystemChooseFileFunction* function, |
243 content::WebContents* web_contents, | 243 content::WebContents* web_contents, |
244 const FilePath& suggested_path, | 244 const FilePath& suggested_path, |
| 245 const SelectFileDialog::FileTypeInfo& file_type_info, |
245 SelectFileDialog::Type picker_type, | 246 SelectFileDialog::Type picker_type, |
246 EntryType entry_type) | 247 EntryType entry_type) |
247 : suggested_path_(suggested_path), | 248 : suggested_path_(suggested_path), |
| 249 file_type_info_(file_type_info), |
248 entry_type_(entry_type), | 250 entry_type_(entry_type), |
249 function_(function) { | 251 function_(function) { |
250 select_file_dialog_ = SelectFileDialog::Create( | 252 select_file_dialog_ = SelectFileDialog::Create( |
251 this, new ChromeSelectFilePolicy(web_contents)); | 253 this, new ChromeSelectFilePolicy(web_contents)); |
252 SelectFileDialog::FileTypeInfo file_type_info; | 254 /* SelectFileDialog::FileTypeInfo file_type_info; |
253 FilePath::StringType extension = suggested_path.Extension(); | 255 FilePath::StringType extension = suggested_path.Extension(); |
254 if (!extension.empty()) { | 256 if (!extension.empty()) { |
255 extension.erase(extension.begin()); // drop the . | 257 extension.erase(extension.begin()); // drop the . |
256 file_type_info.extensions.resize(1); | 258 file_type_info.extensions.resize(1); |
257 file_type_info.extensions[0].push_back(extension); | 259 file_type_info.extensions[0].push_back(extension); |
258 } | 260 } |
259 file_type_info.include_all_files = true; | 261 file_type_info.include_all_files = true;*/ |
260 gfx::NativeWindow owning_window = web_contents ? | 262 gfx::NativeWindow owning_window = web_contents ? |
261 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; | 263 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; |
262 | 264 |
263 if (g_skip_picker_for_test) { | 265 if (g_skip_picker_for_test) { |
264 if (g_path_to_be_picked_for_test) { | 266 if (g_path_to_be_picked_for_test) { |
265 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 267 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
266 base::Bind( | 268 base::Bind( |
267 &FileSystemChooseFileFunction::FilePicker::FileSelected, | 269 &FileSystemChooseFileFunction::FilePicker::FileSelected, |
268 base::Unretained(this), *g_path_to_be_picked_for_test, 1, | 270 base::Unretained(this), *g_path_to_be_picked_for_test, 1, |
269 static_cast<void*>(NULL))); | 271 static_cast<void*>(NULL))); |
270 } else { | 272 } else { |
271 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 273 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
272 base::Bind( | 274 base::Bind( |
273 &FileSystemChooseFileFunction::FilePicker:: | 275 &FileSystemChooseFileFunction::FilePicker:: |
274 FileSelectionCanceled, | 276 FileSelectionCanceled, |
275 base::Unretained(this), static_cast<void*>(NULL))); | 277 base::Unretained(this), static_cast<void*>(NULL))); |
276 } | 278 } |
277 return; | 279 return; |
278 } | 280 } |
279 | 281 |
280 select_file_dialog_->SelectFile(picker_type, | 282 select_file_dialog_->SelectFile(picker_type, |
281 string16(), | 283 string16(), |
282 suggested_path, | 284 suggested_path, |
283 &file_type_info, 0, FILE_PATH_LITERAL(""), | 285 &file_type_info_, 0, FILE_PATH_LITERAL(""), |
284 owning_window, NULL); | 286 owning_window, NULL); |
285 } | 287 } |
286 | 288 |
287 virtual ~FilePicker() {} | 289 virtual ~FilePicker() {} |
288 | 290 |
289 private: | 291 private: |
290 // SelectFileDialog::Listener implementation. | 292 // SelectFileDialog::Listener implementation. |
291 virtual void FileSelected(const FilePath& path, | 293 virtual void FileSelected(const FilePath& path, |
292 int index, | 294 int index, |
293 void* params) OVERRIDE { | 295 void* params) OVERRIDE { |
294 function_->FileSelected(path, entry_type_); | 296 function_->FileSelected(path, entry_type_); |
295 delete this; | 297 delete this; |
296 } | 298 } |
297 | 299 |
298 virtual void FileSelectionCanceled(void* params) OVERRIDE { | 300 virtual void FileSelectionCanceled(void* params) OVERRIDE { |
299 function_->FileSelectionCanceled(); | 301 function_->FileSelectionCanceled(); |
300 delete this; | 302 delete this; |
301 } | 303 } |
302 | 304 |
303 FilePath suggested_path_; | 305 FilePath suggested_path_; |
304 | 306 |
| 307 SelectFileDialog::FileTypeInfo file_type_info_; |
| 308 |
305 EntryType entry_type_; | 309 EntryType entry_type_; |
306 | 310 |
307 scoped_refptr<SelectFileDialog> select_file_dialog_; | 311 scoped_refptr<SelectFileDialog> select_file_dialog_; |
308 scoped_refptr<FileSystemChooseFileFunction> function_; | 312 scoped_refptr<FileSystemChooseFileFunction> function_; |
309 | 313 |
310 DISALLOW_COPY_AND_ASSIGN(FilePicker); | 314 DISALLOW_COPY_AND_ASSIGN(FilePicker); |
311 }; | 315 }; |
312 | 316 |
313 bool FileSystemChooseFileFunction::ShowPicker( | 317 bool FileSystemChooseFileFunction::ShowPicker( |
314 const FilePath& suggested_path, | 318 const FilePath& suggested_path, |
| 319 const SelectFileDialog::FileTypeInfo& file_type_info, |
315 SelectFileDialog::Type picker_type, | 320 SelectFileDialog::Type picker_type, |
316 EntryType entry_type) { | 321 EntryType entry_type) { |
317 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); | 322 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); |
318 DCHECK(registry); | 323 DCHECK(registry); |
319 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( | 324 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( |
320 render_view_host()); | 325 render_view_host()); |
321 if (!shell_window) { | 326 if (!shell_window) { |
322 error_ = kInvalidCallingPage; | 327 error_ = kInvalidCallingPage; |
323 return false; | 328 return false; |
324 } | 329 } |
325 | 330 |
326 // The file picker will hold a reference to this function instance, preventing | 331 // The file picker will hold a reference to this function instance, preventing |
327 // its destruction (and subsequent sending of the function response) until the | 332 // its destruction (and subsequent sending of the function response) until the |
328 // user has selected a file or cancelled the picker. At that point, the picker | 333 // user has selected a file or cancelled the picker. At that point, the picker |
329 // will delete itself, which will also free the function instance. | 334 // will delete itself, which will also free the function instance. |
330 new FilePicker(this, shell_window->web_contents(), suggested_path, | 335 new FilePicker(this, shell_window->web_contents(), suggested_path, |
331 picker_type, entry_type); | 336 file_type_info, picker_type, entry_type); |
332 return true; | 337 return true; |
333 } | 338 } |
334 | 339 |
335 // static | 340 // static |
336 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( | 341 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( |
337 FilePath* path) { | 342 FilePath* path) { |
338 g_skip_picker_for_test = true; | 343 g_skip_picker_for_test = true; |
339 g_path_to_be_picked_for_test = path; | 344 g_path_to_be_picked_for_test = path; |
340 } | 345 } |
341 | 346 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 error_ = kUnknownChooseFileType; | 390 error_ = kUnknownChooseFileType; |
386 return false; | 391 return false; |
387 } | 392 } |
388 } | 393 } |
389 | 394 |
390 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 395 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
391 error_ = kRequiresFileSystemWriteError; | 396 error_ = kRequiresFileSystemWriteError; |
392 return false; | 397 return false; |
393 } | 398 } |
394 | 399 |
395 return ShowPicker(FilePath(), picker_type, entry_type); | 400 FilePath suggested_path; |
| 401 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type_info( |
| 402 new SelectFileDialog::FileTypeInfo()); |
| 403 |
| 404 if (options) { |
| 405 FilePath::StringType suggested_extension; |
| 406 |
| 407 if (options->suggested_path.get()) { |
| 408 suggested_path = FilePath::FromUTF8Unsafe(*options->suggested_path.get()); |
| 409 } |
| 410 |
| 411 file_type_info->include_all_files = options->accepts_all_types.get(); |
| 412 if (file_type_info->extensions.empty() && !file_type_info->include_all_files
) { |
| 413 // TODO: log warning |
| 414 file_type_info->include_all_files = true; |
| 415 } |
| 416 } |
| 417 |
| 418 return ShowPicker(FilePath(), *file_type_info, picker_type, entry_type); |
396 } | 419 } |
397 | 420 |
398 } // namespace extensions | 421 } // namespace extensions |
OLD | NEW |