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/file_select_helper.h" | 5 #include "chrome/browser/file_select_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 // There is only one file-selection happening at any given time, | 40 // There is only one file-selection happening at any given time, |
41 // so we allocate an enumeration ID for that purpose. All IDs from | 41 // so we allocate an enumeration ID for that purpose. All IDs from |
42 // the renderer must start at 0 and increase. | 42 // the renderer must start at 0 and increase. |
43 const int kFileSelectEnumerationId = -1; | 43 const int kFileSelectEnumerationId = -1; |
44 | 44 |
45 void NotifyRenderViewHost(RenderViewHost* render_view_host, | 45 void NotifyRenderViewHost(RenderViewHost* render_view_host, |
46 const std::vector<ui::SelectedFileInfo>& files, | 46 const std::vector<ui::SelectedFileInfo>& files, |
47 SelectFileDialog::Type dialog_type) { | 47 ui::SelectFileDialog::Type dialog_type) { |
48 const int kReadFilePermissions = | 48 const int kReadFilePermissions = |
49 base::PLATFORM_FILE_OPEN | | 49 base::PLATFORM_FILE_OPEN | |
50 base::PLATFORM_FILE_READ | | 50 base::PLATFORM_FILE_READ | |
51 base::PLATFORM_FILE_EXCLUSIVE_READ | | 51 base::PLATFORM_FILE_EXCLUSIVE_READ | |
52 base::PLATFORM_FILE_ASYNC; | 52 base::PLATFORM_FILE_ASYNC; |
53 | 53 |
54 const int kWriteFilePermissions = | 54 const int kWriteFilePermissions = |
55 base::PLATFORM_FILE_CREATE | | 55 base::PLATFORM_FILE_CREATE | |
56 base::PLATFORM_FILE_CREATE_ALWAYS | | 56 base::PLATFORM_FILE_CREATE_ALWAYS | |
57 base::PLATFORM_FILE_OPEN | | 57 base::PLATFORM_FILE_OPEN | |
58 base::PLATFORM_FILE_OPEN_ALWAYS | | 58 base::PLATFORM_FILE_OPEN_ALWAYS | |
59 base::PLATFORM_FILE_OPEN_TRUNCATED | | 59 base::PLATFORM_FILE_OPEN_TRUNCATED | |
60 base::PLATFORM_FILE_WRITE | | 60 base::PLATFORM_FILE_WRITE | |
61 base::PLATFORM_FILE_WRITE_ATTRIBUTES | | 61 base::PLATFORM_FILE_WRITE_ATTRIBUTES | |
62 base::PLATFORM_FILE_ASYNC; | 62 base::PLATFORM_FILE_ASYNC; |
63 | 63 |
64 int permissions = kReadFilePermissions; | 64 int permissions = kReadFilePermissions; |
65 if (dialog_type == SelectFileDialog::SELECT_SAVEAS_FILE) | 65 if (dialog_type == ui::SelectFileDialog::SELECT_SAVEAS_FILE) |
66 permissions = kWriteFilePermissions; | 66 permissions = kWriteFilePermissions; |
67 render_view_host->FilesSelectedInChooser(files, permissions); | 67 render_view_host->FilesSelectedInChooser(files, permissions); |
68 } | 68 } |
69 | 69 |
70 // Converts a list of FilePaths to a list of ui::SelectedFileInfo. | 70 // Converts a list of FilePaths to a list of ui::SelectedFileInfo. |
71 std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList( | 71 std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList( |
72 const std::vector<FilePath>& paths) { | 72 const std::vector<FilePath>& paths) { |
73 std::vector<ui::SelectedFileInfo> selected_files; | 73 std::vector<ui::SelectedFileInfo> selected_files; |
74 for (size_t i = 0; i < paths.size(); ++i) { | 74 for (size_t i = 0; i < paths.size(); ++i) { |
75 selected_files.push_back( | 75 selected_files.push_back( |
(...skipping 12 matching lines...) Expand all Loading... |
88 RenderViewHost* rvh_; | 88 RenderViewHost* rvh_; |
89 std::vector<FilePath> results_; | 89 std::vector<FilePath> results_; |
90 }; | 90 }; |
91 | 91 |
92 FileSelectHelper::FileSelectHelper(Profile* profile) | 92 FileSelectHelper::FileSelectHelper(Profile* profile) |
93 : profile_(profile), | 93 : profile_(profile), |
94 render_view_host_(NULL), | 94 render_view_host_(NULL), |
95 web_contents_(NULL), | 95 web_contents_(NULL), |
96 select_file_dialog_(), | 96 select_file_dialog_(), |
97 select_file_types_(), | 97 select_file_types_(), |
98 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { | 98 dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE) { |
99 } | 99 } |
100 | 100 |
101 FileSelectHelper::~FileSelectHelper() { | 101 FileSelectHelper::~FileSelectHelper() { |
102 // There may be pending file dialogs, we need to tell them that we've gone | 102 // There may be pending file dialogs, we need to tell them that we've gone |
103 // away so they don't try and call back to us. | 103 // away so they don't try and call back to us. |
104 if (select_file_dialog_.get()) | 104 if (select_file_dialog_.get()) |
105 select_file_dialog_->ListenerDestroyed(); | 105 select_file_dialog_->ListenerDestroyed(); |
106 | 106 |
107 // Stop any pending directory enumeration, prevent a callback, and free | 107 // Stop any pending directory enumeration, prevent a callback, and free |
108 // allocated memory. | 108 // allocated memory. |
(...skipping 14 matching lines...) Expand all Loading... |
123 void FileSelectHelper::FileSelectedWithExtraInfo( | 123 void FileSelectHelper::FileSelectedWithExtraInfo( |
124 const ui::SelectedFileInfo& file, | 124 const ui::SelectedFileInfo& file, |
125 int index, | 125 int index, |
126 void* params) { | 126 void* params) { |
127 if (!render_view_host_) | 127 if (!render_view_host_) |
128 return; | 128 return; |
129 | 129 |
130 profile_->set_last_selected_directory(file.file_path.DirName()); | 130 profile_->set_last_selected_directory(file.file_path.DirName()); |
131 | 131 |
132 const FilePath& path = file.local_path; | 132 const FilePath& path = file.local_path; |
133 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { | 133 if (dialog_type_ == ui::SelectFileDialog::SELECT_FOLDER) { |
134 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); | 134 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); |
135 return; | 135 return; |
136 } | 136 } |
137 | 137 |
138 std::vector<ui::SelectedFileInfo> files; | 138 std::vector<ui::SelectedFileInfo> files; |
139 files.push_back(file); | 139 files.push_back(file); |
140 NotifyRenderViewHost(render_view_host_, files, dialog_type_); | 140 NotifyRenderViewHost(render_view_host_, files, dialog_type_); |
141 | 141 |
142 // No members should be accessed from here on. | 142 // No members should be accessed from here on. |
143 RunFileChooserEnd(); | 143 RunFileChooserEnd(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 FilePathListToSelectedFileInfoList(entry->results_); | 229 FilePathListToSelectedFileInfoList(entry->results_); |
230 | 230 |
231 if (id == kFileSelectEnumerationId) | 231 if (id == kFileSelectEnumerationId) |
232 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_); | 232 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_); |
233 else | 233 else |
234 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); | 234 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
235 | 235 |
236 EnumerateDirectoryEnd(); | 236 EnumerateDirectoryEnd(); |
237 } | 237 } |
238 | 238 |
239 SelectFileDialog::FileTypeInfo* FileSelectHelper::GetFileTypesFromAcceptType( | 239 ui::SelectFileDialog::FileTypeInfo* |
| 240 FileSelectHelper::GetFileTypesFromAcceptType( |
240 const std::vector<string16>& accept_types) { | 241 const std::vector<string16>& accept_types) { |
241 if (accept_types.empty()) | 242 if (accept_types.empty()) |
242 return NULL; | 243 return NULL; |
243 | 244 |
244 // Create FileTypeInfo and pre-allocate for the first extension list. | 245 // Create FileTypeInfo and pre-allocate for the first extension list. |
245 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type( | 246 scoped_ptr<ui::SelectFileDialog::FileTypeInfo> file_type( |
246 new SelectFileDialog::FileTypeInfo()); | 247 new ui::SelectFileDialog::FileTypeInfo()); |
247 file_type->include_all_files = true; | 248 file_type->include_all_files = true; |
248 file_type->extensions.resize(1); | 249 file_type->extensions.resize(1); |
249 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back(); | 250 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back(); |
250 | 251 |
251 // Find the corresponding extensions. | 252 // Find the corresponding extensions. |
252 int valid_type_count = 0; | 253 int valid_type_count = 0; |
253 int description_id = 0; | 254 int description_id = 0; |
254 for (size_t i = 0; i < accept_types.size(); ++i) { | 255 for (size_t i = 0; i < accept_types.size(); ++i) { |
255 std::string ascii_type = UTF16ToASCII(accept_types[i]); | 256 std::string ascii_type = UTF16ToASCII(accept_types[i]); |
256 if (!IsAcceptTypeValid(ascii_type)) | 257 if (!IsAcceptTypeValid(ascii_type)) |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 364 |
364 void FileSelectHelper::RunFileChooserOnUIThread( | 365 void FileSelectHelper::RunFileChooserOnUIThread( |
365 const FileChooserParams& params) { | 366 const FileChooserParams& params) { |
366 if (!render_view_host_ || !web_contents_) { | 367 if (!render_view_host_ || !web_contents_) { |
367 // If the renderer was destroyed before we started, just cancel the | 368 // If the renderer was destroyed before we started, just cancel the |
368 // operation. | 369 // operation. |
369 RunFileChooserEnd(); | 370 RunFileChooserEnd(); |
370 return; | 371 return; |
371 } | 372 } |
372 | 373 |
373 select_file_dialog_ = SelectFileDialog::Create( | 374 select_file_dialog_ = ui::SelectFileDialog::Create( |
374 this, new ChromeSelectFilePolicy(web_contents_)); | 375 this, new ChromeSelectFilePolicy(web_contents_)); |
375 | 376 |
376 switch (params.mode) { | 377 switch (params.mode) { |
377 case FileChooserParams::Open: | 378 case FileChooserParams::Open: |
378 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; | 379 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
379 break; | 380 break; |
380 case FileChooserParams::OpenMultiple: | 381 case FileChooserParams::OpenMultiple: |
381 dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE; | 382 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
382 break; | 383 break; |
383 case FileChooserParams::OpenFolder: | 384 case FileChooserParams::OpenFolder: |
384 dialog_type_ = SelectFileDialog::SELECT_FOLDER; | 385 dialog_type_ = ui::SelectFileDialog::SELECT_FOLDER; |
385 break; | 386 break; |
386 case FileChooserParams::Save: | 387 case FileChooserParams::Save: |
387 dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; | 388 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; |
388 break; | 389 break; |
389 default: | 390 default: |
390 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning. | 391 // Prevent warning. |
| 392 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
391 NOTREACHED(); | 393 NOTREACHED(); |
392 } | 394 } |
393 FilePath default_file_name = params.default_file_name; | 395 FilePath default_file_name = params.default_file_name; |
394 if (default_file_name.empty()) | 396 if (default_file_name.empty()) |
395 default_file_name = profile_->last_selected_directory(); | 397 default_file_name = profile_->last_selected_directory(); |
396 | 398 |
397 gfx::NativeWindow owning_window = | 399 gfx::NativeWindow owning_window = |
398 platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView()); | 400 platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView()); |
399 | 401 |
400 select_file_dialog_->SelectFile( | 402 select_file_dialog_->SelectFile( |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 // A 1 character accept type will always be invalid (either a "." in the case | 473 // A 1 character accept type will always be invalid (either a "." in the case |
472 // of an extension or a "/" in the case of a MIME type). | 474 // of an extension or a "/" in the case of a MIME type). |
473 std::string unused; | 475 std::string unused; |
474 if (accept_type.length() <= 1 || | 476 if (accept_type.length() <= 1 || |
475 StringToLowerASCII(accept_type) != accept_type || | 477 StringToLowerASCII(accept_type) != accept_type || |
476 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { | 478 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { |
477 return false; | 479 return false; |
478 } | 480 } |
479 return true; | 481 return true; |
480 } | 482 } |
OLD | NEW |