blob: ae1eb412b5ab5c8596bb2f178d81504ae4161a29 [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
81 // Before servicing a renderer's request for a URL, the browser should call
82 // this method to determine whether the renderer has the capability to
83 // request the URL.
84 bool CanRequestURL(int renderer_id, const GURL& url);
85
86 // Before servicing a renderer's request to upload a file to the web, the
87 // browser should call this method to determine whether the renderer has the
88 // capability to upload the requested file.
[email protected]561abe62009-04-06 18:08:3489 bool CanUploadFile(int renderer_id, const FilePath& file);
initial.commit09911bf2008-07-26 23:55:2990
91 // Returns true of the specified renderer_id has been granted DOMUIBindings.
92 // The browser should check this property before assuming the renderer is
93 // allowed to use DOMUIBindings.
94 bool HasDOMUIBindings(int renderer_id);
95
96 private:
[email protected]79dc62e32009-05-19 21:02:5897 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
98 FRIEND_TEST(ChildProcessSecurityPolicyInProcessBrowserTest, NoLeak);
99
initial.commit09911bf2008-07-26 23:55:29100 class SecurityState;
101
102 typedef std::set<std::string> SchemeSet;
103 typedef std::map<int, SecurityState*> SecurityStateMap;
104
[email protected]f58ddcf2009-05-18 22:22:06105 // Obtain an instance of ChildProcessSecurityPolicy via GetInstance().
106 ChildProcessSecurityPolicy();
107 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicy>;
initial.commit09911bf2008-07-26 23:55:29108
109 // You must acquire this lock before reading or writing any members of this
110 // class. You must not block while holding this lock.
111 Lock lock_;
112
113 // These schemes are white-listed for all renderers. This set is protected
114 // by |lock_|.
115 SchemeSet web_safe_schemes_;
116
117 // These schemes do not actually represent retrievable URLs. For example,
118 // the the URLs in the "about" scheme are aliases to other URLs. This set is
119 // protected by |lock_|.
120 SchemeSet pseudo_schemes_;
121
122 // This map holds a SecurityState for each renderer process. The key for the
123 // map is the ID of the RenderProcessHost. The SecurityState objects are
124 // owned by this object and are protected by |lock_|. References to them must
125 // not escape this class.
126 SecurityStateMap security_state_;
127
[email protected]f58ddcf2009-05-18 22:22:06128 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicy);
initial.commit09911bf2008-07-26 23:55:29129};
130
[email protected]f58ddcf2009-05-18 22:22:06131#endif // CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_