Home | History | Annotate | Download | only in file_handlers
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "content/public/browser/render_view_host.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/manifest_handlers/file_handler_info.h"
     16 
     17 class Profile;
     18 
     19 namespace extensions {
     20 
     21 class ExtensionPrefs;
     22 struct GrantedFileEntry;
     23 
     24 // TODO(benwells): move this to platform_apps namespace.
     25 namespace app_file_handler_util {
     26 
     27 extern const char kInvalidParameters[];
     28 extern const char kSecurityError[];
     29 
     30 // A set of pairs of path and its corresponding MIME type.
     31 typedef std::set<std::pair<base::FilePath, std::string> > PathAndMimeTypeSet;
     32 
     33 // Returns the file handler with the specified |handler_id|, or NULL if there
     34 // is no such handler.
     35 const FileHandlerInfo* FileHandlerForId(const Extension& app,
     36                                         const std::string& handler_id);
     37 
     38 // Returns the first file handler that can handle the given MIME type or
     39 // filename, or NULL if is no such handler.
     40 const FileHandlerInfo* FirstFileHandlerForFile(
     41     const Extension& app,
     42     const std::string& mime_type,
     43     const base::FilePath& path);
     44 
     45 // Returns the handlers that can handle all files in |files|. The paths in
     46 // |files| must be populated, but the MIME types are optional.
     47 std::vector<const FileHandlerInfo*>
     48 FindFileHandlersForFiles(const Extension& extension,
     49                          const PathAndMimeTypeSet& files);
     50 
     51 bool FileHandlerCanHandleFile(
     52     const FileHandlerInfo& handler,
     53     const std::string& mime_type,
     54     const base::FilePath& path);
     55 
     56 // Creates a new file entry and allows |renderer_id| to access |path|. This
     57 // registers a new file system for |path|.
     58 GrantedFileEntry CreateFileEntry(Profile* profile,
     59                                  const Extension* extension,
     60                                  int renderer_id,
     61                                  const base::FilePath& path,
     62                                  bool is_directory);
     63 
     64 // When |is_directory| is true, it verifies that directories exist at each of
     65 // the |paths| and calls back to |on_success| or otherwise to |on_failure|.
     66 // When |is_directory| is false, it ensures regular files exists (not links and
     67 // directories) at the |paths|, creating files if needed, and calls back to
     68 // |on_success| or to |on_failure| depending on the result.
     69 void PrepareFilesForWritableApp(
     70     const std::vector<base::FilePath>& paths,
     71     Profile* profile,
     72     bool is_directory,
     73     const base::Closure& on_success,
     74     const base::Callback<void(const base::FilePath&)>& on_failure);
     75 
     76 // Returns whether |extension| has the fileSystem.write permission.
     77 bool HasFileSystemWritePermission(const Extension* extension);
     78 
     79 // Validates a file entry and populates |file_path| with the absolute path if it
     80 // is valid.
     81 bool ValidateFileEntryAndGetPath(
     82     const std::string& filesystem_name,
     83     const std::string& filesystem_path,
     84     const content::RenderViewHost* render_view_host,
     85     base::FilePath* file_path,
     86     std::string* error);
     87 
     88 }  // namespace app_file_handler_util
     89 
     90 }  // namespace extensions
     91 
     92 #endif  // CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_
     93