license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |
| 6 | #define CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <map> |
| 10 | #include <set> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 13 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/lock.h" |
| 15 | #include "base/singleton.h" |
[email protected] | 79dc62e3 | 2009-05-19 21:02:58 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest_prod.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 17 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 18 | class FilePath; |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 19 | class GURL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 21 | // The ChildProcessSecurityPolicy class is used to grant and revoke security |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | // capabilities for renderers. For example, it restricts whether a renderer |
| 23 | // is permmitted to loaded file:// URLs based on whether the renderer has ever |
| 24 | // been commanded to load file:// URLs by the browser. |
| 25 | // |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 26 | // ChildProcessSecurityPolicy is a singleton that may be used on any thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | // |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 28 | class ChildProcessSecurityPolicy { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | public: |
[email protected] | c11ffb46 | 2009-05-15 18:03:40 | [diff] [blame] | 30 | // Object can only be created through GetInstance() so the constructor is |
| 31 | // private. |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 32 | ~ChildProcessSecurityPolicy(); |
[email protected] | c11ffb46 | 2009-05-15 18:03:40 | [diff] [blame] | 33 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 34 | // There is one global ChildProcessSecurityPolicy object for the entire browser |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | // processes. The object returned by this method may be accessed on any |
| 36 | // thread. |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 37 | static ChildProcessSecurityPolicy* GetInstance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
| 39 | // Web-safe schemes can be requested by any renderer. Once a web-safe scheme |
| 40 | // has been registered, any renderer processes can request URLs with that |
| 41 | // scheme. There is no mechanism for revoking web-safe schemes. |
| 42 | void RegisterWebSafeScheme(const std::string& scheme); |
| 43 | |
| 44 | // Returns true iff |scheme| has been registered as a web-safe scheme. |
| 45 | bool IsWebSafeScheme(const std::string& scheme); |
| 46 | |
| 47 | // Pseudo schemes are treated differently than other schemes because they |
| 48 | // cannot be requested like normal URLs. There is no mechanism for revoking |
| 49 | // pseudo schemes. |
| 50 | void RegisterPseudoScheme(const std::string& scheme); |
| 51 | |
| 52 | // Returns true iff |scheme| has been registered as pseudo scheme. |
| 53 | bool IsPseudoScheme(const std::string& scheme); |
| 54 | |
| 55 | // Upon creation, render processes should register themselves by calling this |
| 56 | // this method exactly once. |
| 57 | void Add(int renderer_id); |
| 58 | |
| 59 | // Upon destruction, render processess should unregister themselves by caling |
| 60 | // this method exactly once. |
| 61 | void Remove(int renderer_id); |
| 62 | |
| 63 | // Whenever the browser processes commands the renderer to request a URL, it |
| 64 | // should call this method to grant the renderer process the capability to |
| 65 | // request the URL. |
| 66 | void GrantRequestURL(int renderer_id, const GURL& url); |
| 67 | |
| 68 | // Whenever the user picks a file from a <input type="file"> element, the |
| 69 | // browser should call this function to grant the renderer the capability to |
| 70 | // upload the file to the web. |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 71 | void GrantUploadFile(int renderer_id, const FilePath& file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | |
| 73 | // Whenever the browser processes commands the renderer to run web inspector, |
| 74 | // it should call this method to grant the renderer process the capability to |
| 75 | // run the inspector. |
| 76 | void GrantInspectElement(int renderer_id); |
| 77 | |
| 78 | // Grant this renderer the ability to use DOM UI Bindings. |
| 79 | void GrantDOMUIBindings(int renderer_id); |
| 80 | |
[email protected] | 1adff06 | 2009-06-02 18:39:55 | [diff] [blame^] | 81 | // Grant this renderer the ability to use extension Bindings. |
| 82 | void GrantExtensionBindings(int renderer_id); |
| 83 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // Before servicing a renderer's request for a URL, the browser should call |
| 85 | // this method to determine whether the renderer has the capability to |
| 86 | // request the URL. |
| 87 | bool CanRequestURL(int renderer_id, const GURL& url); |
| 88 | |
| 89 | // Before servicing a renderer's request to upload a file to the web, the |
| 90 | // browser should call this method to determine whether the renderer has the |
| 91 | // capability to upload the requested file. |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 92 | bool CanUploadFile(int renderer_id, const FilePath& file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
| 94 | // Returns true of the specified renderer_id has been granted DOMUIBindings. |
| 95 | // The browser should check this property before assuming the renderer is |
| 96 | // allowed to use DOMUIBindings. |
| 97 | bool HasDOMUIBindings(int renderer_id); |
| 98 | |
[email protected] | 1adff06 | 2009-06-02 18:39:55 | [diff] [blame^] | 99 | // Returns true of the specified renderer_id has been granted DOMUIBindings. |
| 100 | // The browser should check this property before assuming the renderer is |
| 101 | // allowed to use extension bindings. |
| 102 | bool HasExtensionBindings(int renderer_id); |
| 103 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | private: |
[email protected] | 79dc62e3 | 2009-05-19 21:02:58 | [diff] [blame] | 105 | friend class ChildProcessSecurityPolicyInProcessBrowserTest; |
| 106 | FRIEND_TEST(ChildProcessSecurityPolicyInProcessBrowserTest, NoLeak); |
| 107 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | class SecurityState; |
| 109 | |
| 110 | typedef std::set<std::string> SchemeSet; |
| 111 | typedef std::map<int, SecurityState*> SecurityStateMap; |
| 112 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 113 | // Obtain an instance of ChildProcessSecurityPolicy via GetInstance(). |
| 114 | ChildProcessSecurityPolicy(); |
| 115 | friend struct DefaultSingletonTraits<ChildProcessSecurityPolicy>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
| 117 | // You must acquire this lock before reading or writing any members of this |
| 118 | // class. You must not block while holding this lock. |
| 119 | Lock lock_; |
| 120 | |
| 121 | // These schemes are white-listed for all renderers. This set is protected |
| 122 | // by |lock_|. |
| 123 | SchemeSet web_safe_schemes_; |
| 124 | |
| 125 | // These schemes do not actually represent retrievable URLs. For example, |
| 126 | // the the URLs in the "about" scheme are aliases to other URLs. This set is |
| 127 | // protected by |lock_|. |
| 128 | SchemeSet pseudo_schemes_; |
| 129 | |
| 130 | // This map holds a SecurityState for each renderer process. The key for the |
| 131 | // map is the ID of the RenderProcessHost. The SecurityState objects are |
| 132 | // owned by this object and are protected by |lock_|. References to them must |
| 133 | // not escape this class. |
| 134 | SecurityStateMap security_state_; |
| 135 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicy); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | }; |
| 138 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 139 | #endif // CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |