Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: ui/shell_dialogs/select_file_dialog_win.cc

Issue 426673003: Introduce a helper for OPENFILENAME struct to make certain logic testable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix renamed method calls. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/win/open_file_name_win_unittest.cc ('k') | ui/ui_unittests.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/shell_dialogs/select_file_dialog_win.cc
diff --git a/ui/shell_dialogs/select_file_dialog_win.cc b/ui/shell_dialogs/select_file_dialog_win.cc
index 00a1f7e116815b721fffde962d0944d3792a35f2..73aacf6ced4374d5f135d04694e0ff1d7379134e 100644
--- a/ui/shell_dialogs/select_file_dialog_win.cc
+++ b/ui/shell_dialogs/select_file_dialog_win.cc
@@ -30,6 +30,7 @@
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/win/open_file_name_win.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/shell_dialogs/base_shell_dialog_win.h"
#include "ui/shell_dialogs/shell_dialogs_delegate.h"
@@ -773,47 +774,22 @@ bool SelectFileDialogImpl::RunOpenFileDialog(
const std::wstring& filter,
HWND owner,
base::FilePath* path) {
- OPENFILENAME ofn;
- // We must do this otherwise the ofn's FlagsEx may be initialized to random
- // junk in release builds which can cause the Places Bar not to show up!
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = owner;
-
- wchar_t filename[MAX_PATH];
- // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12,
- // The lpstrFile Buffer MUST be NULL Terminated.
- filename[0] = 0;
- // Define the dir in here to keep the string buffer pointer pointed to
- // ofn.lpstrInitialDir available during the period of running the
- // GetOpenFileName.
- base::FilePath dir;
- // Use lpstrInitialDir to specify the initial directory
- if (!path->empty()) {
- if (IsDirectory(*path)) {
- ofn.lpstrInitialDir = path->value().c_str();
- } else {
- dir = path->DirName();
- ofn.lpstrInitialDir = dir.value().c_str();
- // Only pure filename can be put in lpstrFile field.
- base::wcslcpy(filename, path->BaseName().value().c_str(),
- arraysize(filename));
- }
- }
-
- ofn.lpstrFile = filename;
- ofn.nMaxFile = MAX_PATH;
-
// We use OFN_NOCHANGEDIR so that the user can rename or delete the directory
// without having to close Chrome first.
- ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
+ ui::win::OpenFileName ofn(owner, OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR);
+ if (!path->empty()) {
+ if (IsDirectory(*path))
+ ofn.SetInitialSelection(*path, base::FilePath());
+ else
+ ofn.SetInitialSelection(path->DirName(), path->BaseName());
+ }
if (!filter.empty())
- ofn.lpstrFilter = filter.c_str();
- bool success = CallGetOpenFileName(&ofn);
+ ofn.GetOPENFILENAME()->lpstrFilter = filter.c_str();
+ bool success = CallGetOpenFileName(ofn.GetOPENFILENAME());
DisableOwner(owner);
if (success)
- *path = base::FilePath(filename);
+ *path = ofn.GetSingleResult();
return success;
}
@@ -822,53 +798,30 @@ bool SelectFileDialogImpl::RunOpenMultiFileDialog(
const std::wstring& filter,
HWND owner,
std::vector<base::FilePath>* paths) {
- OPENFILENAME ofn;
- // We must do this otherwise the ofn's FlagsEx may be initialized to random
- // junk in release builds which can cause the Places Bar not to show up!
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = owner;
-
- scoped_ptr<wchar_t[]> filename(new wchar_t[UNICODE_STRING_MAX_CHARS]);
- filename[0] = 0;
-
- ofn.lpstrFile = filename.get();
- ofn.nMaxFile = UNICODE_STRING_MAX_CHARS;
// We use OFN_NOCHANGEDIR so that the user can rename or delete the directory
// without having to close Chrome first.
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER
- | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_NOCHANGEDIR;
+ ui::win::OpenFileName ofn(owner,
+ OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
+ OFN_EXPLORER | OFN_HIDEREADONLY |
+ OFN_ALLOWMULTISELECT | OFN_NOCHANGEDIR);
- if (!filter.empty()) {
- ofn.lpstrFilter = filter.c_str();
- }
+ if (!filter.empty())
+ ofn.GetOPENFILENAME()->lpstrFilter = filter.c_str();
- bool success = CallGetOpenFileName(&ofn);
+ base::FilePath directory;
+ std::vector<base::FilePath> filenames;
+
+ if (CallGetOpenFileName(ofn.GetOPENFILENAME()))
+ ofn.GetResult(&directory, &filenames);
DisableOwner(owner);
- if (success) {
- std::vector<base::FilePath> files;
- const wchar_t* selection = ofn.lpstrFile;
- while (*selection) { // Empty string indicates end of list.
- files.push_back(base::FilePath(selection));
- // Skip over filename and null-terminator.
- selection += files.back().value().length() + 1;
- }
- if (files.empty()) {
- success = false;
- } else if (files.size() == 1) {
- // When there is one file, it contains the path and filename.
- paths->swap(files);
- } else {
- // Otherwise, the first string is the path, and the remainder are
- // filenames.
- std::vector<base::FilePath>::iterator path = files.begin();
- for (std::vector<base::FilePath>::iterator file = path + 1;
- file != files.end(); ++file) {
- paths->push_back(path->Append(*file));
- }
- }
+
+ for (std::vector<base::FilePath>::iterator it = filenames.begin();
+ it != filenames.end();
+ ++it) {
+ paths->push_back(directory.Append(*it));
}
- return success;
+
+ return !paths->empty();
}
base::string16 SelectFileDialogImpl::GetFilterForFileTypes(
« no previous file with comments | « ui/base/win/open_file_name_win_unittest.cc ('k') | ui/ui_unittests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698