OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_ |
| 6 #define CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/metrics/user_metrics_action.h" |
| 10 #include "chrome/browser/guest_view/guest_view_constants.h" |
| 11 #include "chrome/browser/guest_view/web_view/web_view_permission_types.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/common/media_stream_request.h" |
| 15 |
| 16 using base::UserMetricsAction; |
| 17 |
| 18 class WebViewGuest; |
| 19 |
| 20 // WebViewPermissionHelper manages <webview> permission requests. This helper |
| 21 // class is owned by WebViewGuest. Its purpose is to request permission for |
| 22 // various operations from the <webview> embedder, and reply back via callbacks |
| 23 // to the callers on a response from the embedder. |
| 24 class WebViewPermissionHelper |
| 25 : public content::WebContentsObserver { |
| 26 public: |
| 27 explicit WebViewPermissionHelper(WebViewGuest* guest); |
| 28 virtual ~WebViewPermissionHelper(); |
| 29 typedef base::Callback< |
| 30 void(bool /* allow */, const std::string& /* user_input */)> |
| 31 PermissionResponseCallback; |
| 32 |
| 33 // A map to store the callback for a request keyed by the request's id. |
| 34 struct PermissionResponseInfo { |
| 35 PermissionResponseCallback callback; |
| 36 WebViewPermissionType permission_type; |
| 37 bool allowed_by_default; |
| 38 PermissionResponseInfo(); |
| 39 PermissionResponseInfo(const PermissionResponseCallback& callback, |
| 40 WebViewPermissionType permission_type, |
| 41 bool allowed_by_default); |
| 42 ~PermissionResponseInfo(); |
| 43 }; |
| 44 |
| 45 typedef std::map<int, PermissionResponseInfo> RequestMap; |
| 46 |
| 47 int RequestPermission(WebViewPermissionType permission_type, |
| 48 const base::DictionaryValue& request_info, |
| 49 const PermissionResponseCallback& callback, |
| 50 bool allowed_by_default); |
| 51 |
| 52 static WebViewPermissionHelper* FromWebContents( |
| 53 content::WebContents* web_contents); |
| 54 static WebViewPermissionHelper* FromFrameID(int render_process_id, |
| 55 int render_frame_id); |
| 56 void RequestMediaAccessPermission( |
| 57 content::WebContents* source, |
| 58 const content::MediaStreamRequest& request, |
| 59 const content::MediaResponseCallback& callback); |
| 60 void CanDownload(content::RenderViewHost* render_view_host, |
| 61 const GURL& url, |
| 62 const std::string& request_method, |
| 63 const base::Callback<void(bool)>& callback); |
| 64 void RequestPointerLockPermission(bool user_gesture, |
| 65 bool last_unlocked_by_target, |
| 66 const base::Callback<void(bool)>& callback); |
| 67 |
| 68 // Requests Geolocation Permission from the embedder. |
| 69 void RequestGeolocationPermission(int bridge_id, |
| 70 const GURL& requesting_frame, |
| 71 bool user_gesture, |
| 72 const base::Callback<void(bool)>& callback); |
| 73 void CancelGeolocationPermissionRequest(int bridge_id); |
| 74 |
| 75 void RequestFileSystemPermission(const GURL& url, |
| 76 bool allowed_by_default, |
| 77 const base::Callback<void(bool)>& callback); |
| 78 |
| 79 // Called when file system access is requested by the guest content using the |
| 80 // asynchronous HTML5 file system API. The request is plumbed through the |
| 81 // <webview> permission request API. The request will be: |
| 82 // - Allowed if the embedder explicitly allowed it. |
| 83 // - Denied if the embedder explicitly denied. |
| 84 // - Determined by the guest's content settings if the embedder does not |
| 85 // perform an explicit action. |
| 86 // If access was blocked due to the page's content settings, |
| 87 // |blocked_by_policy| should be true, and this function should invoke |
| 88 // OnContentBlocked. |
| 89 void FileSystemAccessedAsync(int render_process_id, |
| 90 int render_frame_id, |
| 91 int request_id, |
| 92 const GURL& url, |
| 93 bool blocked_by_policy); |
| 94 |
| 95 // Called when file system access is requested by the guest content using the |
| 96 // synchronous HTML5 file system API in a worker thread or shared worker. The |
| 97 // request is plumbed through the <webview> permission request API. The |
| 98 // request will be: |
| 99 // - Allowed if the embedder explicitly allowed it. |
| 100 // - Denied if the embedder explicitly denied. |
| 101 // - Determined by the guest's content settings if the embedder does not |
| 102 // perform an explicit action. |
| 103 // If access was blocked due to the page's content settings, |
| 104 // |blocked_by_policy| should be true, and this function should invoke |
| 105 // OnContentBlocked. |
| 106 void FileSystemAccessedSync(int render_process_id, |
| 107 int render_frame_id, |
| 108 const GURL& url, |
| 109 bool blocked_by_policy, |
| 110 IPC::Message* reply_msg); |
| 111 |
| 112 enum PermissionResponseAction { DENY, ALLOW, DEFAULT }; |
| 113 |
| 114 enum SetPermissionResult { |
| 115 SET_PERMISSION_INVALID, |
| 116 SET_PERMISSION_ALLOWED, |
| 117 SET_PERMISSION_DENIED |
| 118 }; |
| 119 |
| 120 // Responds to the permission request |request_id| with |action| and |
| 121 // |user_input|. Returns whether there was a pending request for the provided |
| 122 // |request_id|. |
| 123 SetPermissionResult SetPermission(int request_id, |
| 124 PermissionResponseAction action, |
| 125 const std::string& user_input); |
| 126 |
| 127 private: |
| 128 #if defined(ENABLE_PLUGINS) |
| 129 // content::WebContentsObserver implementation. |
| 130 virtual bool OnMessageReceived( |
| 131 const IPC::Message& message, |
| 132 content::RenderFrameHost* render_frame_host) OVERRIDE; |
| 133 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 134 |
| 135 // Message handlers: |
| 136 void OnBlockedUnauthorizedPlugin(const base::string16& name, |
| 137 const std::string& identifier); |
| 138 void OnCouldNotLoadPlugin(const base::FilePath& plugin_path); |
| 139 void OnBlockedOutdatedPlugin(int placeholder_id, |
| 140 const std::string& identifier); |
| 141 void OnNPAPINotSupported(const std::string& identifier); |
| 142 void OnOpenAboutPlugins(); |
| 143 #if defined(ENABLE_PLUGIN_INSTALLATION) |
| 144 void OnFindMissingPlugin(int placeholder_id, const std::string& mime_type); |
| 145 |
| 146 void OnRemovePluginPlaceholderHost(int placeholder_id); |
| 147 #endif // defined(ENABLE_PLUGIN_INSTALLATION) |
| 148 |
| 149 void OnPermissionResponse(const std::string& identifier, |
| 150 bool allow, |
| 151 const std::string& user_input); |
| 152 #endif // defiend(ENABLE_PLUGINS) |
| 153 |
| 154 void OnGeolocationPermissionResponse( |
| 155 int bridge_id, |
| 156 bool user_gesture, |
| 157 const base::Callback<void(bool)>& callback, |
| 158 bool allow, |
| 159 const std::string& user_input); |
| 160 |
| 161 void OnFileSystemPermissionResponse( |
| 162 const base::Callback<void(bool)>& callback, |
| 163 bool allow, |
| 164 const std::string& user_input); |
| 165 |
| 166 void OnMediaPermissionResponse( |
| 167 const content::MediaStreamRequest& request, |
| 168 const content::MediaResponseCallback& callback, |
| 169 bool allow, |
| 170 const std::string& user_input); |
| 171 |
| 172 void OnDownloadPermissionResponse( |
| 173 const base::Callback<void(bool)>& callback, |
| 174 bool allow, |
| 175 const std::string& user_input); |
| 176 |
| 177 void OnPointerLockPermissionResponse( |
| 178 const base::Callback<void(bool)>& callback, |
| 179 bool allow, |
| 180 const std::string& user_input); |
| 181 |
| 182 // Bridge IDs correspond to a geolocation request. This method will remove |
| 183 // the bookkeeping for a particular geolocation request associated with the |
| 184 // provided |bridge_id|. It returns the request ID of the geolocation request. |
| 185 int RemoveBridgeID(int bridge_id); |
| 186 |
| 187 void FileSystemAccessedAsyncResponse(int render_process_id, |
| 188 int render_frame_id, |
| 189 int request_id, |
| 190 const GURL& url, |
| 191 bool allowed); |
| 192 |
| 193 void FileSystemAccessedSyncResponse(int render_process_id, |
| 194 int render_frame_id, |
| 195 const GURL& url, |
| 196 IPC::Message* reply_msg, |
| 197 bool allowed); |
| 198 |
| 199 // A counter to generate a unique request id for a permission request. |
| 200 // We only need the ids to be unique for a given WebViewGuest. |
| 201 int next_permission_request_id_; |
| 202 |
| 203 WebViewPermissionHelper::RequestMap pending_permission_requests_; |
| 204 |
| 205 std::map<int, int> bridge_id_to_request_id_map_; |
| 206 |
| 207 WebViewGuest* web_view_guest_; |
| 208 |
| 209 base::WeakPtrFactory<WebViewPermissionHelper> weak_factory_; |
| 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper); |
| 212 }; |
| 213 |
| 214 #endif // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_ |
OLD | NEW |