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/guest_view_constants.h"
14 #include "extensions/browser/guest_view/web_view/web_view_permission_types.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 virtual ~WebViewPermissionHelper();
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 void CanDownload(content::RenderViewHost
* render_view_host
,
65 const std::string
& request_method
,
66 const base::Callback
<void(bool)>& callback
);
67 void RequestPointerLockPermission(bool user_gesture
,
68 bool last_unlocked_by_target
,
69 const base::Callback
<void(bool)>& callback
);
71 // Requests Geolocation Permission from the embedder.
72 void RequestGeolocationPermission(int bridge_id
,
73 const GURL
& requesting_frame
,
75 const base::Callback
<void(bool)>& callback
);
76 void CancelGeolocationPermissionRequest(int bridge_id
);
78 void RequestFileSystemPermission(const GURL
& url
,
79 bool allowed_by_default
,
80 const base::Callback
<void(bool)>& callback
);
82 // Called when file system access is requested by the guest content using the
83 // asynchronous HTML5 file system API. The request is plumbed through the
84 // <webview> permission request API. The request will be:
85 // - Allowed if the embedder explicitly allowed it.
86 // - Denied if the embedder explicitly denied.
87 // - Determined by the guest's content settings if the embedder does not
88 // perform an explicit action.
89 // If access was blocked due to the page's content settings,
90 // |blocked_by_policy| should be true, and this function should invoke
92 void FileSystemAccessedAsync(int render_process_id
,
96 bool blocked_by_policy
);
98 // Called when file system access is requested by the guest content using the
99 // synchronous HTML5 file system API in a worker thread or shared worker. The
100 // request is plumbed through the <webview> permission request API. The
102 // - Allowed if the embedder explicitly allowed it.
103 // - Denied if the embedder explicitly denied.
104 // - Determined by the guest's content settings if the embedder does not
105 // perform an explicit action.
106 // If access was blocked due to the page's content settings,
107 // |blocked_by_policy| should be true, and this function should invoke
109 void FileSystemAccessedSync(int render_process_id
,
112 bool blocked_by_policy
,
113 IPC::Message
* reply_msg
);
115 enum PermissionResponseAction
{ DENY
, ALLOW
, DEFAULT
};
117 enum SetPermissionResult
{
118 SET_PERMISSION_INVALID
,
119 SET_PERMISSION_ALLOWED
,
120 SET_PERMISSION_DENIED
123 // Responds to the permission request |request_id| with |action| and
124 // |user_input|. Returns whether there was a pending request for the provided
126 SetPermissionResult
SetPermission(int request_id
,
127 PermissionResponseAction action
,
128 const std::string
& user_input
);
130 void CancelPendingPermissionRequest(int request_id
);
132 WebViewGuest
* web_view_guest() { return web_view_guest_
; }
135 #if defined(ENABLE_PLUGINS)
136 // content::WebContentsObserver implementation.
137 virtual bool OnMessageReceived(
138 const IPC::Message
& message
,
139 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
140 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
141 #endif // defined(ENABLE_PLUGINS)
143 // A counter to generate a unique request id for a permission request.
144 // We only need the ids to be unique for a given WebViewGuest.
145 int next_permission_request_id_
;
147 WebViewPermissionHelper::RequestMap pending_permission_requests_
;
149 scoped_ptr
<WebViewPermissionHelperDelegate
>
150 web_view_permission_helper_delegate_
;
152 WebViewGuest
* const web_view_guest_
;
154 base::WeakPtrFactory
<WebViewPermissionHelper
> weak_factory_
;
156 DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper
);
159 } // namespace extensions
161 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_