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 EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_ 6 #define EXTENSIONS_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 "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" 15 16 using base::UserMetricsAction; 17 18 namespace extensions { 19 20 class WebViewGuest; 21 class WebViewPermissionHelperDelegate; 22 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 virtual ~WebViewPermissionHelper(); 32 typedef base::Callback< 33 void(bool /* allow */, const std::string& /* user_input */)> 34 PermissionResponseCallback; 35 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(); 46 }; 47 48 typedef std::map<int, PermissionResponseInfo> RequestMap; 49 50 int RequestPermission(WebViewPermissionType permission_type, 51 const base::DictionaryValue& request_info, 52 const PermissionResponseCallback& callback, 53 bool allowed_by_default); 54 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(content::RenderViewHost* render_view_host, 67 const GURL& url, 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); 73 74 // Requests Geolocation Permission from the embedder. 75 void RequestGeolocationPermission(int bridge_id, 76 const GURL& requesting_frame, 77 bool user_gesture, 78 const base::Callback<void(bool)>& callback); 79 void CancelGeolocationPermissionRequest(int bridge_id); 80 81 void RequestFileSystemPermission(const GURL& url, 82 bool allowed_by_default, 83 const base::Callback<void(bool)>& callback); 84 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 94 // OnContentBlocked. 95 void FileSystemAccessedAsync(int render_process_id, 96 int render_frame_id, 97 int request_id, 98 const GURL& url, 99 bool blocked_by_policy); 100 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 104 // request will be: 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 111 // OnContentBlocked. 112 void FileSystemAccessedSync(int render_process_id, 113 int render_frame_id, 114 const GURL& url, 115 bool blocked_by_policy, 116 IPC::Message* reply_msg); 117 118 enum PermissionResponseAction { DENY, ALLOW, DEFAULT }; 119 120 enum SetPermissionResult { 121 SET_PERMISSION_INVALID, 122 SET_PERMISSION_ALLOWED, 123 SET_PERMISSION_DENIED 124 }; 125 126 // Responds to the permission request |request_id| with |action| and 127 // |user_input|. Returns whether there was a pending request for the provided 128 // |request_id|. 129 SetPermissionResult SetPermission(int request_id, 130 PermissionResponseAction action, 131 const std::string& user_input); 132 133 void CancelPendingPermissionRequest(int request_id); 134 135 WebViewGuest* web_view_guest() { return web_view_guest_; } 136 137 private: 138 void OnMediaPermissionResponse(const content::MediaStreamRequest& request, 139 const content::MediaResponseCallback& callback, 140 bool allow, 141 const std::string& user_input); 142 143 #if defined(ENABLE_PLUGINS) 144 // content::WebContentsObserver implementation. 145 virtual bool OnMessageReceived( 146 const IPC::Message& message, 147 content::RenderFrameHost* render_frame_host) OVERRIDE; 148 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 149 #endif // defined(ENABLE_PLUGINS) 150 151 // A counter to generate a unique request id for a permission request. 152 // We only need the ids to be unique for a given WebViewGuest. 153 int next_permission_request_id_; 154 155 WebViewPermissionHelper::RequestMap pending_permission_requests_; 156 157 scoped_ptr<WebViewPermissionHelperDelegate> 158 web_view_permission_helper_delegate_; 159 160 WebViewGuest* const web_view_guest_; 161 162 base::WeakPtrFactory<WebViewPermissionHelper> weak_factory_; 163 164 DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper); 165 }; 166 167 } // namespace extensions 168 169 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_ 170