Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / child_process_security_policy_impl.h
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 CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
6 #define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
7
8
9 #include <map>
10 #include <set>
11 #include <string>
12
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"
20
21 class GURL;
22
23 namespace base {
24 class FilePath;
25 }
26
27 namespace storage {
28 class FileSystemURL;
29 }
30
31 namespace content {
32
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;
39
40   static ChildProcessSecurityPolicyImpl* GetInstance();
41
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
77   // Pseudo schemes are treated differently than other schemes because they
78   // cannot be requested like normal URLs.  There is no mechanism for revoking
79   // pseudo schemes.
80   void RegisterPseudoScheme(const std::string& scheme);
81
82   // Returns true iff |scheme| has been registered as pseudo scheme.
83   bool IsPseudoScheme(const std::string& scheme);
84
85   // Upon creation, child processes should register themselves by calling this
86   // this method exactly once.
87   void Add(int child_id);
88
89   // Upon creation, worker thread child processes should register themselves by
90   // calling this this method exactly once. Workers that are not shared will
91   // inherit permissions from their parent renderer process identified with
92   // |main_render_process_id|.
93   void AddWorker(int worker_child_id, int main_render_process_id);
94
95   // Upon destruction, child processess should unregister themselves by caling
96   // this method exactly once.
97   void Remove(int child_id);
98
99   // Whenever the browser processes commands the child process to request a URL,
100   // it should call this method to grant the child process the capability to
101   // request the URL, along with permission to request all URLs of the same
102   // scheme.
103   void GrantRequestURL(int child_id, const GURL& url);
104
105   // Whenever the browser process drops a file icon on a tab, it should call
106   // this method to grant the child process the capability to request this one
107   // file:// URL, but not all urls of the file:// scheme.
108   void GrantRequestSpecificFileURL(int child_id, const GURL& url);
109
110   // Revokes all permissions granted to the given file.
111   void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
112
113   // Grant the child process the ability to use Web UI Bindings.
114   void GrantWebUIBindings(int child_id);
115
116   // Grant the child process the ability to read raw cookies.
117   void GrantReadRawCookies(int child_id);
118
119   // Revoke read raw cookies permission.
120   void RevokeReadRawCookies(int child_id);
121
122   // Grants permission to send system exclusive message to any MIDI devices.
123   void GrantSendMidiSysExMessage(int child_id);
124
125   // Before servicing a child process's request for a URL, the browser should
126   // call this method to determine whether the process has the capability to
127   // request the URL.
128   bool CanRequestURL(int child_id, const GURL& url);
129
130   // Returns true if the process is permitted to load pages from
131   // the given origin in main frames or subframes.
132   // Only might return false if --site-per-process flag is used.
133   bool CanLoadPage(int child_id,
134                    const GURL& url,
135                    ResourceType resource_type);
136
137   // Explicit permissions checks for FileSystemURL specified files.
138   bool CanReadFileSystemFile(int child_id, const storage::FileSystemURL& url);
139   bool CanWriteFileSystemFile(int child_id, const storage::FileSystemURL& url);
140   bool CanCreateFileSystemFile(int child_id, const storage::FileSystemURL& url);
141   bool CanCreateReadWriteFileSystemFile(int child_id,
142                                         const storage::FileSystemURL& url);
143   bool CanCopyIntoFileSystemFile(int child_id,
144                                  const storage::FileSystemURL& url);
145   bool CanDeleteFileSystemFile(int child_id, const storage::FileSystemURL& url);
146
147   // Returns true if the specified child_id has been granted ReadRawCookies.
148   bool CanReadRawCookies(int child_id);
149
150   // Returns true if the process is permitted to read and modify the cookies for
151   // the given origin.  Does not affect cookies attached to or set by network
152   // requests.
153   // Only might return false if the very experimental
154   // --enable-strict-site-isolation or --site-per-process flags are used.
155   bool CanAccessCookiesForOrigin(int child_id, const GURL& gurl);
156
157   // Returns true if the process is permitted to attach cookies to (or have
158   // cookies set by) network requests.
159   // Only might return false if the very experimental
160   // --enable-strict-site-isolation or --site-per-process flags are used.
161   bool CanSendCookiesForOrigin(int child_id, const GURL& gurl);
162
163   // Sets the process as only permitted to use and see the cookies for the
164   // given origin.
165   // Only used if the very experimental --enable-strict-site-isolation or
166   // --site-per-process flags are used.
167   void LockToOrigin(int child_id, const GURL& gurl);
168
169   // Register FileSystem type and permission policy which should be used
170   // for the type.  The |policy| must be a bitwise-or'd value of
171   // storage::FilePermissionPolicy.
172   void RegisterFileSystemPermissionPolicy(storage::FileSystemType type,
173                                           int policy);
174
175   // Returns true if sending system exclusive messages is allowed.
176   bool CanSendMidiSysExMessage(int child_id);
177
178  private:
179   friend class ChildProcessSecurityPolicyInProcessBrowserTest;
180   friend class ChildProcessSecurityPolicyTest;
181   FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
182                            NoLeak);
183   FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
184
185   class SecurityState;
186
187   typedef std::set<std::string> SchemeSet;
188   typedef std::map<int, SecurityState*> SecurityStateMap;
189   typedef std::map<int, int> WorkerToMainProcessMap;
190   typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap;
191
192   // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
193   ChildProcessSecurityPolicyImpl();
194   friend struct DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
195
196   // Adds child process during registration.
197   void AddChild(int child_id);
198
199   // Determines if certain permissions were granted for a file to given child
200   // process. |permissions| is an internally defined bit-set.
201   bool ChildProcessHasPermissionsForFile(int child_id,
202                                          const base::FilePath& file,
203                                          int permissions);
204
205   // Grant a particular permission set for a file. |permissions| is an
206   // internally defined bit-set.
207   void GrantPermissionsForFile(int child_id,
208                                const base::FilePath& file,
209                                int permissions);
210
211   // Grants access permission to the given isolated file system
212   // identified by |filesystem_id|.  See comments for
213   // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
214   void GrantPermissionsForFileSystem(
215       int child_id,
216       const std::string& filesystem_id,
217       int permission);
218
219   // Determines if certain permissions were granted for a file. |permissions|
220   // is an internally defined bit-set. If |child_id| is a worker process,
221   // this returns true if either the worker process or its parent renderer
222   // has permissions for the file.
223   bool HasPermissionsForFile(int child_id,
224                              const base::FilePath& file,
225                              int permissions);
226
227   // Determines if certain permissions were granted for a file in FileSystem
228   // API. |permissions| is an internally defined bit-set.
229   bool HasPermissionsForFileSystemFile(int child_id,
230                                        const storage::FileSystemURL& url,
231                                        int permissions);
232
233   // Determines if certain permissions were granted for a file system.
234   // |permissions| is an internally defined bit-set.
235   bool HasPermissionsForFileSystem(
236       int child_id,
237       const std::string& filesystem_id,
238       int permission);
239
240   // You must acquire this lock before reading or writing any members of this
241   // class.  You must not block while holding this lock.
242   base::Lock lock_;
243
244   // These schemes are white-listed for all child processes.  This set is
245   // protected by |lock_|.
246   SchemeSet web_safe_schemes_;
247
248   // These schemes do not actually represent retrievable URLs.  For example,
249   // the the URLs in the "about" scheme are aliases to other URLs.  This set is
250   // protected by |lock_|.
251   SchemeSet pseudo_schemes_;
252
253   // This map holds a SecurityState for each child process.  The key for the
254   // map is the ID of the ChildProcessHost.  The SecurityState objects are
255   // owned by this object and are protected by |lock_|.  References to them must
256   // not escape this class.
257   SecurityStateMap security_state_;
258
259   // This maps keeps the record of which js worker thread child process
260   // corresponds to which main js thread child process.
261   WorkerToMainProcessMap worker_map_;
262
263   FileSystemPermissionPolicyMap file_system_policy_map_;
264
265   DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
266 };
267
268 }  // namespace content
269
270 #endif  // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_