1 // Copyright (c) 2012 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 CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_ 6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/strings/string16.h" 14 #include "content/common/content_export.h" 15 #include "content/public/browser/navigation_type.h" 16 #include "content/public/common/media_stream_request.h" 17 #include "content/public/common/page_transition_types.h" 18 #include "content/public/common/window_container_type.h" 19 #include "third_party/WebKit/public/web/WebDragOperation.h" 20 #include "third_party/skia/include/core/SkColor.h" 21 #include "ui/base/window_open_disposition.h" 22 #include "ui/gfx/native_widget_types.h" 23 #include "ui/gfx/rect_f.h" 24 #include "ui/gfx/vector2d.h" 25 26 class GURL; 27 28 namespace base { 29 class FilePath; 30 class ListValue; 31 } 32 33 namespace content { 34 class BrowserContext; 35 class ColorChooser; 36 class DownloadItem; 37 class JavaScriptDialogManager; 38 class PageState; 39 class RenderViewHost; 40 class WebContents; 41 class WebContentsImpl; 42 struct ContextMenuParams; 43 struct DropData; 44 struct FileChooserParams; 45 struct NativeWebKeyboardEvent; 46 struct Referrer; 47 struct SSLStatus; 48 } 49 50 namespace gfx { 51 class Point; 52 class Rect; 53 class Size; 54 } 55 56 namespace WebKit { 57 class WebLayer; 58 struct WebWindowFeatures; 59 } 60 61 namespace content { 62 63 struct OpenURLParams; 64 65 // Objects implement this interface to get notified about changes in the 66 // WebContents and to provide necessary functionality. 67 class CONTENT_EXPORT WebContentsDelegate { 68 public: 69 WebContentsDelegate(); 70 71 // Opens a new URL inside the passed in WebContents (if source is 0 open 72 // in the current front-most tab), unless |disposition| indicates the url 73 // should be opened in a new tab or window. 74 // 75 // A NULL source indicates the current tab (callers should probably use 76 // OpenURL() for these cases which does it for you). 77 78 // Returns the WebContents the URL is opened in, or NULL if the URL wasn't 79 // opened immediately. 80 virtual WebContents* OpenURLFromTab(WebContents* source, 81 const OpenURLParams& params); 82 83 // Called to inform the delegate that the WebContents's navigation state 84 // changed. The |changed_flags| indicates the parts of the navigation state 85 // that have been updated, and is any combination of the 86 // |WebContents::InvalidateTypes| bits. 87 virtual void NavigationStateChanged(const WebContents* source, 88 unsigned changed_flags) {} 89 90 // Adds the navigation request headers to |headers|. Use 91 // net::HttpUtil::AppendHeaderIfMissing to build the set of headers. 92 virtual void AddNavigationHeaders(const GURL& url, std::string* headers) {} 93 94 // Creates a new tab with the already-created WebContents 'new_contents'. 95 // The window for the added contents should be reparented correctly when this 96 // method returns. If |disposition| is NEW_POPUP, |pos| should hold the 97 // initial position. If |was_blocked| is non-NULL, then |*was_blocked| will 98 // be set to true if the popup gets blocked, and left unchanged otherwise. 99 virtual void AddNewContents(WebContents* source, 100 WebContents* new_contents, 101 WindowOpenDisposition disposition, 102 const gfx::Rect& initial_pos, 103 bool user_gesture, 104 bool* was_blocked) {} 105 106 // Selects the specified contents, bringing its container to the front. 107 virtual void ActivateContents(WebContents* contents) {} 108 109 // Deactivates the specified contents by deactivating its container and 110 // potentialy moving it to the back of the Z order. 111 virtual void DeactivateContents(WebContents* contents) {} 112 113 // Notifies the delegate that this contents is starting or is done loading 114 // some resource. The delegate should use this notification to represent 115 // loading feedback. See WebContents::IsLoading() 116 virtual void LoadingStateChanged(WebContents* source) {} 117 118 #if defined(OS_ANDROID) 119 // Notifies the delegate that the page has made some progress loading. 120 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully 121 // loaded). 122 virtual void LoadProgressChanged(WebContents* source, 123 double progress) {} 124 #endif 125 126 // Request the delegate to close this web contents, and do whatever cleanup 127 // it needs to do. 128 virtual void CloseContents(WebContents* source) {} 129 130 // Informs the delegate that the underlying RenderViewHost has been swapped 131 // out so it can perform any cleanup necessary. 132 virtual void SwappedOut(WebContents* source) {} 133 134 // Request the delegate to move this WebContents to the specified position 135 // in screen coordinates. 136 virtual void MoveContents(WebContents* source, const gfx::Rect& pos) {} 137 138 // Called to determine if the WebContents is contained in a popup window 139 // or a panel window. 140 virtual bool IsPopupOrPanel(const WebContents* source) const; 141 142 // Notification that the target URL has changed. 143 virtual void UpdateTargetURL(WebContents* source, 144 int32 page_id, 145 const GURL& url) {} 146 147 // Notification that there was a mouse event, along with the absolute 148 // coordinates of the mouse pointer and whether it was a normal motion event 149 // (otherwise, the pointer left the contents area). 150 virtual void ContentsMouseEvent(WebContents* source, 151 const gfx::Point& location, 152 bool motion) {} 153 154 // Request the delegate to change the zoom level of the current tab. 155 virtual void ContentsZoomChange(bool zoom_in) {} 156 157 // Called to determine if the WebContents can be overscrolled with touch/wheel 158 // gestures. 159 virtual bool CanOverscrollContent() const; 160 161 // Callback that allows vertical overscroll activies to be communicated to the 162 // delegate. 163 virtual void OverscrollUpdate(int delta_y) {} 164 165 // Check whether this contents is permitted to load data URLs in WebUI mode. 166 // This is normally disallowed for security. 167 virtual bool CanLoadDataURLsInWebUI() const; 168 169 // Return the rect where to display the resize corner, if any, otherwise 170 // an empty rect. 171 virtual gfx::Rect GetRootWindowResizerRect() const; 172 173 // Invoked prior to showing before unload handler confirmation dialog. 174 virtual void WillRunBeforeUnloadConfirm() {} 175 176 // Returns true if javascript dialogs and unload alerts are suppressed. 177 // Default is false. 178 virtual bool ShouldSuppressDialogs(); 179 180 // Add a message to the console. Returning true indicates that the delegate 181 // handled the message. If false is returned the default logging mechanism 182 // will be used for the message. 183 virtual bool AddMessageToConsole(WebContents* source, 184 int32 level, 185 const string16& message, 186 int32 line_no, 187 const string16& source_id); 188 189 // Tells us that we've finished firing this tab's beforeunload event. 190 // The proceed bool tells us whether the user chose to proceed closing the 191 // tab. Returns true if the tab can continue on firing its unload event. 192 // If we're closing the entire browser, then we'll want to delay firing 193 // unload events until all the beforeunload events have fired. 194 virtual void BeforeUnloadFired(WebContents* tab, 195 bool proceed, 196 bool* proceed_to_fire_unload); 197 198 // Returns true if the location bar should be focused by default rather than 199 // the page contents. NOTE: this is only used if WebContents can't determine 200 // for itself whether the location bar should be focused by default. For a 201 // complete check, you should use WebContents::FocusLocationBarByDefault(). 202 virtual bool ShouldFocusLocationBarByDefault(WebContents* source); 203 204 // Sets focus to the location bar or some other place that is appropriate. 205 // This is called when the tab wants to encourage user input, like for the 206 // new tab page. 207 virtual void SetFocusToLocationBar(bool select_all) {} 208 209 // Returns whether the page should be focused when transitioning from crashed 210 // to live. Default is true. 211 virtual bool ShouldFocusPageAfterCrash(); 212 213 // Called when a popup select is about to be displayed. The delegate can use 214 // this to disable inactive rendering for the frame in the window the select 215 // is opened within if necessary. 216 virtual void RenderWidgetShowing() {} 217 218 // This is called when WebKit tells us that it is done tabbing through 219 // controls on the page. Provides a way for WebContentsDelegates to handle 220 // this. Returns true if the delegate successfully handled it. 221 virtual bool TakeFocus(WebContents* source, 222 bool reverse); 223 224 // Invoked when the page loses mouse capture. 225 virtual void LostCapture() {} 226 227 // Notification that |contents| has gained focus. 228 virtual void WebContentsFocused(WebContents* contents) {} 229 230 // Asks the delegate if the given tab can download. 231 // Invoking the |callback| synchronously is OK. 232 virtual void CanDownload(RenderViewHost* render_view_host, 233 int request_id, 234 const std::string& request_method, 235 const base::Callback<void(bool)>& callback); 236 237 // Return much extra vertical space should be allotted to the 238 // render view widget during various animations (e.g. infobar closing). 239 // This is used to make painting look smoother. 240 virtual int GetExtraRenderViewHeight() const; 241 242 // Returns true if the context menu operation was handled by the delegate. 243 virtual bool HandleContextMenu(const content::ContextMenuParams& params); 244 245 // Opens source view for given WebContents that is navigated to the given 246 // page url. 247 virtual void ViewSourceForTab(WebContents* source, const GURL& page_url); 248 249 // Opens source view for the given subframe. 250 virtual void ViewSourceForFrame(WebContents* source, 251 const GURL& url, 252 const PageState& page_state); 253 254 // Allows delegates to handle keyboard events before sending to the renderer. 255 // Returns true if the |event| was handled. Otherwise, if the |event| would be 256 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut, 257 // |*is_keyboard_shortcut| should be set to true. 258 virtual bool PreHandleKeyboardEvent(WebContents* source, 259 const NativeWebKeyboardEvent& event, 260 bool* is_keyboard_shortcut); 261 262 // Allows delegates to handle unhandled keyboard messages coming back from 263 // the renderer. 264 virtual void HandleKeyboardEvent(WebContents* source, 265 const NativeWebKeyboardEvent& event) {} 266 267 virtual void HandleMouseDown() {} 268 virtual void HandleMouseUp() {} 269 270 // Handles activation resulting from a pointer event (e.g. when mouse is 271 // pressed, or a touch-gesture begins). 272 virtual void HandlePointerActivate() {} 273 274 virtual void HandleGestureBegin() {} 275 virtual void HandleGestureEnd() {} 276 277 // Called when an external drag event enters the web contents window. Return 278 // true to allow dragging and dropping on the web contents window or false to 279 // cancel the operation. This method is used by Chromium Embedded Framework. 280 virtual bool CanDragEnter(WebContents* source, 281 const DropData& data, 282 WebKit::WebDragOperationsMask operations_allowed); 283 284 // Render view drag n drop ended. 285 virtual void DragEnded() {} 286 287 // Shows the repost form confirmation dialog box. 288 virtual void ShowRepostFormWarningDialog(WebContents* source) {} 289 290 // Allows delegate to override navigation to the history entries. 291 // Returns true to allow WebContents to continue with the default processing. 292 virtual bool OnGoToEntryOffset(int offset); 293 294 // Allows delegate to control whether a WebContents will be created. Returns 295 // true to allow the creation. Default is to allow it. In cases where the 296 // delegate handles the creation/navigation itself, it will use |target_url|. 297 virtual bool ShouldCreateWebContents( 298 WebContents* web_contents, 299 int route_id, 300 WindowContainerType window_container_type, 301 const string16& frame_name, 302 const GURL& target_url, 303 const Referrer& referrer, 304 WindowOpenDisposition disposition, 305 const WebKit::WebWindowFeatures& features, 306 bool user_gesture, 307 bool opener_suppressed); 308 309 // Notifies the delegate about the creation of a new WebContents. This 310 // typically happens when popups are created. 311 virtual void WebContentsCreated(WebContents* source_contents, 312 int64 source_frame_id, 313 const string16& frame_name, 314 const GURL& target_url, 315 WebContents* new_contents) {} 316 317 // Notification that the tab is hung. 318 virtual void RendererUnresponsive(WebContents* source) {} 319 320 // Notification that the tab is no longer hung. 321 virtual void RendererResponsive(WebContents* source) {} 322 323 // Notification that a worker associated with this tab has crashed. 324 virtual void WorkerCrashed(WebContents* source) {} 325 326 // Invoked when a main fram navigation occurs. 327 virtual void DidNavigateMainFramePostCommit(WebContents* source) {} 328 329 // Invoked when navigating to a pending entry. When invoked the 330 // NavigationController has configured its pending entry, but it has not yet 331 // been committed. 332 virtual void DidNavigateToPendingEntry(WebContents* source) {} 333 334 // Returns a pointer to a service to manage JavaScript dialogs. May return 335 // NULL in which case dialogs aren't shown. 336 virtual JavaScriptDialogManager* GetJavaScriptDialogManager(); 337 338 // Called when color chooser should open. Returns the opened color chooser. 339 // Ownership of the returned pointer is transferred to the caller. 340 virtual ColorChooser* OpenColorChooser(WebContents* web_contents, 341 SkColor color); 342 343 // Called when a file selection is to be done. 344 virtual void RunFileChooser(WebContents* web_contents, 345 const FileChooserParams& params) {} 346 347 // Request to enumerate a directory. This is equivalent to running the file 348 // chooser in directory-enumeration mode and having the user select the given 349 // directory. 350 virtual void EnumerateDirectory(WebContents* web_contents, 351 int request_id, 352 const base::FilePath& path) {} 353 354 // Called when the renderer puts a tab into or out of fullscreen mode. 355 virtual void ToggleFullscreenModeForTab(WebContents* web_contents, 356 bool enter_fullscreen) {} 357 virtual bool IsFullscreenForTabOrPending( 358 const WebContents* web_contents) const; 359 360 // Called when the renderer has scrolled programmatically. 361 virtual void DidProgrammaticallyScroll(WebContents* web_contents, 362 const gfx::Vector2d& scroll_point) {} 363 364 // Called when a Javascript out of memory notification is received. 365 virtual void JSOutOfMemory(WebContents* web_contents) {} 366 367 // Register a new handler for URL requests with the given scheme. 368 // |user_gesture| is true if the registration is made in the context of a user 369 // gesture. 370 virtual void RegisterProtocolHandler(WebContents* web_contents, 371 const std::string& protocol, 372 const GURL& url, 373 const string16& title, 374 bool user_gesture) {} 375 376 // Result of string search in the page. This includes the number of matches 377 // found and the selection rect (in screen coordinates) for the string found. 378 // If |final_update| is false, it indicates that more results follow. 379 virtual void FindReply(WebContents* web_contents, 380 int request_id, 381 int number_of_matches, 382 const gfx::Rect& selection_rect, 383 int active_match_ordinal, 384 bool final_update) {} 385 386 #if defined(OS_ANDROID) 387 // Provides the rects of the current find-in-page matches. 388 // Sent as a reply to RequestFindMatchRects. 389 virtual void FindMatchRectsReply(WebContents* web_contents, 390 int version, 391 const std::vector<gfx::RectF>& rects, 392 const gfx::RectF& active_rect) {} 393 #endif 394 395 // Invoked when the preferred size of the contents has been changed. 396 virtual void UpdatePreferredSize(WebContents* web_contents, 397 const gfx::Size& pref_size) {} 398 399 // Invoked when the contents auto-resized and the container should match it. 400 virtual void ResizeDueToAutoResize(WebContents* web_contents, 401 const gfx::Size& new_size) {} 402 403 // Notification message from HTML UI. 404 virtual void WebUISend(WebContents* web_contents, 405 const GURL& source_url, 406 const std::string& name, 407 const base::ListValue& args) {} 408 409 // Requests to lock the mouse. Once the request is approved or rejected, 410 // GotResponseToLockMouseRequest() will be called on the requesting tab 411 // contents. 412 virtual void RequestToLockMouse(WebContents* web_contents, 413 bool user_gesture, 414 bool last_unlocked_by_target) {} 415 416 // Notification that the page has lost the mouse lock. 417 virtual void LostMouseLock() {} 418 419 // Asks permission to use the camera and/or microphone. If permission is 420 // granted, a call should be made to |callback| with the devices. If the 421 // request is denied, a call should be made to |callback| with an empty list 422 // of devices. |request| has the details of the request (e.g. which of audio 423 // and/or video devices are requested, and lists of available devices). 424 virtual void RequestMediaAccessPermission( 425 WebContents* web_contents, 426 const MediaStreamRequest& request, 427 const MediaResponseCallback& callback); 428 429 // Requests permission to access the PPAPI broker. The delegate should return 430 // true and call the passed in |callback| with the result, or return false 431 // to indicate that it does not support asking for permission. 432 virtual bool RequestPpapiBrokerPermission( 433 WebContents* web_contents, 434 const GURL& url, 435 const base::FilePath& plugin_path, 436 const base::Callback<void(bool)>& callback); 437 438 protected: 439 virtual ~WebContentsDelegate(); 440 441 private: 442 friend class WebContentsImpl; 443 444 // Called when |this| becomes the WebContentsDelegate for |source|. 445 void Attach(WebContents* source); 446 447 // Called when |this| is no longer the WebContentsDelegate for |source|. 448 void Detach(WebContents* source); 449 450 // The WebContents that this is currently a delegate for. 451 std::set<WebContents*> attached_contents_; 452 }; 453 454 } // namespace content 455 456 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_ 457