File: app_file_handler_util.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 5,913 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_
#define EXTENSIONS_BROWSER_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_

#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/file_handler_info.h"

namespace content {
class BrowserContext;
}

namespace apps {
struct FileHandler;
struct FileHandlerInfo;
using FileHandlers = std::vector<FileHandler>;
}

namespace extensions {

struct EntryInfo;
struct FileHandlerMatch;
struct GrantedFileEntry;

// TODO(michaelpg,benwells): move this to an app-specific namespace and
// directory.
namespace app_file_handler_util {

extern const char kInvalidParameters[];
extern const char kSecurityError[];

class WebAppFileHandlerMatch {
 public:
  explicit WebAppFileHandlerMatch(const apps::FileHandler* file_handler);
  ~WebAppFileHandlerMatch();

  const apps::FileHandler& file_handler() const;

  // Returns true if |file_handler_| matched an entry on MIME type.
  bool matched_mime_type() const;

  // Returns true if |file_handler_| matched an entry on file extension.
  bool matched_file_extension() const;

  // Returns whether |file_handler_| can handle |entry| on either MIME type or
  // file extension, and sets the values of |matched_mime_type_| and
  // |matched_file_extension_|.
  bool DoMatch(const EntryInfo& entry);

 private:
  const raw_ptr<const apps::FileHandler> file_handler_;
  bool matched_mime_type_ = false;
  bool matched_file_extension_ = false;
};

// Returns the file handler with the specified |handler_id|, or NULL if there
// is no such handler.
const apps::FileHandlerInfo* FileHandlerForId(const Extension& app,
                                              const std::string& handler_id);

// Returns the handlers that can handle all files in |entries|
// along with metadata about how the handler matched (MIME or file extension)
std::vector<FileHandlerMatch> FindFileHandlerMatchesForEntries(
    const Extension& extension,
    const std::vector<EntryInfo>& entries);

// Returns the handlers that can handle all files in |entries|
// along with metadata about how the handler matched (MIME or file)
std::vector<FileHandlerMatch> MatchesFromFileHandlersForEntries(
    const FileHandlersInfo& file_handlers,
    const std::vector<EntryInfo>& entries);

// Returns the apps::FileHandlers that can handle all files in |entries", along
// with metadata about how the handler matched (MIME type or file extension).
std::vector<WebAppFileHandlerMatch> MatchesFromWebAppFileHandlersForEntries(
    const apps::FileHandlers& file_handlers,
    const std::vector<EntryInfo>& entries);

bool FileHandlerCanHandleEntry(const apps::FileHandlerInfo& handler,
                               const EntryInfo& entry);

bool WebAppFileHandlerCanHandleEntry(const apps::FileHandler& handler,
                                     const EntryInfo& entry);

// Creates a new file entry and allows |renderer_id| to access |path|, with
// specified permissions. This registers a new file system for |path|. Note
// that |can_create| and |can_delete| both require |can_write|.
GrantedFileEntry CreateFileEntryWithPermissions(int renderer_id,
                                                const base::FilePath& path,
                                                bool can_write,
                                                bool can_create,
                                                bool can_delete);

// Creates a new file entry and allows |renderer_id| to access |path|. This
// registers a new file system for |path|.
GrantedFileEntry CreateFileEntry(content::BrowserContext* context,
                                 const Extension* extension,
                                 int renderer_id,
                                 const base::FilePath& path,
                                 bool is_directory);

// |directory_paths| contain the set of directories out of |paths|.
// For directories it makes sure they exist at their corresponding |paths|,
// while for regular files it makes sure they exist (i.e. not links) at |paths|,
// creating files if needed. If result is successful it calls |on_success|,
// otherwise calls |on_failure|.
void PrepareFilesForWritableApp(
    const std::vector<base::FilePath>& paths,
    content::BrowserContext* context,
    const std::set<base::FilePath>& directory_paths,
    base::OnceClosure on_success,
    base::OnceCallback<void(const base::FilePath&)> on_failure);

// Returns whether |extension| has the fileSystem.write permission.
bool HasFileSystemWritePermission(const Extension* extension);

// Validates a file entry and populates |file_path| with the absolute path if it
// is valid.
bool ValidateFileEntryAndGetPath(const std::string& filesystem_name,
                                 const std::string& filesystem_path,
                                 int render_process_id,
                                 base::FilePath* file_path,
                                 std::string* error);

// Returns a vector of EntryInfo with:
// * |path| fields populated by |entry_paths|.
// * |mime_type| fields populated by |mime_types| (must have same size as
//   |entry_paths|), with empty elements replaced by a fallback.
// * |is_directory| fields decided by whether |path| is in |directory_paths|.
std::vector<extensions::EntryInfo> CreateEntryInfos(
    const std::vector<base::FilePath>& entry_paths,
    const std::vector<std::string>& mime_types,
    const std::set<base::FilePath>& directory_paths);

}  // namespace app_file_handler_util

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_FILE_HANDLERS_APP_FILE_HANDLER_UTIL_H_