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 <vector> |
| 9 |
| 10 #include "base/observer_list.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 "chrome/browser/guest_view/web_view/web_view_permission_helper.h" |
| 14 #include "chrome/browser/guest_view/web_view/web_view_permission_types.h" |
| 15 #include "content/public/browser/javascript_dialog_manager.h" |
| 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "extensions/browser/guest_view/guest_view.h" |
| 19 #include "extensions/browser/guest_view/web_view/web_view_guest_delegate.h" |
| 20 #include "extensions/browser/script_executor.h" |
| 21 #include "third_party/WebKit/public/web/WebFindOptions.h" |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 class WebViewInternalFindFunction; |
| 26 |
| 27 // A WebViewGuest provides the browser-side implementation of the <webview> API |
| 28 // and manages the dispatch of <webview> extension events. WebViewGuest is |
| 29 // created on attachment. That is, when a guest WebContents is associated with |
| 30 // a particular embedder WebContents. This happens on either initial navigation |
| 31 // or through the use of the New Window API, when a new window is attached to |
| 32 // a particular <webview>. |
| 33 class WebViewGuest : public GuestView<WebViewGuest>, |
| 34 public content::NotificationObserver { |
| 35 public: |
| 36 static GuestViewBase* Create(content::BrowserContext* browser_context, |
| 37 int guest_instance_id); |
| 38 |
| 39 // For WebViewGuest, we create special guest processes, which host the |
| 40 // tag content separately from the main application that embeds the tag. |
| 41 // A <webview> can specify both the partition name and whether the storage |
| 42 // for that partition should be persisted. Each tag gets a SiteInstance with |
| 43 // a specially formatted URL, based on the application it is hosted by and |
| 44 // the partition requested by it. The format for that URL is: |
| 45 // chrome-guest://partition_domain/persist?partition_name |
| 46 static bool GetGuestPartitionConfigForSite(const GURL& site, |
| 47 std::string* partition_domain, |
| 48 std::string* partition_name, |
| 49 bool* in_memory); |
| 50 |
| 51 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a |
| 52 // WebViewGuest. |
| 53 static int GetViewInstanceId(content::WebContents* contents); |
| 54 |
| 55 static const char Type[]; |
| 56 |
| 57 // Request navigating the guest to the provided |src| URL. |
| 58 void NavigateGuest(const std::string& src); |
| 59 |
| 60 // Shows the context menu for the guest. |
| 61 // |items| acts as a filter. This restricts the current context's default |
| 62 // menu items to contain only the items from |items|. |
| 63 // |items| == NULL means no filtering will be applied. |
| 64 void ShowContextMenu( |
| 65 int request_id, |
| 66 const WebViewGuestDelegate::MenuItemVector* items); |
| 67 |
| 68 // Sets the frame name of the guest. |
| 69 void SetName(const std::string& name); |
| 70 |
| 71 // Set the zoom factor. |
| 72 void SetZoom(double zoom_factor); |
| 73 |
| 74 // GuestViewBase implementation. |
| 75 virtual const char* GetAPINamespace() OVERRIDE; |
| 76 virtual void CreateWebContents( |
| 77 const std::string& embedder_extension_id, |
| 78 int embedder_render_process_id, |
| 79 const base::DictionaryValue& create_params, |
| 80 const WebContentsCreatedCallback& callback) OVERRIDE; |
| 81 virtual void DidAttachToEmbedder() OVERRIDE; |
| 82 virtual void DidInitialize() OVERRIDE; |
| 83 virtual void DidStopLoading() OVERRIDE; |
| 84 virtual void EmbedderDestroyed() OVERRIDE; |
| 85 virtual void GuestDestroyed() OVERRIDE; |
| 86 virtual void GuestReady() OVERRIDE; |
| 87 virtual void GuestSizeChangedDueToAutoSize( |
| 88 const gfx::Size& old_size, |
| 89 const gfx::Size& new_size) OVERRIDE; |
| 90 virtual bool IsAutoSizeSupported() const OVERRIDE; |
| 91 virtual bool IsDragAndDropEnabled() const OVERRIDE; |
| 92 virtual void WillAttachToEmbedder() OVERRIDE; |
| 93 virtual void WillDestroy() OVERRIDE; |
| 94 |
| 95 // WebContentsDelegate implementation. |
| 96 virtual bool AddMessageToConsole(content::WebContents* source, |
| 97 int32 level, |
| 98 const base::string16& message, |
| 99 int32 line_no, |
| 100 const base::string16& source_id) OVERRIDE; |
| 101 virtual void LoadProgressChanged(content::WebContents* source, |
| 102 double progress) OVERRIDE; |
| 103 virtual void CloseContents(content::WebContents* source) OVERRIDE; |
| 104 virtual void FindReply(content::WebContents* source, |
| 105 int request_id, |
| 106 int number_of_matches, |
| 107 const gfx::Rect& selection_rect, |
| 108 int active_match_ordinal, |
| 109 bool final_update) OVERRIDE; |
| 110 virtual bool HandleContextMenu( |
| 111 const content::ContextMenuParams& params) OVERRIDE; |
| 112 virtual void HandleKeyboardEvent( |
| 113 content::WebContents* source, |
| 114 const content::NativeWebKeyboardEvent& event) OVERRIDE; |
| 115 virtual void RendererResponsive(content::WebContents* source) OVERRIDE; |
| 116 virtual void RendererUnresponsive(content::WebContents* source) OVERRIDE; |
| 117 virtual void RequestMediaAccessPermission( |
| 118 content::WebContents* source, |
| 119 const content::MediaStreamRequest& request, |
| 120 const content::MediaResponseCallback& callback) OVERRIDE; |
| 121 virtual void CanDownload(content::RenderViewHost* render_view_host, |
| 122 const GURL& url, |
| 123 const std::string& request_method, |
| 124 const base::Callback<void(bool)>& callback) OVERRIDE; |
| 125 virtual content::JavaScriptDialogManager* |
| 126 GetJavaScriptDialogManager() OVERRIDE; |
| 127 virtual content::ColorChooser* OpenColorChooser( |
| 128 content::WebContents* web_contents, |
| 129 SkColor color, |
| 130 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE; |
| 131 virtual void RunFileChooser( |
| 132 content::WebContents* web_contents, |
| 133 const content::FileChooserParams& params) OVERRIDE; |
| 134 virtual void AddNewContents(content::WebContents* source, |
| 135 content::WebContents* new_contents, |
| 136 WindowOpenDisposition disposition, |
| 137 const gfx::Rect& initial_pos, |
| 138 bool user_gesture, |
| 139 bool* was_blocked) OVERRIDE; |
| 140 virtual content::WebContents* OpenURLFromTab( |
| 141 content::WebContents* source, |
| 142 const content::OpenURLParams& params) OVERRIDE; |
| 143 virtual void WebContentsCreated(content::WebContents* source_contents, |
| 144 int opener_render_frame_id, |
| 145 const base::string16& frame_name, |
| 146 const GURL& target_url, |
| 147 content::WebContents* new_contents) OVERRIDE; |
| 148 |
| 149 // BrowserPluginGuestDelegate implementation. |
| 150 virtual content::WebContents* CreateNewGuestWindow( |
| 151 const content::WebContents::CreateParams& create_params) OVERRIDE; |
| 152 virtual void RequestPointerLockPermission( |
| 153 bool user_gesture, |
| 154 bool last_unlocked_by_target, |
| 155 const base::Callback<void(bool)>& callback) OVERRIDE; |
| 156 // NotificationObserver implementation. |
| 157 virtual void Observe(int type, |
| 158 const content::NotificationSource& source, |
| 159 const content::NotificationDetails& details) OVERRIDE; |
| 160 |
| 161 // Returns the current zoom factor. |
| 162 double GetZoom(); |
| 163 |
| 164 // Begin or continue a find request. |
| 165 void Find( |
| 166 const base::string16& search_text, |
| 167 const blink::WebFindOptions& options, |
| 168 scoped_refptr<WebViewInternalFindFunction> find_function); |
| 169 |
| 170 // Conclude a find request to clear highlighting. |
| 171 void StopFinding(content::StopFindAction); |
| 172 |
| 173 // If possible, navigate the guest to |relative_index| entries away from the |
| 174 // current navigation entry. |
| 175 void Go(int relative_index); |
| 176 |
| 177 // Reload the guest. |
| 178 void Reload(); |
| 179 |
| 180 typedef base::Callback<void(bool /* allow */, |
| 181 const std::string& /* user_input */)> |
| 182 PermissionResponseCallback; |
| 183 int RequestPermission( |
| 184 WebViewPermissionType permission_type, |
| 185 const base::DictionaryValue& request_info, |
| 186 const PermissionResponseCallback& callback, |
| 187 bool allowed_by_default); |
| 188 |
| 189 // Requests Geolocation Permission from the embedder. |
| 190 void RequestGeolocationPermission(int bridge_id, |
| 191 const GURL& requesting_frame, |
| 192 bool user_gesture, |
| 193 const base::Callback<void(bool)>& callback); |
| 194 void CancelGeolocationPermissionRequest(int bridge_id); |
| 195 |
| 196 // Called when file system access is requested by the guest content using the |
| 197 // HTML5 file system API in main thread, or a worker thread. |
| 198 // The request is plumbed through the <webview> permission request API. The |
| 199 // request will be: |
| 200 // - Allowed if the embedder explicitly allowed it. |
| 201 // - Denied if the embedder explicitly denied. |
| 202 // - Determined by the guest's content settings if the embedder does not |
| 203 // perform an explicit action. |
| 204 void RequestFileSystemPermission(const GURL& url, |
| 205 bool allowed_by_default, |
| 206 const base::Callback<void(bool)>& callback); |
| 207 |
| 208 // Overrides the user agent for this guest. |
| 209 // This affects subsequent guest navigations. |
| 210 void SetUserAgentOverride(const std::string& user_agent_override); |
| 211 |
| 212 // Stop loading the guest. |
| 213 void Stop(); |
| 214 |
| 215 // Kill the guest process. |
| 216 void Terminate(); |
| 217 |
| 218 // Clears data in the storage partition of this guest. |
| 219 // |
| 220 // Partition data that are newer than |removal_since| will be removed. |
| 221 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask. |
| 222 bool ClearData(const base::Time remove_since, |
| 223 uint32 removal_mask, |
| 224 const base::Closure& callback); |
| 225 |
| 226 ScriptExecutor* script_executor() { return script_executor_.get(); } |
| 227 |
| 228 private: |
| 229 friend class WebViewPermissionHelper; |
| 230 WebViewGuest(content::BrowserContext* browser_context, |
| 231 int guest_instance_id); |
| 232 |
| 233 virtual ~WebViewGuest(); |
| 234 |
| 235 void AttachWebViewHelpers(content::WebContents* contents); |
| 236 |
| 237 void OnWebViewNewWindowResponse(int new_window_instance_id, |
| 238 bool allow, |
| 239 const std::string& user_input); |
| 240 |
| 241 // WebContentsObserver implementation. |
| 242 virtual void DidCommitProvisionalLoadForFrame( |
| 243 content::RenderFrameHost* render_frame_host, |
| 244 const GURL& url, |
| 245 content::PageTransition transition_type) OVERRIDE; |
| 246 virtual void DidFailProvisionalLoad( |
| 247 content::RenderFrameHost* render_frame_host, |
| 248 const GURL& validated_url, |
| 249 int error_code, |
| 250 const base::string16& error_description) OVERRIDE; |
| 251 virtual void DidStartProvisionalLoadForFrame( |
| 252 content::RenderFrameHost* render_frame_host, |
| 253 const GURL& validated_url, |
| 254 bool is_error_page, |
| 255 bool is_iframe_srcdoc) OVERRIDE; |
| 256 virtual void DocumentLoadedInFrame( |
| 257 content::RenderFrameHost* render_frame_host) OVERRIDE; |
| 258 virtual bool OnMessageReceived( |
| 259 const IPC::Message& message, |
| 260 content::RenderFrameHost* render_frame_host) OVERRIDE; |
| 261 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
| 262 virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE; |
| 263 |
| 264 // Informs the embedder of a frame name change. |
| 265 void ReportFrameNameChange(const std::string& name); |
| 266 |
| 267 // Called after the load handler is called in the guest's main frame. |
| 268 void LoadHandlerCalled(); |
| 269 |
| 270 // Called when a redirect notification occurs. |
| 271 void LoadRedirect(const GURL& old_url, |
| 272 const GURL& new_url, |
| 273 bool is_top_level); |
| 274 |
| 275 void PushWebViewStateToIOThread(); |
| 276 static void RemoveWebViewStateFromIOThread( |
| 277 content::WebContents* web_contents); |
| 278 |
| 279 void LoadURLWithParams(const GURL& url, |
| 280 const content::Referrer& referrer, |
| 281 content::PageTransition transition_type, |
| 282 content::WebContents* web_contents); |
| 283 |
| 284 void RequestNewWindowPermission( |
| 285 WindowOpenDisposition disposition, |
| 286 const gfx::Rect& initial_bounds, |
| 287 bool user_gesture, |
| 288 content::WebContents* new_contents); |
| 289 |
| 290 // Destroy unattached new windows that have been opened by this |
| 291 // WebViewGuest. |
| 292 void DestroyUnattachedWindows(); |
| 293 |
| 294 // Requests resolution of a potentially relative URL. |
| 295 GURL ResolveURL(const std::string& src); |
| 296 |
| 297 // Notification that a load in the guest resulted in abort. Note that |url| |
| 298 // may be invalid. |
| 299 void LoadAbort(bool is_top_level, |
| 300 const GURL& url, |
| 301 const std::string& error_type); |
| 302 |
| 303 void OnFrameNameChanged(bool is_top_level, const std::string& name); |
| 304 |
| 305 // Creates a new guest window owned by this WebViewGuest. |
| 306 void CreateNewGuestWebViewWindow(const content::OpenURLParams& params); |
| 307 |
| 308 void NewGuestWebViewCallback(const content::OpenURLParams& params, |
| 309 content::WebContents* guest_web_contents); |
| 310 |
| 311 bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent& event); |
| 312 |
| 313 void SetUpAutoSize(); |
| 314 |
| 315 ObserverList<ScriptExecutionObserver> script_observers_; |
| 316 scoped_ptr<ScriptExecutor> script_executor_; |
| 317 |
| 318 content::NotificationRegistrar notification_registrar_; |
| 319 |
| 320 // True if the user agent is overridden. |
| 321 bool is_overriding_user_agent_; |
| 322 |
| 323 // Stores the window name of the main frame of the guest. |
| 324 std::string name_; |
| 325 |
| 326 // Handles find requests and replies for the webview find API. |
| 327 WebViewFindHelper find_helper_; |
| 328 |
| 329 // Handles the JavaScript dialog requests. |
| 330 JavaScriptDialogHelper javascript_dialog_helper_; |
| 331 |
| 332 // Handels permission requests. |
| 333 scoped_ptr<WebViewPermissionHelper> web_view_permission_helper_; |
| 334 |
| 335 scoped_ptr<WebViewGuestDelegate> web_view_guest_delegate_; |
| 336 |
| 337 friend void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, |
| 338 bool final_update); |
| 339 |
| 340 // Tracks the name, and target URL of the new window. Once the first |
| 341 // navigation commits, we no longer track this information. |
| 342 struct NewWindowInfo { |
| 343 GURL url; |
| 344 std::string name; |
| 345 bool changed; |
| 346 NewWindowInfo(const GURL& url, const std::string& name) : |
| 347 url(url), |
| 348 name(name), |
| 349 changed(false) {} |
| 350 }; |
| 351 |
| 352 typedef std::map<WebViewGuest*, NewWindowInfo> PendingWindowMap; |
| 353 PendingWindowMap pending_new_windows_; |
| 354 |
| 355 DISALLOW_COPY_AND_ASSIGN(WebViewGuest); |
| 356 }; |
| 357 |
| 358 } // namespace extensions |
| 359 |
| 360 #endif // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ |
OLD | NEW |