| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/file_handlers/app_file_handler_util.h" | 5 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_prefs.h" | 7 #include "chrome/browser/extensions/extension_prefs.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "content/public/browser/child_process_security_policy.h" | 10 #include "content/public/browser/child_process_security_policy.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 // Also accept files with no extension for handlers that support an | 36 // Also accept files with no extension for handlers that support an |
| 37 // empty extension, i.e. both "foo" and "foo." match. | 37 // empty extension, i.e. both "foo" and "foo." match. |
| 38 if (extension->empty() && | 38 if (extension->empty() && |
| 39 path.MatchesExtension(base::FilePath::StringType())) { | 39 path.MatchesExtension(base::FilePath::StringType())) { |
| 40 return true; | 40 return true; |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 return false; | 43 return false; |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool FileHandlerCanHandleFileWithMimeType( |
| 47 const FileHandlerInfo& handler, |
| 48 const std::string& mime_type) { |
| 49 for (std::set<std::string>::const_iterator type = handler.types.begin(); |
| 50 type != handler.types.end(); ++type) { |
| 51 if (net::MatchesMimeType(*type, mime_type)) |
| 52 return true; |
| 53 } |
| 54 return false; |
| 55 } |
| 56 |
| 46 } // namespace | 57 } // namespace |
| 47 | 58 |
| 48 typedef std::vector<FileHandlerInfo> FileHandlerList; | 59 typedef std::vector<FileHandlerInfo> FileHandlerList; |
| 49 | 60 |
| 50 const FileHandlerInfo* FileHandlerForId(const Extension& app, | 61 const FileHandlerInfo* FileHandlerForId(const Extension& app, |
| 51 const std::string& handler_id) { | 62 const std::string& handler_id) { |
| 52 const FileHandlerList* file_handlers = FileHandlers::GetFileHandlers(&app); | 63 const FileHandlerList* file_handlers = FileHandlers::GetFileHandlers(&app); |
| 53 if (!file_handlers) | 64 if (!file_handlers) |
| 54 return NULL; | 65 return NULL; |
| 55 | 66 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 return NULL; | 81 return NULL; |
| 71 | 82 |
| 72 for (FileHandlerList::const_iterator i = file_handlers->begin(); | 83 for (FileHandlerList::const_iterator i = file_handlers->begin(); |
| 73 i != file_handlers->end(); i++) { | 84 i != file_handlers->end(); i++) { |
| 74 if (FileHandlerCanHandleFile(*i, mime_type, path)) | 85 if (FileHandlerCanHandleFile(*i, mime_type, path)) |
| 75 return &*i; | 86 return &*i; |
| 76 } | 87 } |
| 77 return NULL; | 88 return NULL; |
| 78 } | 89 } |
| 79 | 90 |
| 80 std::vector<const FileHandlerInfo*> FindFileHandlersForMimeTypes( | 91 std::vector<const FileHandlerInfo*> FindFileHandlersForFiles( |
| 81 const Extension& app, const std::set<std::string>& mime_types) { | 92 const Extension& app, const PathAndMimeTypeSet& files) { |
| 82 std::vector<const FileHandlerInfo*> handlers; | 93 std::vector<const FileHandlerInfo*> handlers; |
| 83 if (mime_types.empty()) | 94 if (files.empty()) |
| 84 return handlers; | 95 return handlers; |
| 85 | 96 |
| 86 // Look for file handlers which can handle all the MIME types specified. | 97 // Look for file handlers which can handle all the MIME types specified. |
| 87 const FileHandlerList* file_handlers = FileHandlers::GetFileHandlers(&app); | 98 const FileHandlerList* file_handlers = FileHandlers::GetFileHandlers(&app); |
| 88 if (!file_handlers) | 99 if (!file_handlers) |
| 89 return handlers; | 100 return handlers; |
| 90 | 101 |
| 91 for (FileHandlerList::const_iterator data = file_handlers->begin(); | 102 for (FileHandlerList::const_iterator data = file_handlers->begin(); |
| 92 data != file_handlers->end(); ++data) { | 103 data != file_handlers->end(); ++data) { |
| 93 bool handles_all_types = true; | 104 bool handles_all_types = true; |
| 94 for (std::set<std::string>::const_iterator type_iter = mime_types.begin(); | 105 for (PathAndMimeTypeSet::const_iterator it = files.begin(); |
| 95 type_iter != mime_types.end(); ++type_iter) { | 106 it != files.end(); ++it) { |
| 96 if (!FileHandlerCanHandleFileWithMimeType(*data, *type_iter)) { | 107 if (!FileHandlerCanHandleFile(*data, it->second, it->first)) { |
| 97 handles_all_types = false; | 108 handles_all_types = false; |
| 98 break; | 109 break; |
| 99 } | 110 } |
| 100 } | 111 } |
| 101 if (handles_all_types) | 112 if (handles_all_types) |
| 102 handlers.push_back(&*data); | 113 handlers.push_back(&*data); |
| 103 } | 114 } |
| 104 return handlers; | 115 return handlers; |
| 105 } | 116 } |
| 106 | 117 |
| 107 bool FileHandlerCanHandleFile( | 118 bool FileHandlerCanHandleFile( |
| 108 const FileHandlerInfo& handler, | 119 const FileHandlerInfo& handler, |
| 109 const std::string& mime_type, | 120 const std::string& mime_type, |
| 110 const base::FilePath& path) { | 121 const base::FilePath& path) { |
| 111 return FileHandlerCanHandleFileWithMimeType(handler, mime_type) || | 122 return FileHandlerCanHandleFileWithMimeType(handler, mime_type) || |
| 112 FileHandlerCanHandleFileWithExtension(handler, path); | 123 FileHandlerCanHandleFileWithExtension(handler, path); |
| 113 } | 124 } |
| 114 | 125 |
| 115 bool FileHandlerCanHandleFileWithMimeType( | |
| 116 const FileHandlerInfo& handler, | |
| 117 const std::string& mime_type) { | |
| 118 for (std::set<std::string>::const_iterator type = handler.types.begin(); | |
| 119 type != handler.types.end(); ++type) { | |
| 120 if (net::MatchesMimeType(*type, mime_type)) | |
| 121 return true; | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 GrantedFileEntry CreateFileEntry( | 126 GrantedFileEntry CreateFileEntry( |
| 127 Profile* profile, | 127 Profile* profile, |
| 128 const std::string& extension_id, | 128 const std::string& extension_id, |
| 129 int renderer_id, | 129 int renderer_id, |
| 130 const base::FilePath& path, | 130 const base::FilePath& path, |
| 131 bool writable) { | 131 bool writable) { |
| 132 GrantedFileEntry result; | 132 GrantedFileEntry result; |
| 133 fileapi::IsolatedContext* isolated_context = | 133 fileapi::IsolatedContext* isolated_context = |
| 134 fileapi::IsolatedContext::GetInstance(); | 134 fileapi::IsolatedContext::GetInstance(); |
| 135 DCHECK(isolated_context); | 135 DCHECK(isolated_context); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 156 extension_service()->extension_prefs(); | 156 extension_service()->extension_prefs(); |
| 157 // Save this file entry in the prefs. | 157 // Save this file entry in the prefs. |
| 158 prefs->AddSavedFileEntry(extension_id, result.id, path, writable); | 158 prefs->AddSavedFileEntry(extension_id, result.id, path, writable); |
| 159 | 159 |
| 160 return result; | 160 return result; |
| 161 } | 161 } |
| 162 | 162 |
| 163 } // namespace app_file_handler_util | 163 } // namespace app_file_handler_util |
| 164 | 164 |
| 165 } // namespace extensions | 165 } // namespace extensions |
| OLD | NEW |