blob: 9cde10a5b89689008051f3f5faffc810bbb8f9e4 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f58ddcf2009-05-18 22:22:065#ifndef CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_
6#define CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_
initial.commit09911bf2008-07-26 23:55:297
8#include <string>
9#include <map>
10#include <set>
11
12#include "base/basictypes.h"
13#include "base/lock.h"
14#include "base/singleton.h"
[email protected]79dc62e32009-05-19 21:02:5815#include "testing/gtest/include/gtest/gtest_prod.h"
[email protected]46072d42008-07-28 14:49:3516
[email protected]561abe62009-04-06 18:08:3417class FilePath;
[email protected]46072d42008-07-28 14:49:3518class GURL;
initial.commit09911bf2008-07-26 23:55:2919
[email protected]f58ddcf2009-05-18 22:22:0620// The ChildProcessSecurityPolicy class is used to grant and revoke security
initial.commit09911bf2008-07-26 23:55:2921// capabilities for renderers. For example, it restricts whether a renderer
22// is permmitted to loaded file:// URLs based on whether the renderer has ever
23// been commanded to load file:// URLs by the browser.
24//
[email protected]f58ddcf2009-05-18 22:22:0625// ChildProcessSecurityPolicy is a singleton that may be used on any thread.
initial.commit09911bf2008-07-26 23:55:2926//
[email protected]f58ddcf2009-05-18 22:22:0627class ChildProcessSecurityPolicy {
initial.commit09911bf2008-07-26 23:55:2928 public:
[email protected]c11ffb462009-05-15 18:03:4029 // Object can only be created through GetInstance() so the constructor is
30 // private.
[email protected]f58ddcf2009-05-18 22:22:0631 ~ChildProcessSecurityPolicy();
[email protected]c11ffb462009-05-15 18:03:4032
[email protected]580522632009-08-17 21:55:5533 // There is one global ChildProcessSecurityPolicy object for the entire
34 // browser process. The object returned by this method may be accessed on
35 // any thread.
[email protected]f58ddcf2009-05-18 22:22:0636 static ChildProcessSecurityPolicy* GetInstance();
initial.commit09911bf2008-07-26 23:55:2937
38 // Web-safe schemes can be requested by any renderer. Once a web-safe scheme
39 // has been registered, any renderer processes can request URLs with that
40 // scheme. There is no mechanism for revoking web-safe schemes.
41 void RegisterWebSafeScheme(const std::string& scheme);
42
43 // Returns true iff |scheme| has been registered as a web-safe scheme.
44 bool IsWebSafeScheme(const std::string& scheme);
45
46 // Pseudo schemes are treated differently than other schemes because they
47 // cannot be requested like normal URLs. There is no mechanism for revoking
48 // pseudo schemes.
49 void RegisterPseudoScheme(const std::string& scheme);
50
51 // Returns true iff |scheme| has been registered as pseudo scheme.
52 bool IsPseudoScheme(const std::string& scheme);
53
54 // Upon creation, render processes should register themselves by calling this
55 // this method exactly once.
56 void Add(int renderer_id);
57
58 // Upon destruction, render processess should unregister themselves by caling
59 // this method exactly once.
60 void Remove(int renderer_id);
61
62 // Whenever the browser processes commands the renderer to request a URL, it
63 // should call this method to grant the renderer process the capability to
64 // request the URL.
65 void GrantRequestURL(int renderer_id, const GURL& url);
66
67 // Whenever the user picks a file from a <input type="file"> element, the
68 // browser should call this function to grant the renderer the capability to
69 // upload the file to the web.
[email protected]561abe62009-04-06 18:08:3470 void GrantUploadFile(int renderer_id, const FilePath& file);
initial.commit09911bf2008-07-26 23:55:2971
72 // Whenever the browser processes commands the renderer to run web inspector,
73 // it should call this method to grant the renderer process the capability to
74 // run the inspector.
75 void GrantInspectElement(int renderer_id);
76
77 // Grant this renderer the ability to use DOM UI Bindings.
78 void GrantDOMUIBindings(int renderer_id);
79
[email protected]1adff062009-06-02 18:39:5580 // Grant this renderer the ability to use extension Bindings.
81 void GrantExtensionBindings(int renderer_id);
82
[email protected]971713e2009-10-29 16:07:2183 // Grant this renderer the ability to read raw cookies.
84 void GrantReadRawCookies(int renderer_id);
85
86 // Revoke read raw cookies permission.
87 void RevokeReadRawCookies(int renderer_id);
88
initial.commit09911bf2008-07-26 23:55:2989 // Before servicing a renderer's request for a URL, the browser should call
90 // this method to determine whether the renderer has the capability to
91 // request the URL.
92 bool CanRequestURL(int renderer_id, const GURL& url);
93
94 // Before servicing a renderer's request to upload a file to the web, the
95 // browser should call this method to determine whether the renderer has the
96 // capability to upload the requested file.
[email protected]561abe62009-04-06 18:08:3497 bool CanUploadFile(int renderer_id, const FilePath& file);
initial.commit09911bf2008-07-26 23:55:2998
[email protected]971713e2009-10-29 16:07:2199 // Returns true if the specified renderer_id has been granted DOMUIBindings.
initial.commit09911bf2008-07-26 23:55:29100 // The browser should check this property before assuming the renderer is
101 // allowed to use DOMUIBindings.
102 bool HasDOMUIBindings(int renderer_id);
103
[email protected]b7c2f252009-12-08 00:47:23104 // Returns true if the specified renderer_id has been granted DOMUIBindings.
105 // The browser should check this property before assuming the renderer is
106 // allowed to use extension bindings.
[email protected]1adff062009-06-02 18:39:55107 bool HasExtensionBindings(int renderer_id);
108
[email protected]971713e2009-10-29 16:07:21109 // Returns true if the specified renderer_id has been granted ReadRawCookies.
110 bool CanReadRawCookies(int renderer_id);
111
initial.commit09911bf2008-07-26 23:55:29112 private:
[email protected]79dc62e32009-05-19 21:02:58113 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
114 FRIEND_TEST(ChildProcessSecurityPolicyInProcessBrowserTest, NoLeak);
115
initial.commit09911bf2008-07-26 23:55:29116 class SecurityState;
117
118 typedef std::set<std::string> SchemeSet;
119 typedef std::map<int, SecurityState*> SecurityStateMap;
120
[email protected]f58ddcf2009-05-18 22:22:06121 // Obtain an instance of ChildProcessSecurityPolicy via GetInstance().
122 ChildProcessSecurityPolicy();
123 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicy>;
initial.commit09911bf2008-07-26 23:55:29124
125 // You must acquire this lock before reading or writing any members of this
126 // class. You must not block while holding this lock.
127 Lock lock_;
128
129 // These schemes are white-listed for all renderers. This set is protected
130 // by |lock_|.
131 SchemeSet web_safe_schemes_;
132
133 // These schemes do not actually represent retrievable URLs. For example,
134 // the the URLs in the "about" scheme are aliases to other URLs. This set is
135 // protected by |lock_|.
136 SchemeSet pseudo_schemes_;
137
138 // This map holds a SecurityState for each renderer process. The key for the
139 // map is the ID of the RenderProcessHost. The SecurityState objects are
140 // owned by this object and are protected by |lock_|. References to them must
141 // not escape this class.
142 SecurityStateMap security_state_;
143
[email protected]f58ddcf2009-05-18 22:22:06144 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicy);
initial.commit09911bf2008-07-26 23:55:29145};
146
[email protected]f58ddcf2009-05-18 22:22:06147#endif // CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_