Skip to content

Commit 0f4f4b2

Browse files
committed
separate platform implementations for shell_download_manager_delegate
1 parent 02d78f4 commit 0f4f4b2

File tree

4 files changed

+157
-62
lines changed

4 files changed

+157
-62
lines changed

nw.gypi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@
148148
'src/browser/shell_devtools_delegate.h',
149149
'src/browser/shell_download_manager_delegate.cc',
150150
'src/browser/shell_download_manager_delegate.h',
151+
'src/browser/shell_download_manager_delegate_win.cc',
152+
'src/browser/shell_download_manager_delegate_gtk.cc',
153+
'src/browser/shell_download_manager_delegate_mac.mm',
151154
'src/browser/shell_javascript_dialog_creator.cc',
152155
'src/browser/shell_javascript_dialog_creator.h',
153156
'src/browser/shell_javascript_dialog_gtk.cc',

src/browser/shell_download_manager_delegate.cc

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -134,68 +134,6 @@ void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
134134
ChooseDownloadPath(download_id, callback, suggested_path);
135135
}
136136

137-
void ShellDownloadManagerDelegate::ChooseDownloadPath(
138-
int32 download_id,
139-
const DownloadTargetCallback& callback,
140-
const FilePath& suggested_path) {
141-
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142-
DownloadItem* item = download_manager_->GetDownload(download_id);
143-
if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
144-
return;
145-
146-
FilePath result;
147-
#if defined(OS_WIN)
148-
std::wstring file_part = FilePath(suggested_path).BaseName().value();
149-
wchar_t file_name[MAX_PATH];
150-
base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
151-
OPENFILENAME save_as;
152-
ZeroMemory(&save_as, sizeof(save_as));
153-
save_as.lStructSize = sizeof(OPENFILENAME);
154-
save_as.hwndOwner = item->GetWebContents()->GetNativeView();
155-
save_as.lpstrFile = file_name;
156-
save_as.nMaxFile = arraysize(file_name);
157-
158-
std::wstring directory;
159-
if (!suggested_path.empty())
160-
directory = suggested_path.DirName().value();
161-
162-
save_as.lpstrInitialDir = directory.c_str();
163-
save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
164-
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
165-
166-
if (GetSaveFileName(&save_as))
167-
result = FilePath(std::wstring(save_as.lpstrFile));
168-
#elif defined(TOOLKIT_GTK)
169-
GtkWidget *dialog;
170-
gfx::NativeWindow parent_window;
171-
std::string base_name = FilePath(suggested_path).BaseName().value();
172-
173-
parent_window = item->GetWebContents()->GetView()->GetTopLevelNativeWindow();
174-
dialog = gtk_file_chooser_dialog_new("Save File",
175-
parent_window,
176-
GTK_FILE_CHOOSER_ACTION_SAVE,
177-
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
178-
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
179-
NULL);
180-
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
181-
TRUE);
182-
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog),
183-
base_name.c_str());
184-
185-
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
186-
char *filename;
187-
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
188-
result = FilePath(filename);
189-
}
190-
gtk_widget_destroy(dialog);
191-
#else
192-
NOTIMPLEMENTED();
193-
#endif
194-
195-
callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
196-
DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
197-
}
198-
199137
void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
200138
const FilePath& default_download_path) {
201139
default_download_path_ = default_download_path;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2012 Intel Corp
2+
// Copyright (c) 2012 The Chromium Authors
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
8+
// pies of the Software, and to permit persons to whom the Software is furnished
9+
// to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in al
12+
// l copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
15+
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
16+
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
17+
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
18+
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
21+
#include "content/nw/src/browser/shell_download_manager_delegate.h"
22+
23+
#if defined(TOOLKIT_GTK)
24+
#include <gtk/gtk.h>
25+
#endif
26+
27+
#include "base/bind.h"
28+
#include "base/file_util.h"
29+
#include "base/logging.h"
30+
#include "base/string_util.h"
31+
#include "base/utf_string_conversions.h"
32+
#include "content/public/browser/browser_context.h"
33+
#include "content/public/browser/browser_thread.h"
34+
#include "content/public/browser/download_manager.h"
35+
#include "content/public/browser/web_contents.h"
36+
#include "content/public/browser/web_contents_view.h"
37+
#include "net/base/net_util.h"
38+
39+
namespace content {
40+
41+
void ShellDownloadManagerDelegate::ChooseDownloadPath(
42+
int32 download_id,
43+
const DownloadTargetCallback& callback,
44+
const FilePath& suggested_path) {
45+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
46+
DownloadItem* item = download_manager_->GetDownload(download_id);
47+
if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
48+
return;
49+
50+
FilePath result;
51+
GtkWidget *dialog;
52+
gfx::NativeWindow parent_window;
53+
std::string base_name = FilePath(suggested_path).BaseName().value();
54+
55+
parent_window = item->GetWebContents()->GetView()->GetTopLevelNativeWindow();
56+
dialog = gtk_file_chooser_dialog_new("Save File",
57+
parent_window,
58+
GTK_FILE_CHOOSER_ACTION_SAVE,
59+
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
60+
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
61+
NULL);
62+
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
63+
TRUE);
64+
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog),
65+
base_name.c_str());
66+
67+
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
68+
char *filename;
69+
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
70+
result = FilePath(filename);
71+
}
72+
gtk_widget_destroy(dialog);
73+
74+
callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
75+
DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
76+
}
77+
} // namespace content
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2012 Intel Corp
2+
// Copyright (c) 2012 The Chromium Authors
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
8+
// pies of the Software, and to permit persons to whom the Software is furnished
9+
// to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in al
12+
// l copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
15+
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
16+
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
17+
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
18+
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
21+
#include "content/nw/src/browser/shell_download_manager_delegate.h"
22+
23+
#if defined(OS_WIN)
24+
#include <windows.h>
25+
#include <commdlg.h>
26+
#endif
27+
28+
#include "base/bind.h"
29+
#include "base/file_util.h"
30+
#include "base/logging.h"
31+
#include "base/string_util.h"
32+
#include "base/utf_string_conversions.h"
33+
#include "content/public/browser/browser_context.h"
34+
#include "content/public/browser/browser_thread.h"
35+
#include "content/public/browser/download_manager.h"
36+
#include "content/public/browser/web_contents.h"
37+
#include "content/public/browser/web_contents_view.h"
38+
#include "net/base/net_util.h"
39+
40+
namespace content {
41+
42+
void ShellDownloadManagerDelegate::ChooseDownloadPath(
43+
int32 download_id,
44+
const DownloadTargetCallback& callback,
45+
const FilePath& suggested_path) {
46+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
47+
DownloadItem* item = download_manager_->GetDownload(download_id);
48+
if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
49+
return;
50+
51+
FilePath result;
52+
53+
std::wstring file_part = FilePath(suggested_path).BaseName().value();
54+
wchar_t file_name[MAX_PATH];
55+
base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
56+
OPENFILENAME save_as;
57+
ZeroMemory(&save_as, sizeof(save_as));
58+
save_as.lStructSize = sizeof(OPENFILENAME);
59+
save_as.hwndOwner = item->GetWebContents()->GetNativeView();
60+
save_as.lpstrFile = file_name;
61+
save_as.nMaxFile = arraysize(file_name);
62+
63+
std::wstring directory;
64+
if (!suggested_path.empty())
65+
directory = suggested_path.DirName().value();
66+
67+
save_as.lpstrInitialDir = directory.c_str();
68+
save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
69+
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
70+
71+
if (GetSaveFileName(&save_as))
72+
result = FilePath(std::wstring(save_as.lpstrFile));
73+
74+
callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
75+
DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
76+
}
77+
} // namespace content

0 commit comments

Comments
 (0)