| 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 "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
| 14 #include "webkit/browser/fileapi/file_system_context.h" | 14 #include "webkit/browser/fileapi/file_system_context.h" |
| 15 #include "webkit/browser/fileapi/file_system_url.h" | 15 #include "webkit/browser/fileapi/file_system_url.h" |
| 16 | 16 |
| 17 using extensions::app_file_handler_util::PathAndMimeTypeSet; | 17 using extensions::app_file_handler_util::PathAndMimeTypeSet; |
| 18 using extensions::Extension; | 18 using extensions::Extension; |
| 19 using fileapi::FileSystemURL; | 19 using fileapi::FileSystemURL; |
| 20 | 20 |
| 21 namespace file_manager { | 21 namespace extensions { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Error messages. | 24 // Error messages. |
| 25 const char kInvalidFileUrl[] = "Invalid file URL"; | 25 const char kInvalidFileUrl[] = "Invalid file URL"; |
| 26 | 26 |
| 27 // Make a set of unique filename suffixes out of the list of file URLs. | 27 // 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, | 28 std::set<std::string> GetUniqueSuffixes(base::ListValue* file_url_list, |
| 29 fileapi::FileSystemContext* context) { | 29 fileapi::FileSystemContext* context) { |
| 30 std::set<std::string> suffixes; | 30 std::set<std::string> suffixes; |
| 31 for (size_t i = 0; i < file_url_list->GetSize(); ++i) { | 31 for (size_t i = 0; i < file_url_list->GetSize(); ++i) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 // TODO(kaznacheev): Crack the task_id here, store it in the Executor | 73 // TODO(kaznacheev): Crack the task_id here, store it in the Executor |
| 74 // and avoid passing it around. | 74 // and avoid passing it around. |
| 75 | 75 |
| 76 // The second param is the list of files that need to be executed with this | 76 // The second param is the list of files that need to be executed with this |
| 77 // task. | 77 // task. |
| 78 ListValue* files_list = NULL; | 78 ListValue* files_list = NULL; |
| 79 if (!args_->GetList(1, &files_list)) | 79 if (!args_->GetList(1, &files_list)) |
| 80 return false; | 80 return false; |
| 81 | 81 |
| 82 file_tasks::TaskDescriptor task; | 82 file_manager::file_tasks::TaskDescriptor task; |
| 83 if (!file_tasks::ParseTaskID(task_id, &task)) { | 83 if (!file_manager::file_tasks::ParseTaskID(task_id, &task)) { |
| 84 LOG(WARNING) << "Invalid task " << task_id; | 84 LOG(WARNING) << "Invalid task " << task_id; |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 | 87 |
| 88 if (!files_list->GetSize()) | 88 if (!files_list->GetSize()) |
| 89 return true; | 89 return true; |
| 90 | 90 |
| 91 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 91 scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 92 util::GetFileSystemContextForRenderViewHost( | 92 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 93 profile(), render_view_host()); | 93 profile(), render_view_host()); |
| 94 | 94 |
| 95 std::vector<FileSystemURL> file_urls; | 95 std::vector<FileSystemURL> file_urls; |
| 96 for (size_t i = 0; i < files_list->GetSize(); i++) { | 96 for (size_t i = 0; i < files_list->GetSize(); i++) { |
| 97 std::string file_url_str; | 97 std::string file_url_str; |
| 98 if (!files_list->GetString(i, &file_url_str)) { | 98 if (!files_list->GetString(i, &file_url_str)) { |
| 99 error_ = kInvalidFileUrl; | 99 error_ = kInvalidFileUrl; |
| 100 return false; | 100 return false; |
| 101 } | 101 } |
| 102 FileSystemURL url = file_system_context->CrackURL(GURL(file_url_str)); | 102 FileSystemURL url = file_system_context->CrackURL(GURL(file_url_str)); |
| 103 if (!chromeos::FileSystemBackend::CanHandleURL(url)) { | 103 if (!chromeos::FileSystemBackend::CanHandleURL(url)) { |
| 104 error_ = kInvalidFileUrl; | 104 error_ = kInvalidFileUrl; |
| 105 return false; | 105 return false; |
| 106 } | 106 } |
| 107 file_urls.push_back(url); | 107 file_urls.push_back(url); |
| 108 } | 108 } |
| 109 | 109 |
| 110 int32 tab_id = util::GetTabId(dispatcher()); | 110 int32 tab_id = file_manager::util::GetTabId(dispatcher()); |
| 111 return file_tasks::ExecuteFileTask( | 111 return file_manager::file_tasks::ExecuteFileTask( |
| 112 profile(), | 112 profile(), |
| 113 source_url(), | 113 source_url(), |
| 114 extension_->id(), | 114 extension_->id(), |
| 115 tab_id, | 115 tab_id, |
| 116 task, | 116 task, |
| 117 file_urls, | 117 file_urls, |
| 118 base::Bind(&ExecuteTaskFunction::OnTaskExecuted, this)); | 118 base::Bind(&ExecuteTaskFunction::OnTaskExecuted, this)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void ExecuteTaskFunction::OnTaskExecuted(bool success) { | 121 void ExecuteTaskFunction::OnTaskExecuted(bool success) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 142 ListValue* mime_types_list = NULL; | 142 ListValue* mime_types_list = NULL; |
| 143 if (!args_->GetList(1, &mime_types_list)) | 143 if (!args_->GetList(1, &mime_types_list)) |
| 144 return false; | 144 return false; |
| 145 | 145 |
| 146 // MIME types can either be empty, or there needs to be one for each file. | 146 // MIME types can either be empty, or there needs to be one for each file. |
| 147 if (mime_types_list->GetSize() != files_list->GetSize() && | 147 if (mime_types_list->GetSize() != files_list->GetSize() && |
| 148 mime_types_list->GetSize() != 0) | 148 mime_types_list->GetSize() != 0) |
| 149 return false; | 149 return false; |
| 150 | 150 |
| 151 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 151 scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 152 util::GetFileSystemContextForRenderViewHost( | 152 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 153 profile(), render_view_host()); | 153 profile(), render_view_host()); |
| 154 | 154 |
| 155 // Collect all the URLs, convert them to GURLs, and crack all the urls into | 155 // Collect all the URLs, convert them to GURLs, and crack all the urls into |
| 156 // file paths. | 156 // file paths. |
| 157 PathAndMimeTypeSet path_mime_set; | 157 PathAndMimeTypeSet path_mime_set; |
| 158 std::vector<GURL> file_urls; | 158 std::vector<GURL> file_urls; |
| 159 std::vector<base::FilePath> file_paths; | 159 std::vector<base::FilePath> file_paths; |
| 160 for (size_t i = 0; i < files_list->GetSize(); ++i) { | 160 for (size_t i = 0; i < files_list->GetSize(); ++i) { |
| 161 std::string file_url_str; | 161 std::string file_url_str; |
| 162 if (!files_list->GetString(i, &file_url_str)) | 162 if (!files_list->GetString(i, &file_url_str)) |
| 163 return false; | 163 return false; |
| 164 | 164 |
| 165 std::string mime_type; | 165 std::string mime_type; |
| 166 if (mime_types_list->GetSize() != 0 && | 166 if (mime_types_list->GetSize() != 0 && |
| 167 !mime_types_list->GetString(i, &mime_type)) | 167 !mime_types_list->GetString(i, &mime_type)) |
| 168 return false; | 168 return false; |
| 169 | 169 |
| 170 GURL file_url(file_url_str); | 170 GURL file_url(file_url_str); |
| 171 fileapi::FileSystemURL file_system_url( | 171 fileapi::FileSystemURL file_system_url( |
| 172 file_system_context->CrackURL(file_url)); | 172 file_system_context->CrackURL(file_url)); |
| 173 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 173 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |
| 174 continue; | 174 continue; |
| 175 const base::FilePath file_path = file_system_url.path(); | 175 const base::FilePath file_path = file_system_url.path(); |
| 176 | 176 |
| 177 file_urls.push_back(file_url); | 177 file_urls.push_back(file_url); |
| 178 file_paths.push_back(file_path); | 178 file_paths.push_back(file_path); |
| 179 | 179 |
| 180 // If MIME type is not provided, guess it from the file path. | 180 // If MIME type is not provided, guess it from the file path. |
| 181 if (mime_type.empty()) | 181 if (mime_type.empty()) |
| 182 mime_type = util::GetMimeTypeForPath(file_path); | 182 mime_type = file_manager::util::GetMimeTypeForPath(file_path); |
| 183 | 183 |
| 184 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 184 path_mime_set.insert(std::make_pair(file_path, mime_type)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 ListValue* result_list = new ListValue(); | 187 ListValue* result_list = new ListValue(); |
| 188 SetResult(result_list); | 188 SetResult(result_list); |
| 189 | 189 |
| 190 file_tasks::FindAllTypesOfTasks(profile_, | 190 file_manager::file_tasks::FindAllTypesOfTasks(profile_, |
| 191 path_mime_set, | 191 path_mime_set, |
| 192 file_urls, | 192 file_urls, |
| 193 file_paths, | 193 file_paths, |
| 194 result_list); | 194 result_list); |
| 195 SendResponse(true); | 195 SendResponse(true); |
| 196 return true; | 196 return true; |
| 197 } | 197 } |
| 198 | 198 |
| 199 SetDefaultTaskFunction::SetDefaultTaskFunction() { | 199 SetDefaultTaskFunction::SetDefaultTaskFunction() { |
| 200 } | 200 } |
| 201 | 201 |
| 202 SetDefaultTaskFunction::~SetDefaultTaskFunction() { | 202 SetDefaultTaskFunction::~SetDefaultTaskFunction() { |
| 203 } | 203 } |
| 204 | 204 |
| 205 bool SetDefaultTaskFunction::RunImpl() { | 205 bool SetDefaultTaskFunction::RunImpl() { |
| 206 // First param is task id that was to the extension with setDefaultTask call. | 206 // First param is task id that was to the extension with setDefaultTask call. |
| 207 std::string task_id; | 207 std::string task_id; |
| 208 if (!args_->GetString(0, &task_id) || !task_id.size()) | 208 if (!args_->GetString(0, &task_id) || !task_id.size()) |
| 209 return false; | 209 return false; |
| 210 | 210 |
| 211 base::ListValue* file_url_list; | 211 base::ListValue* file_url_list; |
| 212 if (!args_->GetList(1, &file_url_list)) | 212 if (!args_->GetList(1, &file_url_list)) |
| 213 return false; | 213 return false; |
| 214 | 214 |
| 215 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 215 scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 216 util::GetFileSystemContextForRenderViewHost( | 216 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 217 profile(), render_view_host()); | 217 profile(), render_view_host()); |
| 218 | 218 |
| 219 std::set<std::string> suffixes = | 219 std::set<std::string> suffixes = |
| 220 GetUniqueSuffixes(file_url_list, file_system_context.get()); | 220 GetUniqueSuffixes(file_url_list, file_system_context.get()); |
| 221 | 221 |
| 222 // MIME types are an optional parameter. | 222 // MIME types are an optional parameter. |
| 223 base::ListValue* mime_type_list; | 223 base::ListValue* mime_type_list; |
| 224 std::set<std::string> mime_types; | 224 std::set<std::string> mime_types; |
| 225 if (args_->GetList(2, &mime_type_list) && !mime_type_list->empty()) { | 225 if (args_->GetList(2, &mime_type_list) && !mime_type_list->empty()) { |
| 226 if (mime_type_list->GetSize() != file_url_list->GetSize()) | 226 if (mime_type_list->GetSize() != file_url_list->GetSize()) |
| 227 return false; | 227 return false; |
| 228 mime_types = GetUniqueMimeTypes(mime_type_list); | 228 mime_types = GetUniqueMimeTypes(mime_type_list); |
| 229 } | 229 } |
| 230 | 230 |
| 231 // If there weren't any mime_types, and all the suffixes were blank, | 231 // If there weren't any mime_types, and all the suffixes were blank, |
| 232 // then we "succeed", but don't actually associate with anything. | 232 // then we "succeed", but don't actually associate with anything. |
| 233 // Otherwise, any time we set the default on a file with no extension | 233 // Otherwise, any time we set the default on a file with no extension |
| 234 // on the local drive, we'd fail. | 234 // on the local drive, we'd fail. |
| 235 // TODO(gspencer): Fix file manager so that it never tries to set default in | 235 // TODO(gspencer): Fix file manager so that it never tries to set default in |
| 236 // cases where extensionless local files are part of the selection. | 236 // cases where extensionless local files are part of the selection. |
| 237 if (suffixes.empty() && mime_types.empty()) { | 237 if (suffixes.empty() && mime_types.empty()) { |
| 238 SetResult(new base::FundamentalValue(true)); | 238 SetResult(new base::FundamentalValue(true)); |
| 239 return true; | 239 return true; |
| 240 } | 240 } |
| 241 | 241 |
| 242 file_tasks::UpdateDefaultTask(profile_->GetPrefs(), | 242 file_manager::file_tasks::UpdateDefaultTask(profile_->GetPrefs(), |
| 243 task_id, | 243 task_id, |
| 244 suffixes, | 244 suffixes, |
| 245 mime_types); | 245 mime_types); |
| 246 | |
| 247 return true; | 246 return true; |
| 248 } | 247 } |
| 249 | 248 |
| 250 } // namespace file_manager | 249 } // namespace extensions |
| OLD | NEW |