1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/metrics/user_metrics_action.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/common/media_stream_request.h"
13 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
14 #include "extensions/common/guest_view/guest_view_constants.h"
16 using base::UserMetricsAction
;
18 namespace extensions
{
21 class WebViewPermissionHelperDelegate
;
23 // WebViewPermissionHelper manages <webview> permission requests. This helper
24 // class is owned by WebViewGuest. Its purpose is to request permission for
25 // various operations from the <webview> embedder, and reply back via callbacks
26 // to the callers on a response from the embedder.
27 class WebViewPermissionHelper
28 : public content::WebContentsObserver
{
30 explicit WebViewPermissionHelper(WebViewGuest
* guest
);
31 ~WebViewPermissionHelper() override
;
32 typedef base::Callback
<
33 void(bool /* allow */, const std::string
& /* user_input */)>
34 PermissionResponseCallback
;
36 // A map to store the callback for a request keyed by the request's id.
37 struct PermissionResponseInfo
{
38 PermissionResponseCallback callback
;
39 WebViewPermissionType permission_type
;
40 bool allowed_by_default
;
41 PermissionResponseInfo();
42 PermissionResponseInfo(const PermissionResponseCallback
& callback
,
43 WebViewPermissionType permission_type
,
44 bool allowed_by_default
);
45 ~PermissionResponseInfo();
48 typedef std::map
<int, PermissionResponseInfo
> RequestMap
;
50 int RequestPermission(WebViewPermissionType permission_type
,
51 const base::DictionaryValue
& request_info
,
52 const PermissionResponseCallback
& callback
,
53 bool allowed_by_default
);
55 static WebViewPermissionHelper
* FromWebContents(
56 content::WebContents
* web_contents
);
57 static WebViewPermissionHelper
* FromFrameID(int render_process_id
,
59 void RequestMediaAccessPermission(
60 content::WebContents
* source
,
61 const content::MediaStreamRequest
& request
,
62 const content::MediaResponseCallback
& callback
);
63 bool CheckMediaAccessPermission(content::WebContents
* source
,
64 const GURL
& security_origin
,
65 content::MediaStreamType type
);
66 void CanDownload(content::RenderViewHost
* render_view_host
,
68 const std::string
& request_method
,
69 const base::Callback
<void(bool)>& callback
);
70 void RequestPointerLockPermission(bool user_gesture
,
71 bool last_unlocked_by_target
,
72 const base::Callback
<void(bool)>& callback
);
74 // Requests Geolocation Permission from the embedder.
75 void RequestGeolocationPermission(int bridge_id
,
76 const GURL
& requesting_frame
,
78 const base::Callback
<void(bool)>& callback
);
79 void CancelGeolocationPermissionRequest(int bridge_id
);
81 void RequestFileSystemPermission(const GURL
& url
,
82 bool allowed_by_default
,
83 const base::Callback
<void(bool)>& callback
);
85 // Called when file system access is requested by the guest content using the
86 // asynchronous HTML5 file system API. The request is plumbed through the
87 // <webview> permission request API. The request will be:
88 // - Allowed if the embedder explicitly allowed it.
89 // - Denied if the embedder explicitly denied.
90 // - Determined by the guest's content settings if the embedder does not
91 // perform an explicit action.
92 // If access was blocked due to the page's content settings,
93 // |blocked_by_policy| should be true, and this function should invoke
95 void FileSystemAccessedAsync(int render_process_id
,
99 bool blocked_by_policy
);
101 // Called when file system access is requested by the guest content using the
102 // synchronous HTML5 file system API in a worker thread or shared worker. The
103 // request is plumbed through the <webview> permission request API. The
105 // - Allowed if the embedder explicitly allowed it.
106 // - Denied if the embedder explicitly denied.
107 // - Determined by the guest's content settings if the embedder does not
108 // perform an explicit action.
109 // If access was blocked due to the page's content settings,
110 // |blocked_by_policy| should be true, and this function should invoke
112 void FileSystemAccessedSync(int render_process_id
,
115 bool blocked_by_policy
,
116 IPC::Message
* reply_msg
);
118 enum PermissionResponseAction
{ DENY
, ALLOW
, DEFAULT
};
120 enum SetPermissionResult
{
121 SET_PERMISSION_INVALID
,
122 SET_PERMISSION_ALLOWED
,
123 SET_PERMISSION_DENIED
126 // Responds to the permission request |request_id| with |action| and
127 // |user_input|. Returns whether there was a pending request for the provided
129 SetPermissionResult
SetPermission(int request_id
,
130 PermissionResponseAction action
,
131 const std::string
& user_input
);
133 void CancelPendingPermissionRequest(int request_id
);
135 WebViewGuest
* web_view_guest() { return web_view_guest_
; }
138 void OnMediaPermissionResponse(const content::MediaStreamRequest
& request
,
139 const content::MediaResponseCallback
& callback
,
141 const std::string
& user_input
);
143 #if defined(ENABLE_PLUGINS)
144 // content::WebContentsObserver implementation.
145 bool OnMessageReceived(const IPC::Message
& message
,
146 content::RenderFrameHost
* render_frame_host
) override
;
147 bool OnMessageReceived(const IPC::Message
& message
) override
;
148 #endif // defined(ENABLE_PLUGINS)
150 // A counter to generate a unique request id for a permission request.
151 // We only need the ids to be unique for a given WebViewGuest.
152 int next_permission_request_id_
;
154 WebViewPermissionHelper::RequestMap pending_permission_requests_
;
156 scoped_ptr
<WebViewPermissionHelperDelegate
>
157 web_view_permission_helper_delegate_
;
159 WebViewGuest
* const web_view_guest_
;
161 base::WeakPtrFactory
<WebViewPermissionHelper
> weak_factory_
;
163 DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper
);
166 } // namespace extensions
168 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_