 Chromium Code Reviews
 Chromium Code Reviews Issue 224883008:
  Sniff MIME type for files which have unknown extensions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 224883008:
  Sniff MIME type for files which have unknown extensions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: chrome/browser/chromeos/extensions/file_manager/private_api_tasks.cc | 
| diff --git a/chrome/browser/chromeos/extensions/file_manager/private_api_tasks.cc b/chrome/browser/chromeos/extensions/file_manager/private_api_tasks.cc | 
| index b725c664a68b679aeae34c6ab1e766fa7e26cec3..af9419efad39a279019f8f2d50e143710e713290 100644 | 
| --- a/chrome/browser/chromeos/extensions/file_manager/private_api_tasks.cc | 
| +++ b/chrome/browser/chromeos/extensions/file_manager/private_api_tasks.cc | 
| @@ -9,14 +9,18 @@ | 
| #include <vector> | 
| #include "chrome/browser/chromeos/drive/file_system_util.h" | 
| -#include "chrome/browser/chromeos/file_manager/file_tasks.h" | 
| #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 
| #include "chrome/browser/chromeos/file_manager/mime_util.h" | 
| #include "chrome/browser/chromeos/fileapi/file_system_backend.h" | 
| #include "chrome/browser/profiles/profile.h" | 
| +#include "content/public/browser/browser_thread.h" | 
| +#include "net/base/filename_util.h" | 
| +#include "net/base/mime_sniffer.h" | 
| #include "webkit/browser/fileapi/file_system_context.h" | 
| #include "webkit/browser/fileapi/file_system_url.h" | 
| +using content::BrowserThread; | 
| +using extensions::app_file_handler_util::PathAndMimeTypeSet; | 
| using fileapi::FileSystemURL; | 
| namespace extensions { | 
| @@ -134,8 +138,8 @@ bool FileBrowserPrivateGetFileTasksFunction::RunImpl() { | 
| // Collect all the URLs, convert them to GURLs, and crack all the urls into | 
| // file paths. | 
| - extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 
| - std::vector<GURL> file_urls; | 
| + scoped_ptr<PathAndMimeTypeSet> path_mime_set(new PathAndMimeTypeSet); | 
| + scoped_ptr<std::vector<GURL> > file_urls(new std::vector<GURL>); | 
| for (size_t i = 0; i < params->file_urls.size(); ++i) { | 
| std::string mime_type; | 
| if (params->mime_types.size() != 0) | 
| @@ -148,21 +152,72 @@ bool FileBrowserPrivateGetFileTasksFunction::RunImpl() { | 
| continue; | 
| const base::FilePath file_path = file_system_url.path(); | 
| - file_urls.push_back(file_url); | 
| + file_urls->push_back(file_url); | 
| // If MIME type is not provided, guess it from the file path. | 
| if (mime_type.empty()) | 
| mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 
| - path_mime_set.insert(std::make_pair(file_path, mime_type)); | 
| + path_mime_set->insert(std::make_pair(file_path, mime_type)); | 
| } | 
| + // In case the MIME type of some files are empty, | 
| + // try to sniff their MIME type by their content. | 
| + 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
 | 
| + FROM_HERE, | 
| + base::Bind(&FileBrowserPrivateGetFileTasksFunction::StartSniffingMimeType, | 
| + this, | 
| + base::Passed(&path_mime_set), | 
| + base::Passed(&file_urls))); | 
| + return true; | 
| +} | 
| + | 
| +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.
 | 
| + scoped_ptr<PathAndMimeTypeSet> path_mime_set, | 
| + scoped_ptr<std::vector<GURL> > file_urls) { | 
| + 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.
 | 
| + | 
| + 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.
 | 
| + std::vector<char> content(net::kMaxBytesToSniff); | 
| + | 
| + // 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.
 | 
| + for (PathAndMimeTypeSet::iterator it = path_mime_set->begin(); | 
| + it != path_mime_set->end(); | 
| + ++it) { | 
| + const base::FilePath& file_path = it->first; | 
| + std::string mime_type = it->second; | 
| + if (mime_type.empty() && !drive::util::IsUnderDriveMountPoint(file_path)) { | 
| + int bytes_read = base::ReadFile(file_path, &content[0], content.size()); | 
| + if (bytes_read >= 0) { | 
| + net::SniffMimeType(&content[0], | 
| + bytes_read, | 
| + net::FilePathToFileURL(file_path), | 
| + std::string(), | 
| + &mime_type); | 
| + } | 
| + } | 
| + sniffed_path_mime_set->insert(std::make_pair(file_path, mime_type)); | 
| + } | 
| + | 
| + BrowserThread::PostTask( | 
| + BrowserThread::UI, | 
| + FROM_HERE, | 
| + base::Bind( | 
| + &FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted, | 
| + this, | 
| + base::Passed(&sniffed_path_mime_set), | 
| + base::Passed(&file_urls))); | 
| +} | 
| + | 
| +void FileBrowserPrivateGetFileTasksFunction::OnSniffingMimeTypeCompleted( | 
| + scoped_ptr<PathAndMimeTypeSet> path_mime_set, | 
| + scoped_ptr<std::vector<GURL> > file_urls) { | 
| std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 
| file_manager::file_tasks::FindAllTypesOfTasks( | 
| GetProfile(), | 
| drive::util::GetDriveAppRegistryByProfile(GetProfile()), | 
| - path_mime_set, | 
| - file_urls, | 
| + *path_mime_set, | 
| + *file_urls, | 
| &tasks); | 
| // Convert the tasks into JSON compatible objects. | 
| @@ -182,7 +237,6 @@ bool FileBrowserPrivateGetFileTasksFunction::RunImpl() { | 
| results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | 
| Create(results); | 
| SendResponse(true); | 
| - return true; | 
| } | 
| bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |