Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_permission_helper.h
blobfd730fb9c0f282ce846a44acceaf860d3c85f3b6
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 "components/guest_view/common/guest_view_constants.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/common/media_stream_request.h"
14 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
16 using base::UserMetricsAction;
18 namespace extensions {
20 class WebViewGuest;
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 {
29 public:
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,
58 int render_frame_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(const GURL& url,
67 const std::string& request_method,
68 const base::Callback<void(bool)>& callback);
69 void RequestPointerLockPermission(bool user_gesture,
70 bool last_unlocked_by_target,
71 const base::Callback<void(bool)>& callback);
73 // Requests Geolocation Permission from the embedder.
74 void RequestGeolocationPermission(int bridge_id,
75 const GURL& requesting_frame,
76 bool user_gesture,
77 const base::Callback<void(bool)>& callback);
78 void CancelGeolocationPermissionRequest(int bridge_id);
80 void RequestFileSystemPermission(const GURL& url,
81 bool allowed_by_default,
82 const base::Callback<void(bool)>& callback);
84 // Called when file system access is requested by the guest content using the
85 // asynchronous HTML5 file system API. The request is plumbed through the
86 // <webview> permission request API. The request will be:
87 // - Allowed if the embedder explicitly allowed it.
88 // - Denied if the embedder explicitly denied.
89 // - Determined by the guest's content settings if the embedder does not
90 // perform an explicit action.
91 // If access was blocked due to the page's content settings,
92 // |blocked_by_policy| should be true, and this function should invoke
93 // OnContentBlocked.
94 void FileSystemAccessedAsync(int render_process_id,
95 int render_frame_id,
96 int request_id,
97 const GURL& url,
98 bool blocked_by_policy);
100 // Called when file system access is requested by the guest content using the
101 // synchronous HTML5 file system API in a worker thread or shared worker. The
102 // request is plumbed through the <webview> permission request API. The
103 // request will be:
104 // - Allowed if the embedder explicitly allowed it.
105 // - Denied if the embedder explicitly denied.
106 // - Determined by the guest's content settings if the embedder does not
107 // perform an explicit action.
108 // If access was blocked due to the page's content settings,
109 // |blocked_by_policy| should be true, and this function should invoke
110 // OnContentBlocked.
111 void FileSystemAccessedSync(int render_process_id,
112 int render_frame_id,
113 const GURL& url,
114 bool blocked_by_policy,
115 IPC::Message* reply_msg);
117 enum PermissionResponseAction { DENY, ALLOW, DEFAULT };
119 enum SetPermissionResult {
120 SET_PERMISSION_INVALID,
121 SET_PERMISSION_ALLOWED,
122 SET_PERMISSION_DENIED
125 // Responds to the permission request |request_id| with |action| and
126 // |user_input|. Returns whether there was a pending request for the provided
127 // |request_id|.
128 SetPermissionResult SetPermission(int request_id,
129 PermissionResponseAction action,
130 const std::string& user_input);
132 void CancelPendingPermissionRequest(int request_id);
134 WebViewGuest* web_view_guest() { return web_view_guest_; }
136 private:
137 void OnMediaPermissionResponse(const content::MediaStreamRequest& request,
138 const content::MediaResponseCallback& callback,
139 bool allow,
140 const std::string& user_input);
142 #if defined(ENABLE_PLUGINS)
143 // content::WebContentsObserver implementation.
144 bool OnMessageReceived(const IPC::Message& message,
145 content::RenderFrameHost* render_frame_host) override;
146 bool OnMessageReceived(const IPC::Message& message) override;
147 #endif // defined(ENABLE_PLUGINS)
149 // A counter to generate a unique request id for a permission request.
150 // We only need the ids to be unique for a given WebViewGuest.
151 int next_permission_request_id_;
153 WebViewPermissionHelper::RequestMap pending_permission_requests_;
155 scoped_ptr<WebViewPermissionHelperDelegate>
156 web_view_permission_helper_delegate_;
158 WebViewGuest* const web_view_guest_;
160 base::WeakPtrFactory<WebViewPermissionHelper> weak_factory_;
162 DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper);
165 } // namespace extensions
167 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_