| 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/private_api_util.h" | 7 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
| 8 #include "chrome/browser/chromeos/file_manager/file_tasks.h" | 8 #include "chrome/browser/chromeos/file_manager/file_tasks.h" |
| 9 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 9 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 10 #include "chrome/browser/chromeos/file_manager/mime_util.h" | 10 #include "chrome/browser/chromeos/file_manager/mime_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.empty()) |
| 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.empty()) |
| 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) { |
| 159 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; |
| 160 const linked_ptr<FileTask> converted(new FileTask); |
| 161 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( |
| 162 task.task_descriptor()); |
| 163 if (!task.icon_url().is_empty()) |
| 164 converted->icon_url = task.icon_url().spec(); |
| 165 converted->title = task.task_title(); |
| 166 converted->is_default = task.is_default(); |
| 167 results.push_back(converted); |
| 168 } |
| 169 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: |
| 170 Create(results); |
| 199 SendResponse(true); | 171 SendResponse(true); |
| 200 return true; | 172 return true; |
| 201 } | 173 } |
| 202 | 174 |
| 203 FileBrowserPrivateSetDefaultTaskFunction:: | 175 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |
| 204 FileBrowserPrivateSetDefaultTaskFunction() { | 176 using extensions::api::file_browser_private::SetDefaultTask::Params; |
| 205 } | 177 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 178 EXTENSION_FUNCTION_VALIDATE(params); |
| 206 | 179 |
| 207 FileBrowserPrivateSetDefaultTaskFunction:: | 180 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( | 181 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 223 profile(), render_view_host()); | 182 profile(), render_view_host()); |
| 224 | 183 |
| 225 std::set<std::string> suffixes = | 184 const std::set<std::string> suffixes = |
| 226 GetUniqueSuffixes(file_url_list, file_system_context.get()); | 185 GetUniqueSuffixes(params->file_urls, file_system_context.get()); |
| 227 | 186 |
| 228 // MIME types are an optional parameter. | 187 // MIME types are an optional parameter. |
| 229 base::ListValue* mime_type_list; | |
| 230 std::set<std::string> mime_types; | 188 std::set<std::string> mime_types; |
| 231 if (args_->GetList(2, &mime_type_list) && !mime_type_list->empty()) { | 189 if (params->mime_types && !params->mime_types->empty()) { |
| 232 if (mime_type_list->GetSize() != file_url_list->GetSize()) | 190 if (params->mime_types->size() != params->file_urls.size()) |
| 233 return false; | 191 return false; |
| 234 mime_types = GetUniqueMimeTypes(mime_type_list); | 192 mime_types = GetUniqueMimeTypes(*params->mime_types); |
| 235 } | 193 } |
| 236 | 194 |
| 237 // If there weren't any mime_types, and all the suffixes were blank, | 195 // If there weren't any mime_types, and all the suffixes were blank, |
| 238 // then we "succeed", but don't actually associate with anything. | 196 // then we "succeed", but don't actually associate with anything. |
| 239 // Otherwise, any time we set the default on a file with no extension | 197 // Otherwise, any time we set the default on a file with no extension |
| 240 // on the local drive, we'd fail. | 198 // on the local drive, we'd fail. |
| 241 // TODO(gspencer): Fix file manager so that it never tries to set default in | 199 // 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. | 200 // cases where extensionless local files are part of the selection. |
| 243 if (suffixes.empty() && mime_types.empty()) { | 201 if (suffixes.empty() && mime_types.empty()) { |
| 244 SetResult(new base::FundamentalValue(true)); | 202 SetResult(new base::FundamentalValue(true)); |
| 245 return true; | 203 return true; |
| 246 } | 204 } |
| 247 | 205 |
| 248 file_manager::file_tasks::UpdateDefaultTask(profile_->GetPrefs(), | 206 file_manager::file_tasks::UpdateDefaultTask(profile_->GetPrefs(), |
| 249 task_id, | 207 params->task_id, |
| 250 suffixes, | 208 suffixes, |
| 251 mime_types); | 209 mime_types); |
| 252 return true; | 210 return true; |
| 253 } | 211 } |
| 254 | 212 |
| 255 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |