blob: 22db4225b9649f27537dc83dcf9d9bf061967f15 [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"
[email protected]72cbd322009-04-07 10:17:1213#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/lock.h"
15#include "base/singleton.h"
[email protected]79dc62e32009-05-19 21:02:5816#include "testing/gtest/include/gtest/gtest_prod.h"
[email protected]46072d42008-07-28 14:49:3517
[email protected]561abe62009-04-06 18:08:3418class FilePath;
[email protected]46072d42008-07-28 14:49:3519class GURL;
initial.commit09911bf2008-07-26 23:55:2920
[email protected]f58ddcf2009-05-18 22:22:0621// The ChildProcessSecurityPolicy class is used to grant and revoke security
initial.commit09911bf2008-07-26 23:55:2922// 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]f58ddcf2009-05-18 22:22:0626// ChildProcessSecurityPolicy is a singleton that may be used on any thread.
initial.commit09911bf2008-07-26 23:55:2927//
[email protected]f58ddcf2009-05-18 22:22:0628class ChildProcessSecurityPolicy {
initial.commit09911bf2008-07-26 23:55:2929 public:
[email protected]c11ffb462009-05-15 18:03:4030 // Object can only be created through GetInstance() so the constructor is
31 // private.
[email protected]f58ddcf2009-05-18 22:22:0632 ~ChildProcessSecurityPolicy();
[email protected]c11ffb462009-05-15 18:03:4033
[email protected]f58ddcf2009-05-18 22:22:0634 // There is one global ChildProcessSecurityPolicy object for the entire browser
initial.commit09911bf2008-07-26 23:55:2935 // processes. The object returned by this method may be accessed on any
36 // thread.
[email protected]f58ddcf2009-05-18 22:22:0637 static ChildProcessSecurityPolicy* GetInstance();
initial.commit09911bf2008-07-26 23:55:2938
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]561abe62009-04-06 18:08:3471 void GrantUploadFile(int renderer_id, const FilePath& file);
initial.commit09911bf2008-07-26 23:55:2972
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]1adff062009-06-02 18:39:5581 // Grant this renderer the ability to use extension Bindings.
82 void GrantExtensionBindings(int renderer_id);
83
initial.commit09911bf2008-07-26 23:55:2984 // 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]561abe62009-04-06 18:08:3492 bool CanUploadFile(int renderer_id, const FilePath& file);
initial.commit09911bf2008-07-26 23:55:2993
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]1adff062009-06-02 18:39:5599 // 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.commit09911bf2008-07-26 23:55:29104 private:
[email protected]79dc62e32009-05-19 21:02:58105 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
106 FRIEND_TEST(ChildProcessSecurityPolicyInProcessBrowserTest, NoLeak);
107
initial.commit09911bf2008-07-26 23:55:29108 class SecurityState;
109
110 typedef std::set<std::string> SchemeSet;
111 typedef std::map<int, SecurityState*> SecurityStateMap;
112
[email protected]f58ddcf2009-05-18 22:22:06113 // Obtain an instance of ChildProcessSecurityPolicy via GetInstance().
114 ChildProcessSecurityPolicy();
115 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicy>;
initial.commit09911bf2008-07-26 23:55:29116
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]f58ddcf2009-05-18 22:22:06136 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicy);
initial.commit09911bf2008-07-26 23:55:29137};
138
[email protected]f58ddcf2009-05-18 22:22:06139#endif // CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_