summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-12 05:42:26 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-12 05:42:26 +0000
commit39188540aa003e164d0c23a6cb830a36b78789a1 (patch)
treea2c024650cbecaa3c9b76110bee3bf825c50f196
parent38bba4f8e2f93dd789c3483a3f0c153ca5809793 (diff)
downloadchromium_src-39188540aa003e164d0c23a6cb830a36b78789a1.zip
chromium_src-39188540aa003e164d0c23a6cb830a36b78789a1.tar.gz
chromium_src-39188540aa003e164d0c23a6cb830a36b78789a1.tar.bz2
Remove some unnecessary dependencies on net from app/win_util and app/resource_bundle.
BUG=none TEST=none Review URL: http://codereview.chromium.org/858006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41401 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--app/resource_bundle.cc2
-rw-r--r--app/win_util.cc292
-rw-r--r--app/win_util.h77
-rw-r--r--app/win_util_unittest.cc10
-rw-r--r--chrome/browser/views/shell_dialogs_win.cc352
5 files changed, 350 insertions, 383 deletions
diff --git a/app/resource_bundle.cc b/app/resource_bundle.cc
index f0b9923..20244d7 100644
--- a/app/resource_bundle.cc
+++ b/app/resource_bundle.cc
@@ -8,8 +8,6 @@
#include "app/gfx/font.h"
#include "base/logging.h"
#include "base/string_piece.h"
-#include "net/base/file_stream.h"
-#include "net/base/net_errors.h"
#include "third_party/skia/include/core/SkBitmap.h"
ResourceBundle* ResourceBundle::g_shared_instance_ = NULL;
diff --git a/app/win_util.cc b/app/win_util.cc
index 3e00b95..4698c35 100644
--- a/app/win_util.cc
+++ b/app/win_util.cc
@@ -26,8 +26,6 @@
#include "base/scoped_handle_win.h"
#include "base/string_util.h"
#include "base/win_util.h"
-#include "grit/app_strings.h"
-#include "net/base/mime_util.h"
// Ensure that we pick up this link library.
#pragma comment(lib, "dwmapi.lib")
@@ -45,57 +43,6 @@ const char kSHGetPropertyStoreForWindow[] = "SHGetPropertyStoreForWindow";
typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *SHGPSFW)(HWND hwnd,
REFIID riid,
void** ppv);
-
-// Enforce visible dialog box.
-UINT_PTR CALLBACK SaveAsDialogHook(HWND dialog, UINT message,
- WPARAM wparam, LPARAM lparam) {
- static const UINT kPrivateMessage = 0x2F3F;
- switch (message) {
- case WM_INITDIALOG: {
- // Do nothing here. Just post a message to defer actual processing.
- PostMessage(dialog, kPrivateMessage, 0, 0);
- return TRUE;
- }
- case kPrivateMessage: {
- // The dialog box is the parent of the current handle.
- HWND real_dialog = GetParent(dialog);
-
- // Retrieve the final size.
- RECT dialog_rect;
- GetWindowRect(real_dialog, &dialog_rect);
-
- // Verify that the upper left corner is visible.
- POINT point = { dialog_rect.left, dialog_rect.top };
- HMONITOR monitor1 = MonitorFromPoint(point, MONITOR_DEFAULTTONULL);
- point.x = dialog_rect.right;
- point.y = dialog_rect.bottom;
-
- // Verify that the lower right corner is visible.
- HMONITOR monitor2 = MonitorFromPoint(point, MONITOR_DEFAULTTONULL);
- if (monitor1 && monitor2)
- return 0;
-
- // Some part of the dialog box is not visible, fix it by moving is to the
- // client rect position of the browser window.
- HWND parent_window = GetParent(real_dialog);
- if (!parent_window)
- return 0;
- WINDOWINFO parent_info;
- parent_info.cbSize = sizeof(WINDOWINFO);
- GetWindowInfo(parent_window, &parent_info);
- SetWindowPos(real_dialog, NULL,
- parent_info.rcClient.left,
- parent_info.rcClient.top,
- 0, 0, // Size.
- SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE |
- SWP_NOZORDER);
-
- return 0;
- }
- }
- return 0;
-}
-
} // namespace
std::wstring FormatSystemTime(const SYSTEMTIME& time,
@@ -202,245 +149,6 @@ bool OpenItemWithExternalApp(const std::wstring& full_path) {
return (TRUE == ::ShellExecuteExW(&sei));
}
-// Get the file type description from the registry. This will be "Text Document"
-// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't
-// have an entry for the file type, we return false, true if the description was
-// found. 'file_ext' must be in form ".txt".
-static bool GetRegistryDescriptionFromExtension(const std::wstring& file_ext,
- std::wstring* reg_description) {
- DCHECK(reg_description);
- RegKey reg_ext(HKEY_CLASSES_ROOT, file_ext.c_str(), KEY_READ);
- std::wstring reg_app;
- if (reg_ext.ReadValue(NULL, &reg_app) && !reg_app.empty()) {
- RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ);
- if (reg_link.ReadValue(NULL, reg_description))
- return true;
- }
- return false;
-}
-
-std::wstring FormatFilterForExtensions(
- const std::vector<std::wstring>& file_ext,
- const std::vector<std::wstring>& ext_desc,
- bool include_all_files) {
- const std::wstring all_ext = L"*.*";
- const std::wstring all_desc = l10n_util::GetString(IDS_APP_SAVEAS_ALL_FILES);
-
- DCHECK(file_ext.size() >= ext_desc.size());
-
- std::wstring result;
-
- for (size_t i = 0; i < file_ext.size(); ++i) {
- std::wstring ext = file_ext[i];
- std::wstring desc;
- if (i < ext_desc.size())
- desc = ext_desc[i];
-
- if (ext.empty()) {
- // Force something reasonable to appear in the dialog box if there is no
- // extension provided.
- include_all_files = true;
- continue;
- }
-
- if (desc.empty()) {
- DCHECK(ext.find(L'.') != std::wstring::npos);
- std::wstring first_extension = ext.substr(ext.find(L'.'));
- size_t first_separator_index = first_extension.find(L';');
- if (first_separator_index != std::wstring::npos)
- first_extension = first_extension.substr(0, first_separator_index);
-
- // Find the extension name without the preceeding '.' character.
- std::wstring ext_name = first_extension;
- size_t ext_index = ext_name.find_first_not_of(L'.');
- if (ext_index != std::wstring::npos)
- ext_name = ext_name.substr(ext_index);
-
- if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) {
- // The extension doesn't exist in the registry. Create a description
- // based on the unknown extension type (i.e. if the extension is .qqq,
- // the we create a description "QQQ File (.qqq)").
- include_all_files = true;
- desc = l10n_util::GetStringF(IDS_APP_SAVEAS_EXTENSION_FORMAT,
- l10n_util::ToUpper(ext_name),
- ext_name);
- }
- if (desc.empty())
- desc = L"*." + ext_name;
- }
-
- result.append(desc.c_str(), desc.size() + 1); // Append NULL too.
- result.append(ext.c_str(), ext.size() + 1);
- }
-
- if (include_all_files) {
- result.append(all_desc.c_str(), all_desc.size() + 1);
- result.append(all_ext.c_str(), all_ext.size() + 1);
- }
-
- result.append(1, '\0'); // Double NULL required.
- return result;
-}
-
-bool SaveFileAs(HWND owner,
- const std::wstring& suggested_name,
- std::wstring* final_name) {
- std::wstring file_ext = file_util::GetFileExtensionFromPath(suggested_name);
- file_ext.insert(0, L"*.");
- std::wstring filter = FormatFilterForExtensions(
- std::vector<std::wstring>(1, file_ext),
- std::vector<std::wstring>(),
- true);
- unsigned index = 1;
- return SaveFileAsWithFilter(owner,
- suggested_name,
- filter,
- L"",
- false,
- &index,
- final_name);
-}
-
-bool SaveFileAsWithFilter(HWND owner,
- const std::wstring& suggested_name,
- const std::wstring& filter,
- const std::wstring& def_ext,
- bool ignore_suggested_ext,
- unsigned* index,
- std::wstring* final_name) {
- DCHECK(final_name);
- // Having an empty filter makes for a bad user experience. We should always
- // specify a filter when saving.
- DCHECK(!filter.empty());
- std::wstring file_part = file_util::GetFilenameFromPath(suggested_name);
-
- // The size of the in/out buffer in number of characters we pass to win32
- // GetSaveFileName. From MSDN "The buffer must be large enough to store the
- // path and file name string or strings, including the terminating NULL
- // character. ... The buffer should be at least 256 characters long.".
- // _IsValidPathComDlg does a copy expecting at most MAX_PATH, otherwise will
- // result in an error of FNERR_INVALIDFILENAME. So we should only pass the
- // API a buffer of at most MAX_PATH.
- wchar_t file_name[MAX_PATH];
- base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
-
- OPENFILENAME save_as;
- // 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(&save_as, sizeof(save_as));
- save_as.lStructSize = sizeof(OPENFILENAME);
- save_as.hwndOwner = owner;
- save_as.hInstance = NULL;
-
- save_as.lpstrFilter = filter.empty() ? NULL : filter.c_str();
-
- save_as.lpstrCustomFilter = NULL;
- save_as.nMaxCustFilter = 0;
- save_as.nFilterIndex = *index;
- save_as.lpstrFile = file_name;
- save_as.nMaxFile = arraysize(file_name);
- save_as.lpstrFileTitle = NULL;
- save_as.nMaxFileTitle = 0;
-
- // Set up the initial directory for the dialog.
- std::wstring directory = file_util::GetDirectoryFromPath(suggested_name);
- save_as.lpstrInitialDir = directory.c_str();
- save_as.lpstrTitle = NULL;
- save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
- OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
- save_as.lpstrDefExt = &def_ext[0];
- save_as.lCustData = NULL;
-
- if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) {
- // The save as on Windows XP remembers its last position,
- // and if the screen resolution changed, it will be off screen.
- save_as.Flags |= OFN_ENABLEHOOK;
- save_as.lpfnHook = &SaveAsDialogHook;
- }
-
- // Must be NULL or 0.
- save_as.pvReserved = NULL;
- save_as.dwReserved = 0;
-
- if (!GetSaveFileName(&save_as)) {
- // Zero means the dialog was closed, otherwise we had an error.
- DWORD error_code = CommDlgExtendedError();
- if (error_code != 0) {
- NOTREACHED() << "GetSaveFileName failed with code: " << error_code;
- }
- return false;
- }
-
- // Return the user's choice.
- final_name->assign(save_as.lpstrFile);
- *index = save_as.nFilterIndex;
-
- // Figure out what filter got selected from the vector with embedded nulls.
- // NOTE: The filter contains a string with embedded nulls, such as:
- // JPG Image\0*.jpg\0All files\0*.*\0\0
- // The filter index is 1-based index for which pair got selected. So, using
- // the example above, if the first index was selected we need to skip 1
- // instance of null to get to "*.jpg".
- std::vector<std::wstring> filters;
- if (!filter.empty() && save_as.nFilterIndex > 0)
- SplitString(filter, '\0', &filters);
- std::wstring filter_selected;
- if (!filters.empty())
- filter_selected = filters[(2 * (save_as.nFilterIndex - 1)) + 1];
-
- // Get the extension that was suggested to the user (when the Save As dialog
- // was opened). For saving web pages, we skip this step since there may be
- // 'extension characters' in the title of the web page.
- std::wstring suggested_ext;
- if (!ignore_suggested_ext)
- suggested_ext = file_util::GetFileExtensionFromPath(suggested_name);
-
- // If we can't get the extension from the suggested_name, we use the default
- // extension passed in. This is to cover cases like when saving a web page,
- // where we get passed in a name without an extension and a default extension
- // along with it.
- if (suggested_ext.empty())
- suggested_ext = def_ext;
-
- *final_name =
- AppendExtensionIfNeeded(*final_name, filter_selected, suggested_ext);
- return true;
-}
-
-std::wstring AppendExtensionIfNeeded(const std::wstring& filename,
- const std::wstring& filter_selected,
- const std::wstring& suggested_ext) {
- std::wstring return_value = filename;
-
- // Get the extension the user ended up selecting.
- std::wstring selected_ext = file_util::GetFileExtensionFromPath(filename);
-
- if (filter_selected.empty() || filter_selected == L"*.*") {
- // If the user selects 'All files' we respect any extension given to us from
- // the File Save dialog. We also strip any trailing dots, which matches
- // Windows Explorer and is needed because Windows doesn't allow filenames
- // to have trailing dots. The GetSaveFileName dialog will not return a
- // string with only one or more dots.
- size_t index = return_value.find_last_not_of(L'.');
- if (index < return_value.size() - 1)
- return_value.resize(index + 1);
- } else {
- // User selected a specific filter (not *.*) so we need to check if the
- // extension provided has the same mime type. If it doesn't we append the
- // extension.
- std::string suggested_mime_type, selected_mime_type;
- if (suggested_ext != selected_ext &&
- (!net::GetMimeTypeFromExtension(suggested_ext, &suggested_mime_type) ||
- !net::GetMimeTypeFromExtension(selected_ext, &selected_mime_type) ||
- suggested_mime_type != selected_mime_type)) {
- return_value.append(L".");
- return_value.append(suggested_ext);
- }
- }
-
- return return_value;
-}
-
// Adjust the window to fit, returning true if the window was resized or moved.
static bool AdjustWindowToFit(HWND hwnd, const RECT& bounds) {
// Get the monitor.
diff --git a/app/win_util.h b/app/win_util.h
index 27a3358..802cc35 100644
--- a/app/win_util.h
+++ b/app/win_util.h
@@ -130,83 +130,6 @@ bool OpenItemViaShellNoZoneCheck(const FilePath& full_path);
// Returns 'true' on successful open, 'false' otherwise.
bool OpenItemWithExternalApp(const std::wstring& full_path);
-// Set up a filter for a Save/Open dialog, which will consist of |file_ext| file
-// extensions (internally separated by semicolons), |ext_desc| as the text
-// descriptions of the |file_ext| types (optional), and (optionally) the default
-// 'All Files' view. The purpose of the filter is to show only files of a
-// particular type in a Windows Save/Open dialog box. The resulting filter is
-// returned. The filters created here are:
-// 1. only files that have 'file_ext' as their extension
-// 2. all files (only added if 'include_all_files' is true)
-// Example:
-// file_ext: { "*.txt", "*.htm;*.html" }
-// ext_desc: { "Text Document" }
-// returned: "Text Document\0*.txt\0HTML Document\0*.htm;*.html\0"
-// "All Files\0*.*\0\0" (in one big string)
-// If a description is not provided for a file extension, it will be retrieved
-// from the registry. If the file extension does not exist in the registry, it
-// will be omitted from the filter, as it is likely a bogus extension.
-std::wstring FormatFilterForExtensions(
- const std::vector<std::wstring>& file_ext,
- const std::vector<std::wstring>& ext_desc,
- bool include_all_files);
-
-// Prompt the user for location to save a file. 'suggested_name' is a full path
-// that gives the dialog box a hint as to how to initialize itself.
-// For example, a 'suggested_name' of:
-// "C:\Documents and Settings\jojo\My Documents\picture.png"
-// will start the dialog in the "C:\Documents and Settings\jojo\My Documents\"
-// directory, and filter for .png file types.
-// 'owner' is the window to which the dialog box is modal, NULL for a modeless
-// dialog box.
-// On success, returns true and 'final_name' contains the full path of the file
-// that the user chose. On error, returns false, and 'final_name' is not
-// modified.
-// NOTE: DO NOT CALL THIS FUNCTION DIRECTLY! Instead use the helper objects in
-// browser/shell_dialogs.cc to do this asynchronously on a different
-// thread so that the app isn't jankified if the Windows shell dialog
-// takes a long time to display.
-bool SaveFileAs(HWND owner,
- const std::wstring& suggested_name,
- std::wstring* final_name);
-
-// Prompt the user for location to save a file.
-// Callers should provide the filter string, and also a filter index.
-// The parameter |index| indicates the initial index of filter description
-// and filter pattern for the dialog box. If |index| is zero or greater than
-// the number of total filter types, the system uses the first filter in the
-// |filter| buffer. |index| is used to specify the initial selected extension,
-// and when done contains the extension the user chose. The parameter
-// |final_name| returns the file name which contains the drive designator,
-// path, file name, and extension of the user selected file name. |def_ext| is
-// the default extension to give to the file if the user did not enter an
-// extension. If |ignore_suggested_ext| is true, any file extension contained in
-// |suggested_name| will not be used to generate the file name. This is useful
-// in the case of saving web pages, where we know the extension type already and
-// where |suggested_name| may contain a '.' character as a valid part of the
-// name, thus confusing our extension detection code.
-bool SaveFileAsWithFilter(HWND owner,
- const std::wstring& suggested_name,
- const std::wstring& filter,
- const std::wstring& def_ext,
- bool ignore_suggested_ext,
- unsigned* index,
- std::wstring* final_name);
-
-// This function takes the output of a SaveAs dialog: a filename, a filter and
-// the extension originally suggested to the user (shown in the dialog box) and
-// returns back the filename with the appropriate extension tacked on. For
-// example, if you pass in 'foo' as filename with filter '*.jpg' this function
-// will return 'foo.jpg'. It respects MIME types, so if you pass in 'foo.jpeg'
-// with filer '*.jpg' it will return 'foo.jpeg' (will not append .jpg).
-// |filename| should contain the filename selected in the SaveAs dialog box and
-// may include the path, |filter_selected| should be '*.something', for example
-// '*.*' or it can be blank (which is treated as *.*). |suggested_ext| should
-// contain the extension without the dot (.) in front, for example 'jpg'.
-std::wstring AppendExtensionIfNeeded(const std::wstring& filename,
- const std::wstring& filter_selected,
- const std::wstring& suggested_ext);
-
// If the window does not fit on the default monitor, it is moved and possibly
// resized appropriately.
void AdjustWindowToFit(HWND hwnd);
diff --git a/app/win_util_unittest.cc b/app/win_util_unittest.cc
index 18fb0d0..2c0eafc 100644
--- a/app/win_util_unittest.cc
+++ b/app/win_util_unittest.cc
@@ -82,13 +82,3 @@ static const struct filename_case {
{L"f.jpg", L"", L"jpg", L"f.jpg"},
{L"f.jpeg", L"", L"jpg", L"f.jpeg"},
};
-
-TEST(WinUtilTest, AppendingExtensions) {
- for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
- const filename_case& value = filename_cases[i];
- std::wstring result =
- win_util::AppendExtensionIfNeeded(value.filename, value.filter_selected,
- value.suggested_ext);
- EXPECT_EQ(value.result, result);
- }
-}
diff --git a/chrome/browser/views/shell_dialogs_win.cc b/chrome/browser/views/shell_dialogs_win.cc
index c668390..b745a8d 100644
--- a/chrome/browser/views/shell_dialogs_win.cc
+++ b/chrome/browser/views/shell_dialogs_win.cc
@@ -19,8 +19,356 @@
#include "base/scoped_comptr_win.h"
#include "base/thread.h"
#include "base/utf_string_conversions.h"
+#include "base/win_util.h"
#include "chrome/browser/chrome_thread.h"
+#include "grit/app_strings.h"
#include "grit/generated_resources.h"
+#include "net/base/mime_util.h"
+
+namespace {
+
+// This function takes the output of a SaveAs dialog: a filename, a filter and
+// the extension originally suggested to the user (shown in the dialog box) and
+// returns back the filename with the appropriate extension tacked on. For
+// example, if you pass in 'foo' as filename with filter '*.jpg' this function
+// will return 'foo.jpg'. It respects MIME types, so if you pass in 'foo.jpeg'
+// with filer '*.jpg' it will return 'foo.jpeg' (will not append .jpg).
+// |filename| should contain the filename selected in the SaveAs dialog box and
+// may include the path, |filter_selected| should be '*.something', for example
+// '*.*' or it can be blank (which is treated as *.*). |suggested_ext| should
+// contain the extension without the dot (.) in front, for example 'jpg'.
+std::wstring AppendExtensionIfNeeded(const std::wstring& filename,
+ const std::wstring& filter_selected,
+ const std::wstring& suggested_ext) {
+ std::wstring return_value = filename;
+
+ // Get the extension the user ended up selecting.
+ std::wstring selected_ext = file_util::GetFileExtensionFromPath(filename);
+
+ if (filter_selected.empty() || filter_selected == L"*.*") {
+ // If the user selects 'All files' we respect any extension given to us from
+ // the File Save dialog. We also strip any trailing dots, which matches
+ // Windows Explorer and is needed because Windows doesn't allow filenames
+ // to have trailing dots. The GetSaveFileName dialog will not return a
+ // string with only one or more dots.
+ size_t index = return_value.find_last_not_of(L'.');
+ if (index < return_value.size() - 1)
+ return_value.resize(index + 1);
+ } else {
+ // User selected a specific filter (not *.*) so we need to check if the
+ // extension provided has the same mime type. If it doesn't we append the
+ // extension.
+ std::string suggested_mime_type, selected_mime_type;
+ if (suggested_ext != selected_ext &&
+ (!net::GetMimeTypeFromExtension(suggested_ext, &suggested_mime_type) ||
+ !net::GetMimeTypeFromExtension(selected_ext, &selected_mime_type) ||
+ suggested_mime_type != selected_mime_type)) {
+ return_value.append(L".");
+ return_value.append(suggested_ext);
+ }
+ }
+
+ return return_value;
+}
+
+// Get the file type description from the registry. This will be "Text Document"
+// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't
+// have an entry for the file type, we return false, true if the description was
+// found. 'file_ext' must be in form ".txt".
+static bool GetRegistryDescriptionFromExtension(const std::wstring& file_ext,
+ std::wstring* reg_description) {
+ DCHECK(reg_description);
+ RegKey reg_ext(HKEY_CLASSES_ROOT, file_ext.c_str(), KEY_READ);
+ std::wstring reg_app;
+ if (reg_ext.ReadValue(NULL, &reg_app) && !reg_app.empty()) {
+ RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ);
+ if (reg_link.ReadValue(NULL, reg_description))
+ return true;
+ }
+ return false;
+}
+
+// Set up a filter for a Save/Open dialog, which will consist of |file_ext| file
+// extensions (internally separated by semicolons), |ext_desc| as the text
+// descriptions of the |file_ext| types (optional), and (optionally) the default
+// 'All Files' view. The purpose of the filter is to show only files of a
+// particular type in a Windows Save/Open dialog box. The resulting filter is
+// returned. The filters created here are:
+// 1. only files that have 'file_ext' as their extension
+// 2. all files (only added if 'include_all_files' is true)
+// Example:
+// file_ext: { "*.txt", "*.htm;*.html" }
+// ext_desc: { "Text Document" }
+// returned: "Text Document\0*.txt\0HTML Document\0*.htm;*.html\0"
+// "All Files\0*.*\0\0" (in one big string)
+// If a description is not provided for a file extension, it will be retrieved
+// from the registry. If the file extension does not exist in the registry, it
+// will be omitted from the filter, as it is likely a bogus extension.
+std::wstring FormatFilterForExtensions(
+ const std::vector<std::wstring>& file_ext,
+ const std::vector<std::wstring>& ext_desc,
+ bool include_all_files) {
+ const std::wstring all_ext = L"*.*";
+ const std::wstring all_desc = l10n_util::GetString(IDS_APP_SAVEAS_ALL_FILES);
+
+ DCHECK(file_ext.size() >= ext_desc.size());
+
+ std::wstring result;
+
+ for (size_t i = 0; i < file_ext.size(); ++i) {
+ std::wstring ext = file_ext[i];
+ std::wstring desc;
+ if (i < ext_desc.size())
+ desc = ext_desc[i];
+
+ if (ext.empty()) {
+ // Force something reasonable to appear in the dialog box if there is no
+ // extension provided.
+ include_all_files = true;
+ continue;
+ }
+
+ if (desc.empty()) {
+ DCHECK(ext.find(L'.') != std::wstring::npos);
+ std::wstring first_extension = ext.substr(ext.find(L'.'));
+ size_t first_separator_index = first_extension.find(L';');
+ if (first_separator_index != std::wstring::npos)
+ first_extension = first_extension.substr(0, first_separator_index);
+
+ // Find the extension name without the preceeding '.' character.
+ std::wstring ext_name = first_extension;
+ size_t ext_index = ext_name.find_first_not_of(L'.');
+ if (ext_index != std::wstring::npos)
+ ext_name = ext_name.substr(ext_index);
+
+ if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) {
+ // The extension doesn't exist in the registry. Create a description
+ // based on the unknown extension type (i.e. if the extension is .qqq,
+ // the we create a description "QQQ File (.qqq)").
+ include_all_files = true;
+ desc = l10n_util::GetStringF(IDS_APP_SAVEAS_EXTENSION_FORMAT,
+ l10n_util::ToUpper(ext_name),
+ ext_name);
+ }
+ if (desc.empty())
+ desc = L"*." + ext_name;
+ }
+
+ result.append(desc.c_str(), desc.size() + 1); // Append NULL too.
+ result.append(ext.c_str(), ext.size() + 1);
+ }
+
+ if (include_all_files) {
+ result.append(all_desc.c_str(), all_desc.size() + 1);
+ result.append(all_ext.c_str(), all_ext.size() + 1);
+ }
+
+ result.append(1, '\0'); // Double NULL required.
+ return result;
+}
+
+// Enforce visible dialog box.
+UINT_PTR CALLBACK SaveAsDialogHook(HWND dialog, UINT message,
+ WPARAM wparam, LPARAM lparam) {
+ static const UINT kPrivateMessage = 0x2F3F;
+ switch (message) {
+ case WM_INITDIALOG: {
+ // Do nothing here. Just post a message to defer actual processing.
+ PostMessage(dialog, kPrivateMessage, 0, 0);
+ return TRUE;
+ }
+ case kPrivateMessage: {
+ // The dialog box is the parent of the current handle.
+ HWND real_dialog = GetParent(dialog);
+
+ // Retrieve the final size.
+ RECT dialog_rect;
+ GetWindowRect(real_dialog, &dialog_rect);
+
+ // Verify that the upper left corner is visible.
+ POINT point = { dialog_rect.left, dialog_rect.top };
+ HMONITOR monitor1 = MonitorFromPoint(point, MONITOR_DEFAULTTONULL);
+ point.x = dialog_rect.right;
+ point.y = dialog_rect.bottom;
+
+ // Verify that the lower right corner is visible.
+ HMONITOR monitor2 = MonitorFromPoint(point, MONITOR_DEFAULTTONULL);
+ if (monitor1 && monitor2)
+ return 0;
+
+ // Some part of the dialog box is not visible, fix it by moving is to the
+ // client rect position of the browser window.
+ HWND parent_window = GetParent(real_dialog);
+ if (!parent_window)
+ return 0;
+ WINDOWINFO parent_info;
+ parent_info.cbSize = sizeof(WINDOWINFO);
+ GetWindowInfo(parent_window, &parent_info);
+ SetWindowPos(real_dialog, NULL,
+ parent_info.rcClient.left,
+ parent_info.rcClient.top,
+ 0, 0, // Size.
+ SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE |
+ SWP_NOZORDER);
+
+ return 0;
+ }
+ }
+ return 0;
+}
+
+// Prompt the user for location to save a file.
+// Callers should provide the filter string, and also a filter index.
+// The parameter |index| indicates the initial index of filter description
+// and filter pattern for the dialog box. If |index| is zero or greater than
+// the number of total filter types, the system uses the first filter in the
+// |filter| buffer. |index| is used to specify the initial selected extension,
+// and when done contains the extension the user chose. The parameter
+// |final_name| returns the file name which contains the drive designator,
+// path, file name, and extension of the user selected file name. |def_ext| is
+// the default extension to give to the file if the user did not enter an
+// extension. If |ignore_suggested_ext| is true, any file extension contained in
+// |suggested_name| will not be used to generate the file name. This is useful
+// in the case of saving web pages, where we know the extension type already and
+// where |suggested_name| may contain a '.' character as a valid part of the
+// name, thus confusing our extension detection code.
+bool SaveFileAsWithFilter(HWND owner,
+ const std::wstring& suggested_name,
+ const std::wstring& filter,
+ const std::wstring& def_ext,
+ bool ignore_suggested_ext,
+ unsigned* index,
+ std::wstring* final_name) {
+ DCHECK(final_name);
+ // Having an empty filter makes for a bad user experience. We should always
+ // specify a filter when saving.
+ DCHECK(!filter.empty());
+ std::wstring file_part = file_util::GetFilenameFromPath(suggested_name);
+
+ // The size of the in/out buffer in number of characters we pass to win32
+ // GetSaveFileName. From MSDN "The buffer must be large enough to store the
+ // path and file name string or strings, including the terminating NULL
+ // character. ... The buffer should be at least 256 characters long.".
+ // _IsValidPathComDlg does a copy expecting at most MAX_PATH, otherwise will
+ // result in an error of FNERR_INVALIDFILENAME. So we should only pass the
+ // API a buffer of at most MAX_PATH.
+ wchar_t file_name[MAX_PATH];
+ base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
+
+ OPENFILENAME save_as;
+ // 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(&save_as, sizeof(save_as));
+ save_as.lStructSize = sizeof(OPENFILENAME);
+ save_as.hwndOwner = owner;
+ save_as.hInstance = NULL;
+
+ save_as.lpstrFilter = filter.empty() ? NULL : filter.c_str();
+
+ save_as.lpstrCustomFilter = NULL;
+ save_as.nMaxCustFilter = 0;
+ save_as.nFilterIndex = *index;
+ save_as.lpstrFile = file_name;
+ save_as.nMaxFile = arraysize(file_name);
+ save_as.lpstrFileTitle = NULL;
+ save_as.nMaxFileTitle = 0;
+
+ // Set up the initial directory for the dialog.
+ std::wstring directory = file_util::GetDirectoryFromPath(suggested_name);
+ save_as.lpstrInitialDir = directory.c_str();
+ save_as.lpstrTitle = NULL;
+ save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
+ OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
+ save_as.lpstrDefExt = &def_ext[0];
+ save_as.lCustData = NULL;
+
+ if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) {
+ // The save as on Windows XP remembers its last position,
+ // and if the screen resolution changed, it will be off screen.
+ save_as.Flags |= OFN_ENABLEHOOK;
+ save_as.lpfnHook = &SaveAsDialogHook;
+ }
+
+ // Must be NULL or 0.
+ save_as.pvReserved = NULL;
+ save_as.dwReserved = 0;
+
+ if (!GetSaveFileName(&save_as)) {
+ // Zero means the dialog was closed, otherwise we had an error.
+ DWORD error_code = CommDlgExtendedError();
+ if (error_code != 0) {
+ NOTREACHED() << "GetSaveFileName failed with code: " << error_code;
+ }
+ return false;
+ }
+
+ // Return the user's choice.
+ final_name->assign(save_as.lpstrFile);
+ *index = save_as.nFilterIndex;
+
+ // Figure out what filter got selected from the vector with embedded nulls.
+ // NOTE: The filter contains a string with embedded nulls, such as:
+ // JPG Image\0*.jpg\0All files\0*.*\0\0
+ // The filter index is 1-based index for which pair got selected. So, using
+ // the example above, if the first index was selected we need to skip 1
+ // instance of null to get to "*.jpg".
+ std::vector<std::wstring> filters;
+ if (!filter.empty() && save_as.nFilterIndex > 0)
+ SplitString(filter, '\0', &filters);
+ std::wstring filter_selected;
+ if (!filters.empty())
+ filter_selected = filters[(2 * (save_as.nFilterIndex - 1)) + 1];
+
+ // Get the extension that was suggested to the user (when the Save As dialog
+ // was opened). For saving web pages, we skip this step since there may be
+ // 'extension characters' in the title of the web page.
+ std::wstring suggested_ext;
+ if (!ignore_suggested_ext)
+ suggested_ext = file_util::GetFileExtensionFromPath(suggested_name);
+
+ // If we can't get the extension from the suggested_name, we use the default
+ // extension passed in. This is to cover cases like when saving a web page,
+ // where we get passed in a name without an extension and a default extension
+ // along with it.
+ if (suggested_ext.empty())
+ suggested_ext = def_ext;
+
+ *final_name =
+ AppendExtensionIfNeeded(*final_name, filter_selected, suggested_ext);
+ return true;
+}
+
+// Prompt the user for location to save a file. 'suggested_name' is a full path
+// that gives the dialog box a hint as to how to initialize itself.
+// For example, a 'suggested_name' of:
+// "C:\Documents and Settings\jojo\My Documents\picture.png"
+// will start the dialog in the "C:\Documents and Settings\jojo\My Documents\"
+// directory, and filter for .png file types.
+// 'owner' is the window to which the dialog box is modal, NULL for a modeless
+// dialog box.
+// On success, returns true and 'final_name' contains the full path of the file
+// that the user chose. On error, returns false, and 'final_name' is not
+// modified.
+bool SaveFileAs(HWND owner,
+ const std::wstring& suggested_name,
+ std::wstring* final_name) {
+ std::wstring file_ext = file_util::GetFileExtensionFromPath(suggested_name);
+ file_ext.insert(0, L"*.");
+ std::wstring filter = FormatFilterForExtensions(
+ std::vector<std::wstring>(1, file_ext),
+ std::vector<std::wstring>(),
+ true);
+ unsigned index = 1;
+ return SaveFileAsWithFilter(owner,
+ suggested_name,
+ filter,
+ L"",
+ false,
+ &index,
+ final_name);
+}
+
+} // namespace
// Helpers to show certain types of Windows shell dialogs in a way that doesn't
// block the UI of the entire app.
@@ -341,7 +689,7 @@ void SelectFileDialogImpl::ExecuteSelectFile(
}
exts.push_back(ext_string);
}
- std::wstring filter = win_util::FormatFilterForExtensions(
+ std::wstring filter = FormatFilterForExtensions(
exts,
params.file_types.extension_description_overrides,
params.file_types.include_all_files);
@@ -355,7 +703,7 @@ void SelectFileDialogImpl::ExecuteSelectFile(
&path);
} else if (params.type == SELECT_SAVEAS_FILE) {
std::wstring path_as_wstring = path.ToWStringHack();
- success = win_util::SaveFileAsWithFilter(params.run_state.owner,
+ success = SaveFileAsWithFilter(params.run_state.owner,
params.default_path.ToWStringHack(), filter,
params.default_extension, false, &filter_index, &path_as_wstring);
if (success) {