1 // Copyright 2013 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/chromeos/extensions/file_manager/private_api_dialog.h"
7 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
8 #include "chrome/browser/extensions/extension_function_dispatcher.h"
9 #include "chrome/browser/ui/views/select_file_dialog_extension.h"
10 #include "chrome/common/extensions/api/file_browser_private.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h"
13 #include "ui/shell_dialogs/selected_file_info.h"
15 using content::BrowserThread;
17 namespace extensions {
21 // Returns the WebContents of the tab associated with the dispatcher. Returns
23 content::WebContents* GetWebContents(ExtensionFunctionDispatcher* dispatcher) {
25 LOG(WARNING) << "No dispatcher";
28 if (!dispatcher->delegate()) {
29 LOG(WARNING) << "No delegate";
32 content::WebContents* web_contents =
33 dispatcher->delegate()->GetAssociatedWebContents();
35 LOG(WARNING) << "No associated tab contents";
41 // Computes the routing ID for SelectFileDialogExtension from the |dispatcher|.
42 SelectFileDialogExtension::RoutingID GetFileDialogRoutingID(
43 ExtensionFunctionDispatcher* dispatcher) {
44 return SelectFileDialogExtension::GetRoutingIDFromWebContents(
45 GetWebContents(dispatcher));
50 bool FileBrowserPrivateCancelDialogFunction::RunImpl() {
51 const SelectFileDialogExtension::RoutingID routing_id =
52 GetFileDialogRoutingID(dispatcher());
53 SelectFileDialogExtension::OnFileSelectionCanceled(routing_id);
58 bool FileBrowserPrivateSelectFileFunction::RunImpl() {
59 using extensions::api::file_browser_private::SelectFile::Params;
60 const scoped_ptr<Params> params(Params::Create(*args_));
61 EXTENSION_FUNCTION_VALIDATE(params);
63 std::vector<GURL> file_paths;
64 file_paths.push_back(GURL(params->selected_path));
66 file_manager::util::GetSelectedFileInfoLocalPathOption option =
67 file_manager::util::NO_LOCAL_PATH_RESOLUTION;
68 if (params->should_return_local_path) {
69 option = params->for_opening ?
70 file_manager::util::NEED_LOCAL_PATH_FOR_OPENING :
71 file_manager::util::NEED_LOCAL_PATH_FOR_SAVING;
74 file_manager::util::GetSelectedFileInfo(
80 &FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse,
86 void FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse(
88 const std::vector<ui::SelectedFileInfo>& files) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90 if (files.size() != 1) {
94 const SelectFileDialogExtension::RoutingID routing_id =
95 GetFileDialogRoutingID(dispatcher());
96 SelectFileDialogExtension::OnFileSelected(routing_id, files[0], index);
100 bool FileBrowserPrivateSelectFilesFunction::RunImpl() {
101 using extensions::api::file_browser_private::SelectFiles::Params;
102 const scoped_ptr<Params> params(Params::Create(*args_));
103 EXTENSION_FUNCTION_VALIDATE(params);
105 const size_t len = params->selected_paths.size();
106 std::vector<GURL> file_urls;
107 file_urls.reserve(len);
108 for (size_t i = 0; i < len; ++i) {
109 file_urls.push_back(GURL(params->selected_paths[i]));
112 file_manager::util::GetSelectedFileInfo(
116 params->should_return_local_path ?
117 file_manager::util::NEED_LOCAL_PATH_FOR_OPENING :
118 file_manager::util::NO_LOCAL_PATH_RESOLUTION,
120 &FileBrowserPrivateSelectFilesFunction::GetSelectedFileInfoResponse,
125 void FileBrowserPrivateSelectFilesFunction::GetSelectedFileInfoResponse(
126 const std::vector<ui::SelectedFileInfo>& files) {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 const SelectFileDialogExtension::RoutingID routing_id =
129 GetFileDialogRoutingID(dispatcher());
130 SelectFileDialogExtension::OnMultiFilesSelected(routing_id, files);
134 } // namespace extensions