| 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_GUEST_H_ | |
| 6 #define CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ | |
| 7 | |
| 8 #include "base/observer_list.h" | |
| 9 #include "chrome/browser/extensions/tab_helper.h" | |
| 10 #include "chrome/browser/guest_view/guest_view.h" | |
| 11 #include "chrome/browser/guest_view/web_view/javascript_dialog_helper.h" | |
| 12 #include "chrome/browser/guest_view/web_view/web_view_find_helper.h" | |
| 13 #include "content/public/browser/javascript_dialog_manager.h" | |
| 14 #include "content/public/browser/notification_registrar.h" | |
| 15 #include "content/public/browser/web_contents_observer.h" | |
| 16 #include "third_party/WebKit/public/web/WebFindOptions.h" | |
| 17 | |
| 18 #if defined(OS_CHROMEOS) | |
| 19 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | |
| 20 #endif | |
| 21 | |
| 22 namespace extensions { | |
| 23 class ScriptExecutor; | |
| 24 class WebviewFindFunction; | |
| 25 } // namespace extensions | |
| 26 | |
| 27 // A WebViewGuest is a WebContentsObserver on the guest WebContents of a | |
| 28 // <webview> tag. It provides the browser-side implementation of the <webview> | |
| 29 // API and manages the lifetime of <webview> extension events. WebViewGuest is | |
| 30 // created on attachment. That is, when a guest WebContents is associated with | |
| 31 // a particular embedder WebContents. This happens on either initial navigation | |
| 32 // or through the use of the New Window API, when a new window is attached to | |
| 33 // a particular <webview>. | |
| 34 class WebViewGuest : public GuestView<WebViewGuest>, | |
| 35 public content::NotificationObserver, | |
| 36 public content::WebContentsObserver { | |
| 37 public: | |
| 38 WebViewGuest(content::WebContents* guest_web_contents, | |
| 39 const std::string& embedder_extension_id); | |
| 40 | |
| 41 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a | |
| 42 // WebViewGuest. | |
| 43 static int GetViewInstanceId(content::WebContents* contents); | |
| 44 static const std::string& Type; | |
| 45 | |
| 46 // GuestViewBase implementation. | |
| 47 virtual void Attach(content::WebContents* embedder_web_contents, | |
| 48 const base::DictionaryValue& args) OVERRIDE; | |
| 49 | |
| 50 // GuestDelegate implementation. | |
| 51 virtual void AddMessageToConsole(int32 level, | |
| 52 const base::string16& message, | |
| 53 int32 line_no, | |
| 54 const base::string16& source_id) OVERRIDE; | |
| 55 virtual void LoadProgressed(double progress) OVERRIDE; | |
| 56 virtual void Close() OVERRIDE; | |
| 57 virtual void DidAttach() OVERRIDE; | |
| 58 virtual void EmbedderDestroyed() OVERRIDE; | |
| 59 virtual void FindReply(int request_id, | |
| 60 int number_of_matches, | |
| 61 const gfx::Rect& selection_rect, | |
| 62 int active_match_ordinal, | |
| 63 bool final_update) OVERRIDE; | |
| 64 virtual void GuestProcessGone(base::TerminationStatus status) OVERRIDE; | |
| 65 virtual bool HandleKeyboardEvent( | |
| 66 const content::NativeWebKeyboardEvent& event) OVERRIDE; | |
| 67 virtual bool IsDragAndDropEnabled() OVERRIDE; | |
| 68 virtual bool IsOverridingUserAgent() const OVERRIDE; | |
| 69 virtual void LoadAbort(bool is_top_level, | |
| 70 const GURL& url, | |
| 71 const std::string& error_type) OVERRIDE; | |
| 72 virtual void RendererResponsive() OVERRIDE; | |
| 73 virtual void RendererUnresponsive() OVERRIDE; | |
| 74 virtual void RequestPermission( | |
| 75 BrowserPluginPermissionType permission_type, | |
| 76 const base::DictionaryValue& request_info, | |
| 77 const PermissionResponseCallback& callback, | |
| 78 bool allowed_by_default) OVERRIDE; | |
| 79 virtual GURL ResolveURL(const std::string& src) OVERRIDE; | |
| 80 virtual void SizeChanged(const gfx::Size& old_size, const gfx::Size& new_size) | |
| 81 OVERRIDE; | |
| 82 virtual void RequestMediaAccessPermission( | |
| 83 const content::MediaStreamRequest& request, | |
| 84 const content::MediaResponseCallback& callback) OVERRIDE; | |
| 85 virtual void CanDownload(const std::string& request_method, | |
| 86 const GURL& url, | |
| 87 const base::Callback<void(bool)>& callback) OVERRIDE; | |
| 88 virtual void RequestPointerLockPermission( | |
| 89 bool user_gesture, | |
| 90 bool last_unlocked_by_target, | |
| 91 const base::Callback<void(bool)>& callback) OVERRIDE; | |
| 92 virtual content::JavaScriptDialogManager* | |
| 93 GetJavaScriptDialogManager() OVERRIDE; | |
| 94 | |
| 95 // NotificationObserver implementation. | |
| 96 virtual void Observe(int type, | |
| 97 const content::NotificationSource& source, | |
| 98 const content::NotificationDetails& details) OVERRIDE; | |
| 99 | |
| 100 // Set the zoom factor. | |
| 101 virtual void SetZoom(double zoom_factor) OVERRIDE; | |
| 102 | |
| 103 // Returns the current zoom factor. | |
| 104 double GetZoom(); | |
| 105 | |
| 106 // Begin or continue a find request. | |
| 107 void Find(const base::string16& search_text, | |
| 108 const blink::WebFindOptions& options, | |
| 109 scoped_refptr<extensions::WebviewFindFunction> find_function); | |
| 110 | |
| 111 // Conclude a find request to clear highlighting. | |
| 112 void StopFinding(content::StopFindAction); | |
| 113 | |
| 114 // If possible, navigate the guest to |relative_index| entries away from the | |
| 115 // current navigation entry. | |
| 116 void Go(int relative_index); | |
| 117 | |
| 118 // Reload the guest. | |
| 119 void Reload(); | |
| 120 | |
| 121 // Requests Geolocation Permission from the embedder. | |
| 122 void RequestGeolocationPermission(int bridge_id, | |
| 123 const GURL& requesting_frame, | |
| 124 bool user_gesture, | |
| 125 const base::Callback<void(bool)>& callback); | |
| 126 | |
| 127 void OnWebViewGeolocationPermissionResponse( | |
| 128 int bridge_id, | |
| 129 bool user_gesture, | |
| 130 const base::Callback<void(bool)>& callback, | |
| 131 bool allow, | |
| 132 const std::string& user_input); | |
| 133 | |
| 134 void CancelGeolocationPermissionRequest(int bridge_id); | |
| 135 | |
| 136 void OnWebViewMediaPermissionResponse( | |
| 137 const content::MediaStreamRequest& request, | |
| 138 const content::MediaResponseCallback& callback, | |
| 139 bool allow, | |
| 140 const std::string& user_input); | |
| 141 | |
| 142 void OnWebViewDownloadPermissionResponse( | |
| 143 const base::Callback<void(bool)>& callback, | |
| 144 bool allow, | |
| 145 const std::string& user_input); | |
| 146 | |
| 147 void OnWebViewPointerLockPermissionResponse( | |
| 148 const base::Callback<void(bool)>& callback, | |
| 149 bool allow, | |
| 150 const std::string& user_input); | |
| 151 | |
| 152 enum PermissionResponseAction { | |
| 153 DENY, | |
| 154 ALLOW, | |
| 155 DEFAULT | |
| 156 }; | |
| 157 | |
| 158 enum SetPermissionResult { | |
| 159 SET_PERMISSION_INVALID, | |
| 160 SET_PERMISSION_ALLOWED, | |
| 161 SET_PERMISSION_DENIED | |
| 162 }; | |
| 163 | |
| 164 // Responds to the permission request |request_id| with |action| and | |
| 165 // |user_input|. Returns whether there was a pending request for the provided | |
| 166 // |request_id|. | |
| 167 SetPermissionResult SetPermission(int request_id, | |
| 168 PermissionResponseAction action, | |
| 169 const std::string& user_input); | |
| 170 | |
| 171 // Overrides the user agent for this guest. | |
| 172 // This affects subsequent guest navigations. | |
| 173 void SetUserAgentOverride(const std::string& user_agent_override); | |
| 174 | |
| 175 // Stop loading the guest. | |
| 176 void Stop(); | |
| 177 | |
| 178 // Kill the guest process. | |
| 179 void Terminate(); | |
| 180 | |
| 181 // Clears data in the storage partition of this guest. | |
| 182 // | |
| 183 // Partition data that are newer than |removal_since| will be removed. | |
| 184 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask. | |
| 185 bool ClearData(const base::Time remove_since, | |
| 186 uint32 removal_mask, | |
| 187 const base::Closure& callback); | |
| 188 | |
| 189 extensions::ScriptExecutor* script_executor() { | |
| 190 return script_executor_.get(); | |
| 191 } | |
| 192 | |
| 193 private: | |
| 194 virtual ~WebViewGuest(); | |
| 195 | |
| 196 // A map to store the callback for a request keyed by the request's id. | |
| 197 struct PermissionResponseInfo { | |
| 198 PermissionResponseCallback callback; | |
| 199 BrowserPluginPermissionType permission_type; | |
| 200 bool allowed_by_default; | |
| 201 PermissionResponseInfo(); | |
| 202 PermissionResponseInfo(const PermissionResponseCallback& callback, | |
| 203 BrowserPluginPermissionType permission_type, | |
| 204 bool allowed_by_default); | |
| 205 ~PermissionResponseInfo(); | |
| 206 }; | |
| 207 | |
| 208 static void RecordUserInitiatedUMA(const PermissionResponseInfo& info, | |
| 209 bool allow); | |
| 210 // WebContentsObserver implementation. | |
| 211 virtual void DidCommitProvisionalLoadForFrame( | |
| 212 int64 frame_id, | |
| 213 const base::string16& frame_unique_name, | |
| 214 bool is_main_frame, | |
| 215 const GURL& url, | |
| 216 content::PageTransition transition_type, | |
| 217 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 218 virtual void DidFailProvisionalLoad( | |
| 219 int64 frame_id, | |
| 220 const base::string16& frame_unique_name, | |
| 221 bool is_main_frame, | |
| 222 const GURL& validated_url, | |
| 223 int error_code, | |
| 224 const base::string16& error_description, | |
| 225 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 226 virtual void DidStartProvisionalLoadForFrame( | |
| 227 int64 frame_id, | |
| 228 int64 parent_frame_id, | |
| 229 bool is_main_frame, | |
| 230 const GURL& validated_url, | |
| 231 bool is_error_page, | |
| 232 bool is_iframe_srcdoc, | |
| 233 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 234 virtual void DocumentLoadedInFrame( | |
| 235 int64 frame_id, | |
| 236 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 237 virtual void DidStopLoading( | |
| 238 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 239 virtual void WebContentsDestroyed( | |
| 240 content::WebContents* web_contents) OVERRIDE; | |
| 241 virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE; | |
| 242 | |
| 243 // Called after the load handler is called in the guest's main frame. | |
| 244 void LoadHandlerCalled(); | |
| 245 | |
| 246 // Called when a redirect notification occurs. | |
| 247 void LoadRedirect(const GURL& old_url, | |
| 248 const GURL& new_url, | |
| 249 bool is_top_level); | |
| 250 | |
| 251 void AddWebViewToExtensionRendererState(); | |
| 252 static void RemoveWebViewFromExtensionRendererState( | |
| 253 content::WebContents* web_contents); | |
| 254 | |
| 255 #if defined(OS_CHROMEOS) | |
| 256 // Notification of a change in the state of an accessibility setting. | |
| 257 void OnAccessibilityStatusChanged( | |
| 258 const chromeos::AccessibilityStatusEventDetails& details); | |
| 259 #endif | |
| 260 | |
| 261 void InjectChromeVoxIfNeeded(content::RenderViewHost* render_view_host); | |
| 262 | |
| 263 // Bridge IDs correspond to a geolocation request. This method will remove | |
| 264 // the bookkeeping for a particular geolocation request associated with the | |
| 265 // provided |bridge_id|. It returns the request ID of the geolocation request. | |
| 266 int RemoveBridgeID(int bridge_id); | |
| 267 | |
| 268 int RequestPermissionInternal( | |
| 269 BrowserPluginPermissionType permission_type, | |
| 270 const base::DictionaryValue& request_info, | |
| 271 const PermissionResponseCallback& callback, | |
| 272 bool allowed_by_default); | |
| 273 | |
| 274 ObserverList<extensions::TabHelper::ScriptExecutionObserver> | |
| 275 script_observers_; | |
| 276 scoped_ptr<extensions::ScriptExecutor> script_executor_; | |
| 277 | |
| 278 content::NotificationRegistrar notification_registrar_; | |
| 279 | |
| 280 // A counter to generate a unique request id for a permission request. | |
| 281 // We only need the ids to be unique for a given WebViewGuest. | |
| 282 int next_permission_request_id_; | |
| 283 | |
| 284 typedef std::map<int, PermissionResponseInfo> RequestMap; | |
| 285 RequestMap pending_permission_requests_; | |
| 286 | |
| 287 // True if the user agent is overridden. | |
| 288 bool is_overriding_user_agent_; | |
| 289 | |
| 290 // Indicates that the page needs to be reloaded once it has been attached to | |
| 291 // an embedder. | |
| 292 bool pending_reload_on_attachment_; | |
| 293 | |
| 294 // Main frame ID of last committed page. | |
| 295 int64 main_frame_id_; | |
| 296 | |
| 297 // Set to |true| if ChromeVox was already injected in main frame. | |
| 298 bool chromevox_injected_; | |
| 299 | |
| 300 // Stores the current zoom factor. | |
| 301 double current_zoom_factor_; | |
| 302 | |
| 303 // Handles find requests and replies for the webview find API. | |
| 304 WebviewFindHelper find_helper_; | |
| 305 | |
| 306 // Handles the JavaScript dialog requests. | |
| 307 JavaScriptDialogHelper javascript_dialog_helper_; | |
| 308 | |
| 309 friend void WebviewFindHelper::DispatchFindUpdateEvent(bool canceled, | |
| 310 bool final_update); | |
| 311 | |
| 312 #if defined(OS_CHROMEOS) | |
| 313 // Subscription to receive notifications on changes to a11y settings. | |
| 314 scoped_ptr<chromeos::AccessibilityStatusSubscription> | |
| 315 accessibility_subscription_; | |
| 316 #endif | |
| 317 | |
| 318 std::map<int, int> bridge_id_to_request_id_map_; | |
| 319 | |
| 320 DISALLOW_COPY_AND_ASSIGN(WebViewGuest); | |
| 321 }; | |
| 322 | |
| 323 #endif // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ | |
| OLD | NEW |