chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / content / browser / child_process_security_policy_impl.h
blob61486b9a28b483f12e35da14426f0bbfb3f173f1
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.
5 #ifndef CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
6 #define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
9 #include <map>
10 #include <set>
11 #include <string>
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/singleton.h"
16 #include "base/synchronization/lock.h"
17 #include "content/public/browser/child_process_security_policy.h"
18 #include "content/public/common/resource_type.h"
19 #include "storage/common/fileapi/file_system_types.h"
21 class GURL;
23 namespace base {
24 class FilePath;
27 namespace storage {
28 class FileSystemURL;
31 namespace content {
33 class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
34 : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) {
35 public:
36 // Object can only be created through GetInstance() so the constructor is
37 // private.
38 ~ChildProcessSecurityPolicyImpl() override;
40 static ChildProcessSecurityPolicyImpl* GetInstance();
42 // ChildProcessSecurityPolicy implementation.
43 void RegisterWebSafeScheme(const std::string& scheme) override;
44 bool IsWebSafeScheme(const std::string& scheme) override;
45 void GrantReadFile(int child_id, const base::FilePath& file) override;
46 void GrantCreateReadWriteFile(int child_id,
47 const base::FilePath& file) override;
48 void GrantCopyInto(int child_id, const base::FilePath& dir) override;
49 void GrantDeleteFrom(int child_id, const base::FilePath& dir) override;
50 void GrantReadFileSystem(int child_id,
51 const std::string& filesystem_id) override;
52 void GrantWriteFileSystem(int child_id,
53 const std::string& filesystem_id) override;
54 void GrantCreateFileForFileSystem(int child_id,
55 const std::string& filesystem_id) override;
56 void GrantCreateReadWriteFileSystem(
57 int child_id,
58 const std::string& filesystem_id) override;
59 void GrantCopyIntoFileSystem(int child_id,
60 const std::string& filesystem_id) override;
61 void GrantDeleteFromFileSystem(int child_id,
62 const std::string& filesystem_id) override;
63 void GrantScheme(int child_id, const std::string& scheme) override;
64 bool CanReadFile(int child_id, const base::FilePath& file) override;
65 bool CanCreateReadWriteFile(int child_id,
66 const base::FilePath& file) override;
67 bool CanReadFileSystem(int child_id,
68 const std::string& filesystem_id) override;
69 bool CanReadWriteFileSystem(int child_id,
70 const std::string& filesystem_id) override;
71 bool CanCopyIntoFileSystem(int child_id,
72 const std::string& filesystem_id) override;
73 bool CanDeleteFromFileSystem(int child_id,
74 const std::string& filesystem_id) override;
75 bool HasWebUIBindings(int child_id) override;
76 void GrantSendMidiSysExMessage(int child_id) override;
78 // Pseudo schemes are treated differently than other schemes because they
79 // cannot be requested like normal URLs. There is no mechanism for revoking
80 // pseudo schemes.
81 void RegisterPseudoScheme(const std::string& scheme);
83 // Returns true iff |scheme| has been registered as pseudo scheme.
84 bool IsPseudoScheme(const std::string& scheme);
86 // Upon creation, child processes should register themselves by calling this
87 // this method exactly once.
88 void Add(int child_id);
90 // Upon creation, worker thread child processes should register themselves by
91 // calling this this method exactly once. Workers that are not shared will
92 // inherit permissions from their parent renderer process identified with
93 // |main_render_process_id|.
94 void AddWorker(int worker_child_id, int main_render_process_id);
96 // Upon destruction, child processess should unregister themselves by caling
97 // this method exactly once.
98 void Remove(int child_id);
100 // Whenever the browser processes commands the child process to request a URL,
101 // it should call this method to grant the child process the capability to
102 // request the URL, along with permission to request all URLs of the same
103 // scheme.
104 void GrantRequestURL(int child_id, const GURL& url);
106 // Whenever the browser process drops a file icon on a tab, it should call
107 // this method to grant the child process the capability to request this one
108 // file:// URL, but not all urls of the file:// scheme.
109 void GrantRequestSpecificFileURL(int child_id, const GURL& url);
111 // Revokes all permissions granted to the given file.
112 void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
114 // Grant the child process the ability to use Web UI Bindings.
115 void GrantWebUIBindings(int child_id);
117 // Grant the child process the ability to read raw cookies.
118 void GrantReadRawCookies(int child_id);
120 // Revoke read raw cookies permission.
121 void RevokeReadRawCookies(int child_id);
123 // Before servicing a child process's request for a URL, the browser should
124 // call this method to determine whether the process has the capability to
125 // request the URL.
126 bool CanRequestURL(int child_id, const GURL& url);
128 // Returns true if the process is permitted to load pages from
129 // the given origin in main frames or subframes.
130 // Only might return false if --site-per-process flag is used.
131 bool CanLoadPage(int child_id,
132 const GURL& url,
133 ResourceType resource_type);
135 // Explicit permissions checks for FileSystemURL specified files.
136 bool CanReadFileSystemFile(int child_id, const storage::FileSystemURL& url);
137 bool CanWriteFileSystemFile(int child_id, const storage::FileSystemURL& url);
138 bool CanCreateFileSystemFile(int child_id, const storage::FileSystemURL& url);
139 bool CanCreateReadWriteFileSystemFile(int child_id,
140 const storage::FileSystemURL& url);
141 bool CanCopyIntoFileSystemFile(int child_id,
142 const storage::FileSystemURL& url);
143 bool CanDeleteFileSystemFile(int child_id, const storage::FileSystemURL& url);
145 // Returns true if the specified child_id has been granted ReadRawCookies.
146 bool CanReadRawCookies(int child_id);
148 // Returns true if the process is permitted to read and modify the cookies for
149 // the given origin. Does not affect cookies attached to or set by network
150 // requests.
151 // Only might return false if the --site-per-process flag is used.
152 bool CanAccessCookiesForOrigin(int child_id, const GURL& gurl);
154 // Sets the process as only permitted to use and see the cookies for the
155 // given origin.
156 // Origin lock is applied only if the --site-per-process flag is used.
157 void LockToOrigin(int child_id, const GURL& gurl);
159 // Register FileSystem type and permission policy which should be used
160 // for the type. The |policy| must be a bitwise-or'd value of
161 // storage::FilePermissionPolicy.
162 void RegisterFileSystemPermissionPolicy(storage::FileSystemType type,
163 int policy);
165 // Returns true if sending system exclusive messages is allowed.
166 bool CanSendMidiSysExMessage(int child_id);
168 private:
169 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
170 friend class ChildProcessSecurityPolicyTest;
171 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
172 NoLeak);
173 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
175 class SecurityState;
177 typedef std::set<std::string> SchemeSet;
178 typedef std::map<int, SecurityState*> SecurityStateMap;
179 typedef std::map<int, int> WorkerToMainProcessMap;
180 typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap;
182 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
183 ChildProcessSecurityPolicyImpl();
184 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
186 // Adds child process during registration.
187 void AddChild(int child_id);
189 // Determines if certain permissions were granted for a file to given child
190 // process. |permissions| is an internally defined bit-set.
191 bool ChildProcessHasPermissionsForFile(int child_id,
192 const base::FilePath& file,
193 int permissions);
195 // Grant a particular permission set for a file. |permissions| is an
196 // internally defined bit-set.
197 void GrantPermissionsForFile(int child_id,
198 const base::FilePath& file,
199 int permissions);
201 // Grants access permission to the given isolated file system
202 // identified by |filesystem_id|. See comments for
203 // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
204 void GrantPermissionsForFileSystem(
205 int child_id,
206 const std::string& filesystem_id,
207 int permission);
209 // Determines if certain permissions were granted for a file. |permissions|
210 // is an internally defined bit-set. If |child_id| is a worker process,
211 // this returns true if either the worker process or its parent renderer
212 // has permissions for the file.
213 bool HasPermissionsForFile(int child_id,
214 const base::FilePath& file,
215 int permissions);
217 // Determines if certain permissions were granted for a file in FileSystem
218 // API. |permissions| is an internally defined bit-set.
219 bool HasPermissionsForFileSystemFile(int child_id,
220 const storage::FileSystemURL& url,
221 int permissions);
223 // Determines if certain permissions were granted for a file system.
224 // |permissions| is an internally defined bit-set.
225 bool HasPermissionsForFileSystem(
226 int child_id,
227 const std::string& filesystem_id,
228 int permission);
230 // You must acquire this lock before reading or writing any members of this
231 // class. You must not block while holding this lock.
232 base::Lock lock_;
234 // These schemes are white-listed for all child processes. This set is
235 // protected by |lock_|.
236 SchemeSet web_safe_schemes_;
238 // These schemes do not actually represent retrievable URLs. For example,
239 // the the URLs in the "about" scheme are aliases to other URLs. This set is
240 // protected by |lock_|.
241 SchemeSet pseudo_schemes_;
243 // This map holds a SecurityState for each child process. The key for the
244 // map is the ID of the ChildProcessHost. The SecurityState objects are
245 // owned by this object and are protected by |lock_|. References to them must
246 // not escape this class.
247 SecurityStateMap security_state_;
249 // This maps keeps the record of which js worker thread child process
250 // corresponds to which main js thread child process.
251 WorkerToMainProcessMap worker_map_;
253 FileSystemPermissionPolicyMap file_system_policy_map_;
255 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
258 } // namespace content
260 #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_