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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 if (params->mime_types.size() != params->file_urls.size() && | 131 if (params->mime_types.size() != params->file_urls.size() && |
| 128 params->mime_types.size() != 0) | 132 params->mime_types.size() != 0) |
| 129 return false; | 133 return false; |
| 130 | 134 |
| 131 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 135 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 132 file_manager::util::GetFileSystemContextForRenderViewHost( | 136 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 133 GetProfile(), render_view_host()); | 137 GetProfile(), render_view_host()); |
| 134 | 138 |
| 135 // Collect all the URLs, convert them to GURLs, and crack all the urls into | 139 // Collect all the URLs, convert them to GURLs, and crack all the urls into |
| 136 // file paths. | 140 // file paths. |
| 137 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 141 scoped_ptr<PathAndMimeTypeSet> path_mime_set(new PathAndMimeTypeSet); |
| 138 std::vector<GURL> file_urls; | 142 scoped_ptr<std::vector<GURL> > file_urls(new std::vector<GURL>); |
| 139 for (size_t i = 0; i < params->file_urls.size(); ++i) { | 143 for (size_t i = 0; i < params->file_urls.size(); ++i) { |
| 140 std::string mime_type; | 144 std::string mime_type; |
| 141 if (params->mime_types.size() != 0) | 145 if (params->mime_types.size() != 0) |
| 142 mime_type = params->mime_types[i]; | 146 mime_type = params->mime_types[i]; |
| 143 | 147 |
| 144 const GURL file_url(params->file_urls[i]); | 148 const GURL file_url(params->file_urls[i]); |
| 145 fileapi::FileSystemURL file_system_url( | 149 fileapi::FileSystemURL file_system_url( |
| 146 file_system_context->CrackURL(file_url)); | 150 file_system_context->CrackURL(file_url)); |
| 147 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 151 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |
| 148 continue; | 152 continue; |
| 149 const base::FilePath file_path = file_system_url.path(); | 153 const base::FilePath file_path = file_system_url.path(); |
| 150 | 154 |
| 151 file_urls.push_back(file_url); | 155 file_urls->push_back(file_url); |
| 152 | 156 |
| 153 // If MIME type is not provided, guess it from the file path. | 157 // If MIME type is not provided, guess it from the file path. |
| 154 if (mime_type.empty()) | 158 if (mime_type.empty()) |
| 155 mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 159 mime_type = file_manager::util::GetMimeTypeForPath(file_path); |
| 156 | 160 |
| 157 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 161 path_mime_set->insert(std::make_pair(file_path, mime_type)); |
| 158 } | 162 } |
| 159 | 163 |
| 164 // In case the MIME type of some files are empty, | |
| 165 // try to sniff their MIME type by their content. | |
| 166 BrowserThread::PostBlockingPoolTask( | |
|
hashimoto
2014/04/10 09:29:12
You can use PostBlockingPoolTaskAndReply instead o
fukino
2014/04/10 11:12:06
Thank you for sample code and note!
I would be cau
| |
| 167 FROM_HERE, | |
| 168 base::Bind(&FileBrowserPrivateGetFileTasksFunction::StartSniffingMimeType, | |
| 169 this, | |
| 170 base::Passed(&path_mime_set), | |
| 171 base::Passed(&file_urls))); | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 void FileBrowserPrivateGetFileTasksFunction::StartSniffingMimeType( | |
|
hashimoto
2014/04/10 09:29:12
Since this function is not accessing any member of
fukino
2014/04/10 11:12:06
Done.
| |
| 176 scoped_ptr<PathAndMimeTypeSet> path_mime_set, | |
| 177 scoped_ptr<std::vector<GURL> > file_urls) { | |
| 178 DCHECK(!BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
hashimoto
2014/04/10 09:29:12
nit: We usually don't have this kind of DCHECK.
Fi
fukino
2014/04/10 11:12:06
Deleted DCHECK.
| |
| 179 | |
| 180 scoped_ptr<PathAndMimeTypeSet> sniffed_path_mime_set(new PathAndMimeTypeSet); | |
|
hashimoto
2014/04/10 09:29:12
nit: Why don't you modify path_mime_set directly?
fukino
2014/04/10 11:12:06
path_mime_set is std::set, so I avoid modifying ke
hashimoto
2014/04/10 11:54:21
Ah, that makes sense.
Thank you for clarification.
| |
| 181 std::vector<char> content(net::kMaxBytesToSniff); | |
| 182 | |
| 183 // For each files, sniff its MIME type if it is empty | |
|
hashimoto
2014/04/10 09:29:12
nit: Please add a note about that we sniff local f
fukino
2014/04/10 11:12:06
Done.
| |
| 184 for (PathAndMimeTypeSet::iterator it = path_mime_set->begin(); | |
| 185 it != path_mime_set->end(); | |
| 186 ++it) { | |
| 187 const base::FilePath& file_path = it->first; | |
| 188 std::string mime_type = it->second; | |
| 189 if (mime_type.empty() && !drive::util::IsUnderDriveMountPoint(file_path)) { | |
| 190 int bytes_read = base::ReadFile(file_path, &content[0], content.size()); | |
| 191 if (bytes_read >= 0) { | |
| 192 net::SniffMimeType(&content[0], | |
| 193 bytes_read, | |
| 194 net::FilePathToFileURL(file_path), | |
| 195 std::string(), | |
| 196 &mime_type); | |
| 197 } | |
| 198 } | |
| 199 sniffed_path_mime_set->insert(std::make_pair(file_path, mime_type)); | |
| 200 } | |
| 201 | |
| 202 BrowserThread::PostTask( | |
| 203 BrowserThread::UI, | |
| 204 FROM_HERE, | |
| 205 base::Bind( | |
| 206 &FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted, | |
| 207 this, | |
| 208 base::Passed(&sniffed_path_mime_set), | |
| 209 base::Passed(&file_urls))); | |
| 210 } | |
| 211 | |
| 212 void FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted( | |
| 213 scoped_ptr<PathAndMimeTypeSet> path_mime_set, | |
| 214 scoped_ptr<std::vector<GURL> > file_urls) { | |
| 160 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 215 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; |
| 161 file_manager::file_tasks::FindAllTypesOfTasks( | 216 file_manager::file_tasks::FindAllTypesOfTasks( |
| 162 GetProfile(), | 217 GetProfile(), |
| 163 drive::util::GetDriveAppRegistryByProfile(GetProfile()), | 218 drive::util::GetDriveAppRegistryByProfile(GetProfile()), |
| 164 path_mime_set, | 219 *path_mime_set, |
| 165 file_urls, | 220 *file_urls, |
| 166 &tasks); | 221 &tasks); |
| 167 | 222 |
| 168 // Convert the tasks into JSON compatible objects. | 223 // Convert the tasks into JSON compatible objects. |
| 169 using api::file_browser_private::FileTask; | 224 using api::file_browser_private::FileTask; |
| 170 std::vector<linked_ptr<FileTask> > results; | 225 std::vector<linked_ptr<FileTask> > results; |
| 171 for (size_t i = 0; i < tasks.size(); ++i) { | 226 for (size_t i = 0; i < tasks.size(); ++i) { |
| 172 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; | 227 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; |
| 173 const linked_ptr<FileTask> converted(new FileTask); | 228 const linked_ptr<FileTask> converted(new FileTask); |
| 174 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( | 229 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( |
| 175 task.task_descriptor()); | 230 task.task_descriptor()); |
| 176 if (!task.icon_url().is_empty()) | 231 if (!task.icon_url().is_empty()) |
| 177 converted->icon_url = task.icon_url().spec(); | 232 converted->icon_url = task.icon_url().spec(); |
| 178 converted->title = task.task_title(); | 233 converted->title = task.task_title(); |
| 179 converted->is_default = task.is_default(); | 234 converted->is_default = task.is_default(); |
| 180 results.push_back(converted); | 235 results.push_back(converted); |
| 181 } | 236 } |
| 182 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | 237 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: |
| 183 Create(results); | 238 Create(results); |
| 184 SendResponse(true); | 239 SendResponse(true); |
| 185 return true; | |
| 186 } | 240 } |
| 187 | 241 |
| 188 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { | 242 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |
| 189 using extensions::api::file_browser_private::SetDefaultTask::Params; | 243 using extensions::api::file_browser_private::SetDefaultTask::Params; |
| 190 const scoped_ptr<Params> params(Params::Create(*args_)); | 244 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 191 EXTENSION_FUNCTION_VALIDATE(params); | 245 EXTENSION_FUNCTION_VALIDATE(params); |
| 192 | 246 |
| 193 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 247 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
| 194 file_manager::util::GetFileSystemContextForRenderViewHost( | 248 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 195 GetProfile(), render_view_host()); | 249 GetProfile(), render_view_host()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 215 SetResult(new base::FundamentalValue(true)); | 269 SetResult(new base::FundamentalValue(true)); |
| 216 return true; | 270 return true; |
| 217 } | 271 } |
| 218 | 272 |
| 219 file_manager::file_tasks::UpdateDefaultTask( | 273 file_manager::file_tasks::UpdateDefaultTask( |
| 220 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); | 274 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); |
| 221 return true; | 275 return true; |
| 222 } | 276 } |
| 223 | 277 |
| 224 } // namespace extensions | 278 } // namespace extensions |
| OLD | NEW |