Chromium Code Reviews| 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 <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "chrome/browser/chromeos/drive/file_system_util.h" | 11 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 12 #include "chrome/browser/chromeos/file_manager/file_tasks.h" | |
| 13 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 12 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 14 #include "chrome/browser/chromeos/file_manager/mime_util.h" | 13 #include "chrome/browser/chromeos/file_manager/mime_util.h" |
| 15 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" | 14 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "net/base/filename_util.h" | |
| 18 #include "net/base/mime_sniffer.h" | |
| 17 #include "webkit/browser/fileapi/file_system_context.h" | 19 #include "webkit/browser/fileapi/file_system_context.h" |
| 18 #include "webkit/browser/fileapi/file_system_url.h" | 20 #include "webkit/browser/fileapi/file_system_url.h" |
| 19 | 21 |
| 22 using content::BrowserThread; | |
| 23 using extensions::app_file_handler_util::PathAndMimeTypeSet; | |
| 20 using fileapi::FileSystemURL; | 24 using fileapi::FileSystemURL; |
| 21 | 25 |
| 22 namespace extensions { | 26 namespace extensions { |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 25 // Error messages. | 29 // Error messages. |
| 26 const char kInvalidFileUrl[] = "Invalid file URL"; | 30 const char kInvalidFileUrl[] = "Invalid file URL"; |
| 27 | 31 |
| 28 // Make a set of unique filename suffixes out of the list of file URLs. | 32 // Make a set of unique filename suffixes out of the list of file URLs. |
| 29 std::set<std::string> GetUniqueSuffixes( | 33 std::set<std::string> GetUniqueSuffixes( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 47 std::set<std::string> mime_types; | 51 std::set<std::string> mime_types; |
| 48 for (size_t i = 0; i < mime_type_list.size(); ++i) { | 52 for (size_t i = 0; i < mime_type_list.size(); ++i) { |
| 49 const std::string mime_type = mime_type_list[i]; | 53 const std::string mime_type = mime_type_list[i]; |
| 50 // We'll skip empty MIME types and existing MIME types. | 54 // We'll skip empty MIME types and existing MIME types. |
| 51 if (!mime_type.empty()) | 55 if (!mime_type.empty()) |
| 52 mime_types.insert(mime_type); | 56 mime_types.insert(mime_type); |
| 53 } | 57 } |
| 54 return mime_types; | 58 return mime_types; |
| 55 } | 59 } |
| 56 | 60 |
| 61 void SniffMimeType(PathAndMimeTypeSet* path_mime_set, | |
| 62 std::vector<GURL>* file_urls) { | |
| 63 PathAndMimeTypeSet sniffed_path_mime_set; | |
| 64 std::vector<char> content(net::kMaxBytesToSniff); | |
| 65 | |
| 66 // For each files, sniff its MIME type if it is empty | |
| 67 for (PathAndMimeTypeSet::iterator it = path_mime_set->begin(); | |
| 68 it != path_mime_set->end(); | |
| 69 ++it) { | |
| 70 const base::FilePath& file_path = it->first; | |
| 71 std::string mime_type = it->second; | |
| 72 | |
| 73 // Note: sniff MIME type only for local files to avoid getting file's | |
| 74 // content via network frequently. | |
|
hashimoto
2014/04/10 11:54:21
This comment is misleading as we can get MIME type
fukino
2014/04/10 12:10:59
Done.
| |
| 75 if (mime_type.empty() && !drive::util::IsUnderDriveMountPoint(file_path)) { | |
| 76 int bytes_read = base::ReadFile(file_path, &content[0], content.size()); | |
| 77 if (bytes_read >= 0) { | |
| 78 net::SniffMimeType(&content[0], | |
| 79 bytes_read, | |
| 80 net::FilePathToFileURL(file_path), | |
| 81 std::string(), // type_hint (passes no hint) | |
| 82 &mime_type); | |
| 83 } | |
| 84 } | |
| 85 sniffed_path_mime_set.insert(std::make_pair(file_path, mime_type)); | |
| 86 } | |
| 87 path_mime_set->swap(sniffed_path_mime_set); | |
| 88 } | |
| 89 | |
| 57 } // namespace | 90 } // namespace |
| 58 | 91 |
| 59 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { | 92 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { |
| 60 using extensions::api::file_browser_private::ExecuteTask::Params; | 93 using extensions::api::file_browser_private::ExecuteTask::Params; |
| 61 using extensions::api::file_browser_private::ExecuteTask::Results::Create; | 94 using extensions::api::file_browser_private::ExecuteTask::Results::Create; |
| 62 const scoped_ptr<Params> params(Params::Create(*args_)); | 95 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 63 EXTENSION_FUNCTION_VALIDATE(params); | 96 EXTENSION_FUNCTION_VALIDATE(params); |
| 64 | 97 |
| 65 file_manager::file_tasks::TaskDescriptor task; | 98 file_manager::file_tasks::TaskDescriptor task; |
| 66 if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { | 99 if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 if (params->mime_types.size() != params->file_urls.size() && | 160 if (params->mime_types.size() != params->file_urls.size() && |
| 128 params->mime_types.size() != 0) | 161 params->mime_types.size() != 0) |
| 129 return false; | 162 return false; |
| 130 | 163 |
| 131 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 164 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 132 file_manager::util::GetFileSystemContextForRenderViewHost( | 165 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 133 GetProfile(), render_view_host()); | 166 GetProfile(), render_view_host()); |
| 134 | 167 |
| 135 // Collect all the URLs, convert them to GURLs, and crack all the urls into | 168 // Collect all the URLs, convert them to GURLs, and crack all the urls into |
| 136 // file paths. | 169 // file paths. |
| 137 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 170 scoped_ptr<PathAndMimeTypeSet> path_mime_set(new PathAndMimeTypeSet); |
| 138 std::vector<GURL> file_urls; | 171 scoped_ptr<std::vector<GURL> > file_urls(new std::vector<GURL>); |
| 139 for (size_t i = 0; i < params->file_urls.size(); ++i) { | 172 for (size_t i = 0; i < params->file_urls.size(); ++i) { |
| 140 std::string mime_type; | 173 std::string mime_type; |
| 141 if (params->mime_types.size() != 0) | 174 if (params->mime_types.size() != 0) |
| 142 mime_type = params->mime_types[i]; | 175 mime_type = params->mime_types[i]; |
| 143 | 176 |
| 144 const GURL file_url(params->file_urls[i]); | 177 const GURL file_url(params->file_urls[i]); |
| 145 fileapi::FileSystemURL file_system_url( | 178 fileapi::FileSystemURL file_system_url( |
| 146 file_system_context->CrackURL(file_url)); | 179 file_system_context->CrackURL(file_url)); |
| 147 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 180 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |
| 148 continue; | 181 continue; |
| 149 const base::FilePath file_path = file_system_url.path(); | 182 const base::FilePath file_path = file_system_url.path(); |
| 150 | 183 |
| 151 file_urls.push_back(file_url); | 184 file_urls->push_back(file_url); |
| 152 | 185 |
| 153 // If MIME type is not provided, guess it from the file path. | 186 // If MIME type is not provided, guess it from the file path. |
| 154 if (mime_type.empty()) | 187 if (mime_type.empty()) |
| 155 mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 188 mime_type = file_manager::util::GetMimeTypeForPath(file_path); |
| 156 | 189 |
| 157 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 190 path_mime_set->insert(std::make_pair(file_path, mime_type)); |
| 158 } | 191 } |
| 159 | 192 |
| 193 // In case the MIME type of some files are empty, | |
| 194 // try to sniff their MIME type by their content. | |
| 195 PathAndMimeTypeSet* path_mime_set_ptr = path_mime_set.get(); | |
| 196 std::vector<GURL>* file_urls_ptr = file_urls.get(); | |
| 197 | |
| 198 BrowserThread::PostBlockingPoolTaskAndReply( | |
| 199 FROM_HERE, | |
| 200 base::Bind(&SniffMimeType, path_mime_set_ptr, file_urls_ptr), | |
| 201 base::Bind( | |
| 202 &FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted, | |
| 203 this, | |
| 204 base::Passed(&path_mime_set), | |
| 205 base::Passed(&file_urls))); | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 void FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted( | |
| 210 scoped_ptr<PathAndMimeTypeSet> path_mime_set, | |
| 211 scoped_ptr<std::vector<GURL> > file_urls) { | |
| 160 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 212 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; |
| 161 file_manager::file_tasks::FindAllTypesOfTasks( | 213 file_manager::file_tasks::FindAllTypesOfTasks( |
| 162 GetProfile(), | 214 GetProfile(), |
| 163 drive::util::GetDriveAppRegistryByProfile(GetProfile()), | 215 drive::util::GetDriveAppRegistryByProfile(GetProfile()), |
| 164 path_mime_set, | 216 *path_mime_set, |
| 165 file_urls, | 217 *file_urls, |
| 166 &tasks); | 218 &tasks); |
| 167 | 219 |
| 168 // Convert the tasks into JSON compatible objects. | 220 // Convert the tasks into JSON compatible objects. |
| 169 using api::file_browser_private::FileTask; | 221 using api::file_browser_private::FileTask; |
| 170 std::vector<linked_ptr<FileTask> > results; | 222 std::vector<linked_ptr<FileTask> > results; |
| 171 for (size_t i = 0; i < tasks.size(); ++i) { | 223 for (size_t i = 0; i < tasks.size(); ++i) { |
| 172 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; | 224 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; |
| 173 const linked_ptr<FileTask> converted(new FileTask); | 225 const linked_ptr<FileTask> converted(new FileTask); |
| 174 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( | 226 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( |
| 175 task.task_descriptor()); | 227 task.task_descriptor()); |
| 176 if (!task.icon_url().is_empty()) | 228 if (!task.icon_url().is_empty()) |
| 177 converted->icon_url = task.icon_url().spec(); | 229 converted->icon_url = task.icon_url().spec(); |
| 178 converted->title = task.task_title(); | 230 converted->title = task.task_title(); |
| 179 converted->is_default = task.is_default(); | 231 converted->is_default = task.is_default(); |
| 180 results.push_back(converted); | 232 results.push_back(converted); |
| 181 } | 233 } |
| 182 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | 234 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: |
| 183 Create(results); | 235 Create(results); |
| 184 SendResponse(true); | 236 SendResponse(true); |
| 185 return true; | |
| 186 } | 237 } |
| 187 | 238 |
| 188 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { | 239 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |
| 189 using extensions::api::file_browser_private::SetDefaultTask::Params; | 240 using extensions::api::file_browser_private::SetDefaultTask::Params; |
| 190 const scoped_ptr<Params> params(Params::Create(*args_)); | 241 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 191 EXTENSION_FUNCTION_VALIDATE(params); | 242 EXTENSION_FUNCTION_VALIDATE(params); |
| 192 | 243 |
| 193 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 244 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 194 file_manager::util::GetFileSystemContextForRenderViewHost( | 245 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 195 GetProfile(), render_view_host()); | 246 GetProfile(), render_view_host()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 215 SetResult(new base::FundamentalValue(true)); | 266 SetResult(new base::FundamentalValue(true)); |
| 216 return true; | 267 return true; |
| 217 } | 268 } |
| 218 | 269 |
| 219 file_manager::file_tasks::UpdateDefaultTask( | 270 file_manager::file_tasks::UpdateDefaultTask( |
| 220 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); | 271 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); |
| 221 return true; | 272 return true; |
| 222 } | 273 } |
| 223 | 274 |
| 224 } // namespace extensions | 275 } // namespace extensions |
| OLD | NEW |