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 // Note: sniff MIME type only for local files. |
| 73 if (mime_type.empty() && !drive::util::IsUnderDriveMountPoint(file_path)) { |
| 74 int bytes_read = base::ReadFile(file_path, &content[0], content.size()); |
| 75 if (bytes_read >= 0) { |
| 76 net::SniffMimeType(&content[0], |
| 77 bytes_read, |
| 78 net::FilePathToFileURL(file_path), |
| 79 std::string(), // type_hint (passes no hint) |
| 80 &mime_type); |
| 81 } |
| 82 } |
| 83 sniffed_path_mime_set.insert(std::make_pair(file_path, mime_type)); |
| 84 } |
| 85 path_mime_set->swap(sniffed_path_mime_set); |
| 86 } |
| 87 |
57 } // namespace | 88 } // namespace |
58 | 89 |
59 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { | 90 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { |
60 using extensions::api::file_browser_private::ExecuteTask::Params; | 91 using extensions::api::file_browser_private::ExecuteTask::Params; |
61 using extensions::api::file_browser_private::ExecuteTask::Results::Create; | 92 using extensions::api::file_browser_private::ExecuteTask::Results::Create; |
62 const scoped_ptr<Params> params(Params::Create(*args_)); | 93 const scoped_ptr<Params> params(Params::Create(*args_)); |
63 EXTENSION_FUNCTION_VALIDATE(params); | 94 EXTENSION_FUNCTION_VALIDATE(params); |
64 | 95 |
65 file_manager::file_tasks::TaskDescriptor task; | 96 file_manager::file_tasks::TaskDescriptor task; |
66 if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { | 97 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() && | 158 if (params->mime_types.size() != params->file_urls.size() && |
128 params->mime_types.size() != 0) | 159 params->mime_types.size() != 0) |
129 return false; | 160 return false; |
130 | 161 |
131 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 162 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
132 file_manager::util::GetFileSystemContextForRenderViewHost( | 163 file_manager::util::GetFileSystemContextForRenderViewHost( |
133 GetProfile(), render_view_host()); | 164 GetProfile(), render_view_host()); |
134 | 165 |
135 // Collect all the URLs, convert them to GURLs, and crack all the urls into | 166 // Collect all the URLs, convert them to GURLs, and crack all the urls into |
136 // file paths. | 167 // file paths. |
137 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 168 scoped_ptr<PathAndMimeTypeSet> path_mime_set(new PathAndMimeTypeSet); |
138 std::vector<GURL> file_urls; | 169 scoped_ptr<std::vector<GURL> > file_urls(new std::vector<GURL>); |
139 for (size_t i = 0; i < params->file_urls.size(); ++i) { | 170 for (size_t i = 0; i < params->file_urls.size(); ++i) { |
140 std::string mime_type; | 171 std::string mime_type; |
141 if (params->mime_types.size() != 0) | 172 if (params->mime_types.size() != 0) |
142 mime_type = params->mime_types[i]; | 173 mime_type = params->mime_types[i]; |
143 | 174 |
144 const GURL file_url(params->file_urls[i]); | 175 const GURL file_url(params->file_urls[i]); |
145 fileapi::FileSystemURL file_system_url( | 176 fileapi::FileSystemURL file_system_url( |
146 file_system_context->CrackURL(file_url)); | 177 file_system_context->CrackURL(file_url)); |
147 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 178 if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |
148 continue; | 179 continue; |
149 const base::FilePath file_path = file_system_url.path(); | 180 const base::FilePath file_path = file_system_url.path(); |
150 | 181 |
151 file_urls.push_back(file_url); | 182 file_urls->push_back(file_url); |
152 | 183 |
153 // If MIME type is not provided, guess it from the file path. | 184 // If MIME type is not provided, guess it from the file path. |
154 if (mime_type.empty()) | 185 if (mime_type.empty()) |
155 mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 186 mime_type = file_manager::util::GetMimeTypeForPath(file_path); |
156 | 187 |
157 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 188 path_mime_set->insert(std::make_pair(file_path, mime_type)); |
158 } | 189 } |
159 | 190 |
| 191 // In case the MIME type of some files are empty, |
| 192 // try to sniff their MIME type by their content. |
| 193 PathAndMimeTypeSet* path_mime_set_ptr = path_mime_set.get(); |
| 194 std::vector<GURL>* file_urls_ptr = file_urls.get(); |
| 195 |
| 196 BrowserThread::PostBlockingPoolTaskAndReply( |
| 197 FROM_HERE, |
| 198 base::Bind(&SniffMimeType, path_mime_set_ptr, file_urls_ptr), |
| 199 base::Bind( |
| 200 &FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted, |
| 201 this, |
| 202 base::Passed(&path_mime_set), |
| 203 base::Passed(&file_urls))); |
| 204 return true; |
| 205 } |
| 206 |
| 207 void FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted( |
| 208 scoped_ptr<PathAndMimeTypeSet> path_mime_set, |
| 209 scoped_ptr<std::vector<GURL> > file_urls) { |
160 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 210 std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; |
161 file_manager::file_tasks::FindAllTypesOfTasks( | 211 file_manager::file_tasks::FindAllTypesOfTasks( |
162 GetProfile(), | 212 GetProfile(), |
163 drive::util::GetDriveAppRegistryByProfile(GetProfile()), | 213 drive::util::GetDriveAppRegistryByProfile(GetProfile()), |
164 path_mime_set, | 214 *path_mime_set, |
165 file_urls, | 215 *file_urls, |
166 &tasks); | 216 &tasks); |
167 | 217 |
168 // Convert the tasks into JSON compatible objects. | 218 // Convert the tasks into JSON compatible objects. |
169 using api::file_browser_private::FileTask; | 219 using api::file_browser_private::FileTask; |
170 std::vector<linked_ptr<FileTask> > results; | 220 std::vector<linked_ptr<FileTask> > results; |
171 for (size_t i = 0; i < tasks.size(); ++i) { | 221 for (size_t i = 0; i < tasks.size(); ++i) { |
172 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; | 222 const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; |
173 const linked_ptr<FileTask> converted(new FileTask); | 223 const linked_ptr<FileTask> converted(new FileTask); |
174 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( | 224 converted->task_id = file_manager::file_tasks::TaskDescriptorToId( |
175 task.task_descriptor()); | 225 task.task_descriptor()); |
176 if (!task.icon_url().is_empty()) | 226 if (!task.icon_url().is_empty()) |
177 converted->icon_url = task.icon_url().spec(); | 227 converted->icon_url = task.icon_url().spec(); |
178 converted->title = task.task_title(); | 228 converted->title = task.task_title(); |
179 converted->is_default = task.is_default(); | 229 converted->is_default = task.is_default(); |
180 results.push_back(converted); | 230 results.push_back(converted); |
181 } | 231 } |
182 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | 232 results_ = extensions::api::file_browser_private::GetFileTasks::Results:: |
183 Create(results); | 233 Create(results); |
184 SendResponse(true); | 234 SendResponse(true); |
185 return true; | |
186 } | 235 } |
187 | 236 |
188 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { | 237 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |
189 using extensions::api::file_browser_private::SetDefaultTask::Params; | 238 using extensions::api::file_browser_private::SetDefaultTask::Params; |
190 const scoped_ptr<Params> params(Params::Create(*args_)); | 239 const scoped_ptr<Params> params(Params::Create(*args_)); |
191 EXTENSION_FUNCTION_VALIDATE(params); | 240 EXTENSION_FUNCTION_VALIDATE(params); |
192 | 241 |
193 const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 242 const scoped_refptr<fileapi::FileSystemContext> file_system_context = |
194 file_manager::util::GetFileSystemContextForRenderViewHost( | 243 file_manager::util::GetFileSystemContextForRenderViewHost( |
195 GetProfile(), render_view_host()); | 244 GetProfile(), render_view_host()); |
(...skipping 19 matching lines...) Expand all Loading... |
215 SetResult(new base::FundamentalValue(true)); | 264 SetResult(new base::FundamentalValue(true)); |
216 return true; | 265 return true; |
217 } | 266 } |
218 | 267 |
219 file_manager::file_tasks::UpdateDefaultTask( | 268 file_manager::file_tasks::UpdateDefaultTask( |
220 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); | 269 GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); |
221 return true; | 270 return true; |
222 } | 271 } |
223 | 272 |
224 } // namespace extensions | 273 } // namespace extensions |
OLD | NEW |