| 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" | 
|   12 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |   13 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 
|   13 #include "chrome/browser/chromeos/file_manager/mime_util.h" |   14 #include "chrome/browser/chromeos/file_manager/mime_util.h" | 
|   14 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" |   15 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" | 
|   15 #include "chrome/browser/profiles/profile.h" |   16 #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" |  | 
|   19 #include "webkit/browser/fileapi/file_system_context.h" |   17 #include "webkit/browser/fileapi/file_system_context.h" | 
|   20 #include "webkit/browser/fileapi/file_system_url.h" |   18 #include "webkit/browser/fileapi/file_system_url.h" | 
|   21  |   19  | 
|   22 using content::BrowserThread; |  | 
|   23 using extensions::app_file_handler_util::PathAndMimeTypeSet; |  | 
|   24 using fileapi::FileSystemURL; |   20 using fileapi::FileSystemURL; | 
|   25  |   21  | 
|   26 namespace extensions { |   22 namespace extensions { | 
|   27 namespace { |   23 namespace { | 
|   28  |   24  | 
|   29 // Error messages. |   25 // Error messages. | 
|   30 const char kInvalidFileUrl[] = "Invalid file URL"; |   26 const char kInvalidFileUrl[] = "Invalid file URL"; | 
|   31  |   27  | 
|   32 // Make a set of unique filename suffixes out of the list of file URLs. |   28 // Make a set of unique filename suffixes out of the list of file URLs. | 
|   33 std::set<std::string> GetUniqueSuffixes( |   29 std::set<std::string> GetUniqueSuffixes( | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
|   51   std::set<std::string> mime_types; |   47   std::set<std::string> mime_types; | 
|   52   for (size_t i = 0; i < mime_type_list.size(); ++i) { |   48   for (size_t i = 0; i < mime_type_list.size(); ++i) { | 
|   53     const std::string mime_type = mime_type_list[i]; |   49     const std::string mime_type = mime_type_list[i]; | 
|   54     // We'll skip empty MIME types and existing MIME types. |   50     // We'll skip empty MIME types and existing MIME types. | 
|   55     if (!mime_type.empty()) |   51     if (!mime_type.empty()) | 
|   56       mime_types.insert(mime_type); |   52       mime_types.insert(mime_type); | 
|   57   } |   53   } | 
|   58   return mime_types; |   54   return mime_types; | 
|   59 } |   55 } | 
|   60  |   56  | 
|   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  |  | 
|   88 }  // namespace |   57 }  // namespace | 
|   89  |   58  | 
|   90 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { |   59 bool FileBrowserPrivateExecuteTaskFunction::RunImpl() { | 
|   91   using extensions::api::file_browser_private::ExecuteTask::Params; |   60   using extensions::api::file_browser_private::ExecuteTask::Params; | 
|   92   using extensions::api::file_browser_private::ExecuteTask::Results::Create; |   61   using extensions::api::file_browser_private::ExecuteTask::Results::Create; | 
|   93   const scoped_ptr<Params> params(Params::Create(*args_)); |   62   const scoped_ptr<Params> params(Params::Create(*args_)); | 
|   94   EXTENSION_FUNCTION_VALIDATE(params); |   63   EXTENSION_FUNCTION_VALIDATE(params); | 
|   95  |   64  | 
|   96   file_manager::file_tasks::TaskDescriptor task; |   65   file_manager::file_tasks::TaskDescriptor task; | 
|   97   if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { |   66   if (!file_manager::file_tasks::ParseTaskID(params->task_id, &task)) { | 
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  158   if (params->mime_types.size() != params->file_urls.size() && |  127   if (params->mime_types.size() != params->file_urls.size() && | 
|  159       params->mime_types.size() != 0) |  128       params->mime_types.size() != 0) | 
|  160     return false; |  129     return false; | 
|  161  |  130  | 
|  162   const scoped_refptr<fileapi::FileSystemContext> file_system_context = |  131   const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 
|  163       file_manager::util::GetFileSystemContextForRenderViewHost( |  132       file_manager::util::GetFileSystemContextForRenderViewHost( | 
|  164           GetProfile(), render_view_host()); |  133           GetProfile(), render_view_host()); | 
|  165  |  134  | 
|  166   // Collect all the URLs, convert them to GURLs, and crack all the urls into |  135   // Collect all the URLs, convert them to GURLs, and crack all the urls into | 
|  167   // file paths. |  136   // file paths. | 
|  168   scoped_ptr<PathAndMimeTypeSet> path_mime_set(new PathAndMimeTypeSet); |  137   extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 
|  169   scoped_ptr<std::vector<GURL> > file_urls(new std::vector<GURL>); |  138   std::vector<GURL> file_urls; | 
|  170   for (size_t i = 0; i < params->file_urls.size(); ++i) { |  139   for (size_t i = 0; i < params->file_urls.size(); ++i) { | 
|  171     std::string mime_type; |  140     std::string mime_type; | 
|  172     if (params->mime_types.size() != 0) |  141     if (params->mime_types.size() != 0) | 
|  173       mime_type = params->mime_types[i]; |  142       mime_type = params->mime_types[i]; | 
|  174  |  143  | 
|  175     const GURL file_url(params->file_urls[i]); |  144     const GURL file_url(params->file_urls[i]); | 
|  176     fileapi::FileSystemURL file_system_url( |  145     fileapi::FileSystemURL file_system_url( | 
|  177         file_system_context->CrackURL(file_url)); |  146         file_system_context->CrackURL(file_url)); | 
|  178     if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) |  147     if (!chromeos::FileSystemBackend::CanHandleURL(file_system_url)) | 
|  179       continue; |  148       continue; | 
|  180     const base::FilePath file_path = file_system_url.path(); |  149     const base::FilePath file_path = file_system_url.path(); | 
|  181  |  150  | 
|  182     file_urls->push_back(file_url); |  151     file_urls.push_back(file_url); | 
|  183  |  152  | 
|  184     // If MIME type is not provided, guess it from the file path. |  153     // If MIME type is not provided, guess it from the file path. | 
|  185     if (mime_type.empty()) |  154     if (mime_type.empty()) | 
|  186       mime_type = file_manager::util::GetMimeTypeForPath(file_path); |  155       mime_type = file_manager::util::GetMimeTypeForPath(file_path); | 
|  187  |  156  | 
|  188     path_mime_set->insert(std::make_pair(file_path, mime_type)); |  157     path_mime_set.insert(std::make_pair(file_path, mime_type)); | 
|  189   } |  158   } | 
|  190  |  159  | 
|  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) { |  | 
|  210   std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; |  160   std::vector<file_manager::file_tasks::FullTaskDescriptor> tasks; | 
|  211   file_manager::file_tasks::FindAllTypesOfTasks( |  161   file_manager::file_tasks::FindAllTypesOfTasks( | 
|  212       GetProfile(), |  162       GetProfile(), | 
|  213       drive::util::GetDriveAppRegistryByProfile(GetProfile()), |  163       drive::util::GetDriveAppRegistryByProfile(GetProfile()), | 
|  214       *path_mime_set, |  164       path_mime_set, | 
|  215       *file_urls, |  165       file_urls, | 
|  216       &tasks); |  166       &tasks); | 
|  217  |  167  | 
|  218   // Convert the tasks into JSON compatible objects. |  168   // Convert the tasks into JSON compatible objects. | 
|  219   using api::file_browser_private::FileTask; |  169   using api::file_browser_private::FileTask; | 
|  220   std::vector<linked_ptr<FileTask> > results; |  170   std::vector<linked_ptr<FileTask> > results; | 
|  221   for (size_t i = 0; i < tasks.size(); ++i) { |  171   for (size_t i = 0; i < tasks.size(); ++i) { | 
|  222     const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; |  172     const file_manager::file_tasks::FullTaskDescriptor& task = tasks[i]; | 
|  223     const linked_ptr<FileTask> converted(new FileTask); |  173     const linked_ptr<FileTask> converted(new FileTask); | 
|  224     converted->task_id = file_manager::file_tasks::TaskDescriptorToId( |  174     converted->task_id = file_manager::file_tasks::TaskDescriptorToId( | 
|  225         task.task_descriptor()); |  175         task.task_descriptor()); | 
|  226     if (!task.icon_url().is_empty()) |  176     if (!task.icon_url().is_empty()) | 
|  227       converted->icon_url = task.icon_url().spec(); |  177       converted->icon_url = task.icon_url().spec(); | 
|  228     converted->title = task.task_title(); |  178     converted->title = task.task_title(); | 
|  229     converted->is_default = task.is_default(); |  179     converted->is_default = task.is_default(); | 
|  230     results.push_back(converted); |  180     results.push_back(converted); | 
|  231   } |  181   } | 
|  232   results_ = extensions::api::file_browser_private::GetFileTasks::Results:: |  182   results_ = extensions::api::file_browser_private::GetFileTasks::Results:: | 
|  233       Create(results); |  183       Create(results); | 
|  234   SendResponse(true); |  184   SendResponse(true); | 
 |  185   return true; | 
|  235 } |  186 } | 
|  236  |  187  | 
|  237 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { |  188 bool FileBrowserPrivateSetDefaultTaskFunction::RunImpl() { | 
|  238   using extensions::api::file_browser_private::SetDefaultTask::Params; |  189   using extensions::api::file_browser_private::SetDefaultTask::Params; | 
|  239   const scoped_ptr<Params> params(Params::Create(*args_)); |  190   const scoped_ptr<Params> params(Params::Create(*args_)); | 
|  240   EXTENSION_FUNCTION_VALIDATE(params); |  191   EXTENSION_FUNCTION_VALIDATE(params); | 
|  241  |  192  | 
|  242   const scoped_refptr<fileapi::FileSystemContext> file_system_context = |  193   const scoped_refptr<fileapi::FileSystemContext> file_system_context = | 
|  243       file_manager::util::GetFileSystemContextForRenderViewHost( |  194       file_manager::util::GetFileSystemContextForRenderViewHost( | 
|  244           GetProfile(), render_view_host()); |  195           GetProfile(), render_view_host()); | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|  264     SetResult(new base::FundamentalValue(true)); |  215     SetResult(new base::FundamentalValue(true)); | 
|  265     return true; |  216     return true; | 
|  266   } |  217   } | 
|  267  |  218  | 
|  268   file_manager::file_tasks::UpdateDefaultTask( |  219   file_manager::file_tasks::UpdateDefaultTask( | 
|  269       GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); |  220       GetProfile()->GetPrefs(), params->task_id, suffixes, mime_types); | 
|  270   return true; |  221   return true; | 
|  271 } |  222 } | 
|  272  |  223  | 
|  273 }  // namespace extensions |  224 }  // namespace extensions | 
| OLD | NEW |