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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/select_file_dialog_extension.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/singleton.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/app_mode/app_mode_utils.h"
15 #include "chrome/browser/apps/app_window_registry_util.h"
16 #include "chrome/browser/chromeos/file_manager/app_id.h"
17 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
18 #include "chrome/browser/chromeos/file_manager/select_file_dialog_util.h"
19 #include "chrome/browser/chromeos/file_manager/url_util.h"
20 #include "chrome/browser/chromeos/login/ui/login_web_dialog.h"
21 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/extension_view_host.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/sessions/session_tab_helper.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/chrome_select_file_policy.h"
29 #include "chrome/browser/ui/host_desktop.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/browser/ui/views/extensions/extension_dialog.h"
32 #include "chrome/common/pref_names.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "extensions/browser/app_window/app_window.h"
35 #include "extensions/browser/app_window/native_app_window.h"
36 #include "extensions/browser/extension_system.h"
37 #include "ui/base/base_window.h"
38 #include "ui/shell_dialogs/selected_file_info.h"
39 #include "ui/views/widget/widget.h"
41 using extensions::AppWindow
;
42 using content::BrowserThread
;
46 const int kFileManagerWidth
= 972; // pixels
47 const int kFileManagerHeight
= 640; // pixels
48 const int kFileManagerMinimumWidth
= 640; // pixels
49 const int kFileManagerMinimumHeight
= 240; // pixels
51 // Holds references to file manager dialogs that have callbacks pending
52 // to their listeners.
55 static PendingDialog
* GetInstance();
56 void Add(SelectFileDialogExtension::RoutingID id
,
57 scoped_refptr
<SelectFileDialogExtension
> dialog
);
58 void Remove(SelectFileDialogExtension::RoutingID id
);
59 scoped_refptr
<SelectFileDialogExtension
> Find(
60 SelectFileDialogExtension::RoutingID id
);
63 friend struct DefaultSingletonTraits
<PendingDialog
>;
64 typedef std::map
<SelectFileDialogExtension::RoutingID
,
65 scoped_refptr
<SelectFileDialogExtension
> > Map
;
70 PendingDialog
* PendingDialog::GetInstance() {
71 return Singleton
<PendingDialog
>::get();
74 void PendingDialog::Add(SelectFileDialogExtension::RoutingID id
,
75 scoped_refptr
<SelectFileDialogExtension
> dialog
) {
77 if (map_
.find(id
) == map_
.end())
78 map_
.insert(std::make_pair(id
, dialog
));
80 DLOG(WARNING
) << "Duplicate pending dialog " << id
;
83 void PendingDialog::Remove(SelectFileDialogExtension::RoutingID id
) {
87 scoped_refptr
<SelectFileDialogExtension
> PendingDialog::Find(
88 SelectFileDialogExtension::RoutingID id
) {
89 Map::const_iterator it
= map_
.find(id
);
95 // Given |owner_window| finds corresponding |base_window|, it's associated
96 // |web_contents| and |profile|.
97 void FindRuntimeContext(gfx::NativeWindow owner_window
,
98 ui::BaseWindow
** base_window
,
99 content::WebContents
** web_contents
) {
101 *web_contents
= NULL
;
102 // To get the base_window and web contents, either a Browser or AppWindow is
104 Browser
* owner_browser
= NULL
;
105 AppWindow
* app_window
= NULL
;
107 // If owner_window is supplied, use that to find a browser or a app window.
109 owner_browser
= chrome::FindBrowserWithWindow(owner_window
);
110 if (!owner_browser
) {
111 // If an owner_window was supplied but we couldn't find a browser, this
112 // could be for a app window.
114 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
120 DCHECK(!app_window
->window_type_is_panel());
121 *base_window
= app_window
->GetBaseWindow();
122 *web_contents
= app_window
->web_contents();
124 // If the owning window is still unknown, this could be a background page or
125 // and extension popup. Use the last active browser.
126 if (!owner_browser
) {
128 chrome::FindLastActiveWithHostDesktopType(chrome::GetActiveDesktop());
131 *base_window
= owner_browser
->window();
132 *web_contents
= owner_browser
->tab_strip_model()->GetActiveWebContents();
136 // In ChromeOS kiosk launch mode, we can still show file picker for
137 // certificate manager dialog. There are no browser or webapp window
138 // instances present in this case.
139 if (chrome::IsRunningInForcedAppMode() && !(*web_contents
))
140 *web_contents
= chromeos::LoginWebDialog::GetCurrentWebContents();
147 /////////////////////////////////////////////////////////////////////////////
150 SelectFileDialogExtension::RoutingID
151 SelectFileDialogExtension::GetRoutingIDFromWebContents(
152 const content::WebContents
* web_contents
) {
153 // Use the raw pointer value as the identifier. Previously we have used the
154 // tab ID for the purpose, but some web_contents, especially those of the
155 // packaged apps, don't have tab IDs assigned.
159 // TODO(jamescook): Move this into a new file shell_dialogs_chromeos.cc
161 SelectFileDialogExtension
* SelectFileDialogExtension::Create(
163 ui::SelectFilePolicy
* policy
) {
164 return new SelectFileDialogExtension(listener
, policy
);
167 SelectFileDialogExtension::SelectFileDialogExtension(
169 ui::SelectFilePolicy
* policy
)
170 : SelectFileDialog(listener
, policy
),
171 has_multiple_file_type_choices_(false),
175 selection_type_(CANCEL
),
180 SelectFileDialogExtension::~SelectFileDialogExtension() {
181 if (extension_dialog_
.get())
182 extension_dialog_
->ObserverDestroyed();
185 bool SelectFileDialogExtension::IsRunning(
186 gfx::NativeWindow owner_window
) const {
187 return owner_window_
== owner_window
;
190 void SelectFileDialogExtension::ListenerDestroyed() {
193 PendingDialog::GetInstance()->Remove(routing_id_
);
196 void SelectFileDialogExtension::ExtensionDialogClosing(
197 ExtensionDialog
* /*dialog*/) {
199 owner_window_
= NULL
;
200 // Release our reference to the underlying dialog to allow it to close.
201 extension_dialog_
= NULL
;
202 PendingDialog::GetInstance()->Remove(routing_id_
);
203 // Actually invoke the appropriate callback on our listener.
207 void SelectFileDialogExtension::ExtensionTerminated(
208 ExtensionDialog
* dialog
) {
209 // The extension would have been unloaded because of the termination,
211 std::string extension_id
= dialog
->host()->extension()->id();
212 // Reload the extension after a bit; the extension may not have been unloaded
213 // yet. We don't want to try to reload the extension only to have the Unload
214 // code execute after us and re-unload the extension.
216 // TODO(rkc): This is ugly. The ideal solution is that we shouldn't need to
217 // reload the extension at all - when we try to open the extension the next
218 // time, the extension subsystem would automatically reload it for us. At
219 // this time though this is broken because of some faulty wiring in
220 // extensions::ProcessManager::CreateViewHost. Once that is fixed, remove
223 base::MessageLoop::current()->PostTask(
225 base::Bind(&ExtensionService::ReloadExtension
,
226 base::Unretained(extensions::ExtensionSystem::Get(profile_
)
227 ->extension_service()),
231 dialog
->GetWidget()->Close();
235 void SelectFileDialogExtension::OnFileSelected(
236 RoutingID routing_id
,
237 const ui::SelectedFileInfo
& file
,
239 scoped_refptr
<SelectFileDialogExtension
> dialog
=
240 PendingDialog::GetInstance()->Find(routing_id
);
243 dialog
->selection_type_
= SINGLE_FILE
;
244 dialog
->selection_files_
.clear();
245 dialog
->selection_files_
.push_back(file
);
246 dialog
->selection_index_
= index
;
250 void SelectFileDialogExtension::OnMultiFilesSelected(
251 RoutingID routing_id
,
252 const std::vector
<ui::SelectedFileInfo
>& files
) {
253 scoped_refptr
<SelectFileDialogExtension
> dialog
=
254 PendingDialog::GetInstance()->Find(routing_id
);
257 dialog
->selection_type_
= MULTIPLE_FILES
;
258 dialog
->selection_files_
= files
;
259 dialog
->selection_index_
= 0;
263 void SelectFileDialogExtension::OnFileSelectionCanceled(RoutingID routing_id
) {
264 scoped_refptr
<SelectFileDialogExtension
> dialog
=
265 PendingDialog::GetInstance()->Find(routing_id
);
268 dialog
->selection_type_
= CANCEL
;
269 dialog
->selection_files_
.clear();
270 dialog
->selection_index_
= 0;
273 content::RenderViewHost
* SelectFileDialogExtension::GetRenderViewHost() {
274 if (extension_dialog_
.get())
275 return extension_dialog_
->host()->render_view_host();
279 void SelectFileDialogExtension::NotifyListener() {
282 switch (selection_type_
) {
284 listener_
->FileSelectionCanceled(params_
);
287 listener_
->FileSelectedWithExtraInfo(selection_files_
[0],
292 listener_
->MultiFilesSelectedWithExtraInfo(selection_files_
, params_
);
300 void SelectFileDialogExtension::AddPending(RoutingID routing_id
) {
301 PendingDialog::GetInstance()->Add(routing_id
, this);
305 bool SelectFileDialogExtension::PendingExists(RoutingID routing_id
) {
306 return PendingDialog::GetInstance()->Find(routing_id
).get() != NULL
;
309 bool SelectFileDialogExtension::HasMultipleFileTypeChoicesImpl() {
310 return has_multiple_file_type_choices_
;
313 void SelectFileDialogExtension::SelectFileImpl(
315 const base::string16
& title
,
316 const base::FilePath
& default_path
,
317 const FileTypeInfo
* file_types
,
319 const base::FilePath::StringType
& default_extension
,
320 gfx::NativeWindow owner_window
,
323 LOG(ERROR
) << "File dialog already in use!";
327 // The base window to associate the dialog with.
328 ui::BaseWindow
* base_window
= NULL
;
330 // The web contents to associate the dialog with.
331 content::WebContents
* web_contents
= NULL
;
332 FindRuntimeContext(owner_window
, &base_window
, &web_contents
);
334 profile_
= Profile::FromBrowserContext(web_contents
->GetBrowserContext());
337 // Check if we have another dialog opened for the contents. It's unlikely, but
338 // possible. In such situation, discard this request.
339 RoutingID routing_id
= GetRoutingIDFromWebContents(web_contents
);
340 if (PendingExists(routing_id
))
343 const PrefService
* pref_service
= profile_
->GetPrefs();
344 DCHECK(pref_service
);
346 base::FilePath
download_default_path(
347 pref_service
->GetFilePath(prefs::kDownloadDefaultDirectory
));
349 base::FilePath selection_path
= default_path
.IsAbsolute() ?
350 default_path
: download_default_path
.Append(default_path
.BaseName());
352 base::FilePath fallback_path
= profile_
->last_selected_directory().empty() ?
353 download_default_path
: profile_
->last_selected_directory();
355 // Convert the above absolute paths to file system URLs.
357 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
360 file_manager::kFileManagerAppId
,
362 // Due to the current design, an invalid temporal cache file path may passed
363 // as |default_path| (crbug.com/178013 #9-#11). In such a case, we use the
364 // last selected directory as a workaround. Real fix is tracked at
366 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
368 fallback_path
.Append(default_path
.BaseName()),
369 file_manager::kFileManagerAppId
,
371 DVLOG(1) << "Unable to resolve the selection URL.";
375 GURL current_directory_url
;
376 base::FilePath current_directory_path
= selection_path
.DirName();
377 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
379 current_directory_path
,
380 file_manager::kFileManagerAppId
,
381 ¤t_directory_url
)) {
382 // Fallback if necessary, see the comment above.
383 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
386 file_manager::kFileManagerAppId
,
387 ¤t_directory_url
)) {
388 DVLOG(1) << "Unable to resolve the current directory URL for: "
389 << fallback_path
.value();
393 has_multiple_file_type_choices_
=
394 !file_types
|| (file_types
->extensions
.size() > 1);
396 GURL file_manager_url
=
397 file_manager::util::GetFileManagerMainPageUrlWithParams(
400 current_directory_url
,
402 default_path
.BaseName().value(),
407 ExtensionDialog
* dialog
= ExtensionDialog::Show(
409 base_window
? base_window
->GetNativeWindow() : owner_window
,
414 kFileManagerMinimumWidth
,
415 kFileManagerMinimumHeight
,
416 file_manager::util::GetSelectFileDialogTitle(type
),
417 this /* ExtensionDialog::Observer */);
419 LOG(ERROR
) << "Unable to create extension dialog";
423 // Connect our listener to FileDialogFunction's per-tab callbacks.
424 AddPending(routing_id
);
426 extension_dialog_
= dialog
;
428 routing_id_
= routing_id
;
429 owner_window_
= owner_window
;