OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_tasks.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_tasks.h" |
6 | 6 |
7 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" | 7 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" |
8 #include "chrome/browser/chromeos/extensions/file_manager/fileapi_util.h" | 8 #include "chrome/browser/chromeos/extensions/file_manager/fileapi_util.h" |
9 #include "chrome/browser/chromeos/extensions/file_manager/mime_util.h" | 9 #include "chrome/browser/chromeos/extensions/file_manager/mime_util.h" |
10 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" | 10 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
11 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" | 11 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/extensions/api/file_browser_private.h" | |
13 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
14 #include "webkit/browser/fileapi/file_system_context.h" | 15 #include "webkit/browser/fileapi/file_system_context.h" |
15 #include "webkit/browser/fileapi/file_system_url.h" | 16 #include "webkit/browser/fileapi/file_system_url.h" |
16 | 17 |
17 using extensions::app_file_handler_util::PathAndMimeTypeSet; | 18 using extensions::app_file_handler_util::PathAndMimeTypeSet; |
18 using extensions::Extension; | 19 using extensions::Extension; |
19 using fileapi::FileSystemURL; | 20 using fileapi::FileSystemURL; |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Error messages. | 25 // Error messages. |
25 const char kInvalidFileUrl[] = "Invalid file URL"; | 26 const char kInvalidFileUrl[] = "Invalid file URL"; |
26 | 27 |
27 // Make a set of unique filename suffixes out of the list of file URLs. | 28 // Make a set of unique filename suffixes out of the list of file URLs. |
28 std::set<std::string> GetUniqueSuffixes(base::ListValue* file_url_list, | 29 std::set<std::string> GetUniqueSuffixes( |
29 fileapi::FileSystemContext* context) { | 30 const std::vector<std::string>& file_url_list, |
31 const fileapi::FileSystemContext* context) { | |
30 std::set<std::string> suffixes; | 32 std::set<std::string> suffixes; |
31 for (size_t i = 0; i < file_url_list->GetSize(); ++i) { | 33 for (size_t i = 0; i < file_url_list.size(); ++i) { |
32 std::string url_str; | 34 const FileSystemURL url = context->CrackURL(GURL(file_url_list[i])); |
33 if (!file_url_list->GetString(i, &url_str)) | |
34 return std::set<std::string>(); | |
35 FileSystemURL url = context->CrackURL(GURL(url_str)); | |
36 if (!url.is_valid() || url.path().empty()) | 35 if (!url.is_valid() || url.path().empty()) |
37 return std::set<std::string>(); | 36 return std::set<std::string>(); |
38 // We'll skip empty suffixes. | 37 // We'll skip empty suffixes. |
39 if (!url.path().Extension().empty()) | 38 if (!url.path().Extension().empty()) |
40 suffixes.insert(url.path().Extension()); | 39 suffixes.insert(url.path().Extension()); |
41 } | 40 } |
42 return suffixes; | 41 return suffixes; |
43 } | 42 } |
44 | 43 |
45 // Make a set of unique MIME types out of the list of MIME types. | 44 // Make a set of unique MIME types out of the list of MIME types. |
46 std::set<std::string> GetUniqueMimeTypes(base::ListValue* mime_type_list) { | 45 std::set<std::string> GetUniqueMimeTypes( |
46 const std::vector<std::string>& mime_type_list) { | |
47 std::set<std::string> mime_types; | 47 std::set<std::string> mime_types; |
48 for (size_t i = 0; i < mime_type_list->GetSize(); ++i) { | 48 for (size_t i = 0; i < mime_type_list.size(); ++i) { |
49 std::string mime_type; | 49 std::string mime_type; |
50 if (!mime_type_list->GetString(i, &mime_type)) | |
51 return std::set<std::string>(); | |
52 // We'll skip empty MIME types. | 50 // We'll skip empty MIME types. |
53 if (!mime_type.empty()) | 51 if (!mime_type.empty()) |
54 mime_types.insert(mime_type); | 52 mime_types.insert(mime_type); |
55 } | 53 } |
56 return mime_types; | 54 return mime_types; |
57 } | 55 } |
58 | 56 |
59 } // namespace | 57 } // namespace |
60 | 58 |
61 FileBrowserPrivateExecuteTaskFunction::FileBrowserPrivateExecuteTaskFunction() { | |
62 } | |
63 | |
64 FileBrowserPrivateExecuteTaskFunction:: | |
65 ~FileBrowserPrivateExecuteTaskFunction() { | |
66 } | |
67 | |
68 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { | 59 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { |
69 // First param is task id that was to the extension with getFileTasks call. | 60 using extensions::api::file_browser_private::ExecuteTask::Params; |
70 std::string task_id; | 61 const scoped_ptr<Params> params(Params::Create(*args_)); |
71 if (!args_->GetString(0, &task_id) || !task_id.size()) | 62 EXTENSION_FUNCTION_VALIDATE(params); |
72 return false; | |
73 | 63 |
74 // TODO(kaznacheev): Crack the task_id here, store it in the Executor | 64 // TODO(kaznacheev): Crack the task_id here, store it in the Executor |
75 // and avoid passing it around. | 65 // and avoid passing it around. |
76 | 66 |
77 // The second param is the list of files that need to be executed with this | |
78 // task. | |
79 ListValue* files_list = NULL; | |
80 if (!args_->GetList(1, &files_list)) | |
81 return false; | |
82 | |
83 file_manager::file_tasks::TaskDescriptor task; | 67 file_manager::file_tasks::TaskDescriptor task; |
84 if (!file_manager::file_tasks::ParseTaskID(task_id, &task)) { | 68 if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { |
85 LOG(WARNING) << "Invalid task " << task_id; | 69 LOG(WARNING) << "Invalid task " << params->task_id; |
86 return false; | 70 return false; |
87 } | 71 } |
88 | 72 |
89 if (!files_list->GetSize()) | 73 if (!params->file_urls.size()) |
satorux1
2013/09/05 01:36:53
if (params->file_urls.empty())
hirono
2013/09/05 03:15:46
Done.
| |
90 return true; | 74 return true; |
91 | 75 |
92 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 76 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
93 file_manager::util::GetFileSystemContextForRenderViewHost( | 77 file_manager::util::GetFileSystemContextForRenderViewHost( |
94 profile(), render_view_host()); | 78 profile(), render_view_host()); |
95 | 79 |
96 std::vector<FileSystemURL> file_urls; | 80 std::vector<FileSystemURL> file_urls; |
97 for (size_t i = 0; i < files_list->GetSize(); i++) { | 81 for (size_t i = 0; i < params->file_urls.size(); i++) { |
98 std::string file_url_str; | 82 const FileSystemURL url = |
99 if (!files_list->GetString(i, &file_url_str)) { | 83 file_system_context->CrackURL(GURL(params->file_urls[i])); |
100 error_ = kInvalidFileUrl; | |
101 return false; | |
102 } | |
103 FileSystemURL url = file_system_context->CrackURL(GURL(file_url_str)); | |
104 if (!chromeos::FileSystemBackend::CanHandleURL(url)) { | 84 if (!chromeos::FileSystemBackend::CanHandleURL(url)) { |
105 error_ = kInvalidFileUrl; | 85 error_ = kInvalidFileUrl; |
106 return false; | 86 return false; |
107 } | 87 } |
108 file_urls.push_back(url); | 88 file_urls.push_back(url); |
109 } | 89 } |
110 | 90 |
111 int32 tab_id = file_manager::util::GetTabId(dispatcher()); | 91 const int32 tab_id = file_manager::util::GetTabId(dispatcher()); |
112 return file_manager::file_tasks::ExecuteFileTask( | 92 return file_manager::file_tasks::ExecuteFileTask( |
113 profile(), | 93 profile(), |
114 source_url(), | 94 source_url(), |
115 extension_->id(), | 95 extension_->id(), |
116 tab_id, | 96 tab_id, |
117 task, | 97 task, |
118 file_urls, | 98 file_urls, |
119 base::Bind(&FileBrowserPrivateExecuteTaskFunction::OnTaskExecuted, this)); | 99 base::Bind(&FileBrowserPrivateExecuteTaskFunction::OnTaskExecuted, this)); |
120 } | 100 } |
121 | 101 |
122 void FileBrowserPrivateExecuteTaskFunction::OnTaskExecuted(bool success) { | 102 void FileBrowserPrivateExecuteTaskFunction::OnTaskExecuted(bool success) { |
123 SetResult(new base::FundamentalValue(success)); | 103 SetResult(new base::FundamentalValue(success)); |
124 SendResponse(true); | 104 SendResponse(true); |
125 } | 105 } |
126 | 106 |
127 FileBrowserPrivateGetFileTasksFunction:: | 107 bool FileBrowserPrivateGetFileTasksFunction::RunImpl() { |
128 FileBrowserPrivateGetFileTasksFunction() { | 108 using extensions::api::file_browser_private::GetFileTasks::Params; |
129 } | 109 const scoped_ptr<Params> params(Params::Create(*args_)); |
110 EXTENSION_FUNCTION_VALIDATE(params); | |
130 | 111 |
131 FileBrowserPrivateGetFileTasksFunction:: | 112 if (!params->file_urls.size()) |
satorux1
2013/09/05 01:36:53
ditto
hirono
2013/09/05 03:15:46
Done.
| |
132 ~FileBrowserPrivateGetFileTasksFunction() { | |
133 } | |
134 | |
135 bool FileBrowserPrivateGetFileTasksFunction::RunImpl() { | |
136 // First argument is the list of files to get tasks for. | |
137 ListValue* files_list = NULL; | |
138 if (!args_->GetList(0, &files_list)) | |
139 return false; | |
140 | |
141 if (files_list->GetSize() == 0) | |
142 return false; | |
143 | |
144 // Second argument is the list of mime types of each of the files in the list. | |
145 ListValue* mime_types_list = NULL; | |
146 if (!args_->GetList(1, &mime_types_list)) | |
147 return false; | 113 return false; |
148 | 114 |
149 // MIME types can either be empty, or there needs to be one for each file. | 115 // MIME types can either be empty, or there needs to be one for each file. |
150 if (mime_types_list->GetSize() != files_list->GetSize() && | 116 if (params->mime_types.size() != params->file_urls.size() && |
151 mime_types_list->GetSize() != 0) | 117 params->mime_types.size() != 0) |
152 return false; | 118 return false; |
153 | 119 |
154 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 120 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
155 file_manager::util::GetFileSystemContextForRenderViewHost( | 121 file_manager::util::GetFileSystemContextForRenderViewHost( |
156 profile(), render_view_host()); | 122 profile(), render_view_host()); |
157 | 123 |
158 // Collect all the URLs, convert them to GURLs, and crack all the urls into | 124 // Collect all the URLs, convert them to GURLs, and crack all the urls into |
159 // file paths. | 125 // file paths. |
160 PathAndMimeTypeSet path_mime_set; | 126 PathAndMimeTypeSet path_mime_set; |
161 std::vector<GURL> file_urls; | 127 std::vector<GURL> file_urls; |
162 for (size_t i = 0; i < files_list->GetSize(); ++i) { | 128 for (size_t i = 0; i < params->file_urls.size(); ++i) { |
163 std::string file_url_str; | 129 std::string mime_type; |
164 if (!files_list->GetString(i, &file_url_str)) | 130 if (params->mime_types.size() != 0) |
165 return false; | 131 mime_type = params->mime_types[i]; |
166 | 132 |
167 std::string mime_type; | 133 const GURL file_url(params->file_urls[i]); |
168 if (mime_types_list->GetSize() != 0 && | |
169 !mime_types_list->GetString(i, &mime_type)) | |
170 return false; | |
171 | |
172 GURL file_url(file_url_str); | |
173 fileapi::FileSystemURL file_system_url( | 134 fileapi::FileSystemURL file_system_url( |
174 file_system_context->CrackURL(file_url)); | 135 file_system_context->CrackURL(file_url)); |
175 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 136 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |
176 continue; | 137 continue; |
177 const base::FilePath file_path = file_system_url.path(); | 138 const base::FilePath file_path = file_system_url.path(); |
178 | 139 |
179 file_urls.push_back(file_url); | 140 file_urls.push_back(file_url); |
180 | 141 |
181 // If MIME type is not provided, guess it from the file path. | 142 // If MIME type is not provided, guess it from the file path. |
182 if (mime_type.empty()) | 143 if (mime_type.empty()) |
183 mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 144 mime_type = file_manager::util::GetMimeTypeForPath(file_path); |
184 | 145 |
185 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 146 path_mime_set.insert(std::make_pair(file_path, mime_type)); |
186 } | 147 } |
187 | 148 |
188 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 149 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; |
189 file_manager::file_tasks::FindAllTypesOfTasks(profile_, | 150 file_manager::file_tasks::FindAllTypesOfTasks(profile_, |
190 path_mime_set, | 151 path_mime_set, |
191 file_urls, | 152 file_urls, |
192 &tasks); | 153 &tasks); |
193 // Convert the tasks into JSON format. | |
194 ListValue* result_list = new ListValue(); | |
195 for (size_t i = 0; i < tasks.size(); ++i) | |
196 result_list->Append(tasks[i].AsDictionaryValue().release()); | |
197 | 154 |
198 SetResult(result_list); | 155 // Convert the tasks into JSON compatible objects. |
156 using api::file_browser_private::FileTask; | |
157 std::vector<linked_ptr<FileTask> > results; | |
158 for (size_t i = 0; i < tasks.size(); ++i) { | |
satorux1
2013/09/05 01:36:53
tasks[i] is references many times in the loop. may
hirono
2013/09/05 03:15:46
Done.
| |
159 linked_ptr<FileTask> task(new FileTask); | |
160 task->task_id = file_manager::file_tasks::TaskDescriptorToId( | |
161 tasks[i].task_descriptor()); | |
162 if (!tasks[i].icon_url().is_empty()) | |
163 task->icon_url = tasks[i].icon_url().spec(); | |
164 task->drive_app = | |
165 tasks[i].task_descriptor().task_type == | |
satorux1
2013/09/05 01:36:53
please add ()
hirono
2013/09/05 03:15:46
This is removed.
satorux1
2013/09/05 03:18:15
That's right. I just removed it. :)
| |
166 file_manager::file_tasks::TASK_TYPE_DRIVE_APP; | |
167 task->title = tasks[i].task_title(); | |
168 task->is_default = tasks[i].is_default(); | |
169 results.push_back(task); | |
170 } | |
171 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | |
172 Create(results); | |
199 SendResponse(true); | 173 SendResponse(true); |
200 return true; | 174 return true; |
201 } | 175 } |
202 | 176 |
203 FileBrowserPrivateSetDefaultTaskFunction:: | 177 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |
204 FileBrowserPrivateSetDefaultTaskFunction() { | 178 using extensions::api::file_browser_private::SetDefaultTask::Params; |
205 } | 179 const scoped_ptr<Params> params(Params::Create(*args_)); |
180 EXTENSION_FUNCTION_VALIDATE(params); | |
206 | 181 |
207 FileBrowserPrivateSetDefaultTaskFunction:: | 182 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
208 ~FileBrowserPrivateSetDefaultTaskFunction() { | |
209 } | |
210 | |
211 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { | |
212 // First param is task id that was to the extension with setDefaultTask call. | |
213 std::string task_id; | |
214 if (!args_->GetString(0, &task_id) || !task_id.size()) | |
215 return false; | |
216 | |
217 base::ListValue* file_url_list; | |
218 if (!args_->GetList(1, &file_url_list)) | |
219 return false; | |
220 | |
221 scoped_refptr<fileapi::FileSystemContext> file_system_context = | |
222 file_manager::util::GetFileSystemContextForRenderViewHost( | 183 file_manager::util::GetFileSystemContextForRenderViewHost( |
223 profile(), render_view_host()); | 184 profile(), render_view_host()); |
224 | 185 |
225 std::set<std::string> suffixes = | 186 const std::set<std::string> suffixes = |
226 GetUniqueSuffixes(file_url_list, file_system_context.get()); | 187 GetUniqueSuffixes(params->file_urls, file_system_context.get()); |
227 | 188 |
228 // MIME types are an optional parameter. | 189 // MIME types are an optional parameter. |
229 base::ListValue* mime_type_list; | |
230 std::set<std::string> mime_types; | 190 std::set<std::string> mime_types; |
231 if (args_->GetList(2, &mime_type_list) && !mime_type_list->empty()) { | 191 if (params->mime_types && !params->mime_types->empty()) { |
232 if (mime_type_list->GetSize() != file_url_list->GetSize()) | 192 if (params->mime_types->size() != params->file_urls.size()) |
233 return false; | 193 return false; |
234 mime_types = GetUniqueMimeTypes(mime_type_list); | 194 mime_types = GetUniqueMimeTypes(*params->mime_types); |
235 } | 195 } |
236 | 196 |
237 // If there weren't any mime_types, and all the suffixes were blank, | 197 // If there weren't any mime_types, and all the suffixes were blank, |
238 // then we "succeed", but don't actually associate with anything. | 198 // then we "succeed", but don't actually associate with anything. |
239 // Otherwise, any time we set the default on a file with no extension | 199 // Otherwise, any time we set the default on a file with no extension |
240 // on the local drive, we'd fail. | 200 // on the local drive, we'd fail. |
241 // TODO(gspencer): Fix file manager so that it never tries to set default in | 201 // TODO(gspencer): Fix file manager so that it never tries to set default in |
242 // cases where extensionless local files are part of the selection. | 202 // cases where extensionless local files are part of the selection. |
243 if (suffixes.empty() && mime_types.empty()) { | 203 if (suffixes.empty() && mime_types.empty()) { |
244 SetResult(new base::FundamentalValue(true)); | 204 SetResult(new base::FundamentalValue(true)); |
245 return true; | 205 return true; |
246 } | 206 } |
247 | 207 |
248 file_manager::file_tasks::UpdateDefaultTask(profile_->GetPrefs(), | 208 file_manager::file_tasks::UpdateDefaultTask(profile_->GetPrefs(), |
249 task_id, | 209 params->task_id, |
250 suffixes, | 210 suffixes, |
251 mime_types); | 211 mime_types); |
252 return true; | 212 return true; |
253 } | 213 } |
254 | 214 |
255 } // namespace extensions | 215 } // namespace extensions |
OLD | NEW |