5a753df86672ade86140ce8f950da17ce8cb1f56
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / file_manager / private_api_dialog.cc
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.
4
5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_dialog.h"
6
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"
14
15 using content::BrowserThread;
16
17 namespace extensions {
18
19 namespace {
20
21 // Returns the WebContents of the tab associated with the dispatcher. Returns
22 // NULL on error.
23 content::WebContents* GetWebContents(ExtensionFunctionDispatcher* dispatcher) {
24   if (!dispatcher) {
25     LOG(WARNING) << "No dispatcher";
26     return NULL;
27   }
28   if (!dispatcher->delegate()) {
29     LOG(WARNING) << "No delegate";
30     return NULL;
31   }
32   content::WebContents* web_contents =
33       dispatcher->delegate()->GetAssociatedWebContents();
34   if (!web_contents) {
35     LOG(WARNING) << "No associated tab contents";
36     return NULL;
37   }
38   return web_contents;
39 }
40
41 // Computes the routing ID for SelectFileDialogExtension from the |dispatcher|.
42 SelectFileDialogExtension::RoutingID GetFileDialogRoutingID(
43     ExtensionFunctionDispatcher* dispatcher) {
44   return SelectFileDialogExtension::GetRoutingIDFromWebContents(
45       GetWebContents(dispatcher));
46 }
47
48 }  // namespace
49
50 bool FileBrowserPrivateCancelDialogFunction::RunImpl() {
51   const SelectFileDialogExtension::RoutingID routing_id =
52       GetFileDialogRoutingID(dispatcher());
53   SelectFileDialogExtension::OnFileSelectionCanceled(routing_id);
54   SendResponse(true);
55   return true;
56 }
57
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);
62
63   std::vector<GURL> file_paths;
64   file_paths.push_back(GURL(params->selected_path));
65
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;
72   }
73
74   file_manager::util::GetSelectedFileInfo(
75       render_view_host(),
76       GetProfile(),
77       file_paths,
78       option,
79       base::Bind(
80           &FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse,
81           this,
82           params->index));
83   return true;
84 }
85
86 void FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse(
87     int index,
88     const std::vector<ui::SelectedFileInfo>& files) {
89   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90   if (files.size() != 1) {
91     SendResponse(false);
92     return;
93   }
94   const SelectFileDialogExtension::RoutingID routing_id =
95       GetFileDialogRoutingID(dispatcher());
96   SelectFileDialogExtension::OnFileSelected(routing_id, files[0], index);
97   SendResponse(true);
98 }
99
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);
104
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]));
110   }
111
112   file_manager::util::GetSelectedFileInfo(
113       render_view_host(),
114       GetProfile(),
115       file_urls,
116       params->should_return_local_path ?
117           file_manager::util::NEED_LOCAL_PATH_FOR_OPENING :
118           file_manager::util::NO_LOCAL_PATH_RESOLUTION,
119       base::Bind(
120           &FileBrowserPrivateSelectFilesFunction::GetSelectedFileInfoResponse,
121           this));
122   return true;
123 }
124
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);
131   SendResponse(true);
132 }
133
134 }  // namespace extensions