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.
5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_
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/bluetooth_chooser.h"
16 #include "content/public/browser/invalidate_type.h"
17 #include "content/public/browser/navigation_type.h"
18 #include "content/public/common/media_stream_request.h"
19 #include "content/public/common/security_style.h"
20 #include "content/public/common/window_container_type.h"
21 #include "third_party/WebKit/public/platform/WebDisplayMode.h"
22 #include "third_party/WebKit/public/web/WebDragOperation.h"
23 #include "third_party/skia/include/core/SkColor.h"
24 #include "ui/base/window_open_disposition.h"
25 #include "ui/gfx/geometry/rect_f.h"
26 #include "ui/gfx/native_widget_types.h"
39 class JavaScriptDialogManager
;
42 class SessionStorageNamespace
;
44 class WebContentsImpl
;
45 struct ColorSuggestion
;
46 struct ContextMenuParams
;
48 struct FileChooserParams
;
49 struct NativeWebKeyboardEvent
;
51 struct SecurityStyleExplanations
;
62 class WebGestureEvent
;
69 // Objects implement this interface to get notified about changes in the
70 // WebContents and to provide necessary functionality.
71 class CONTENT_EXPORT WebContentsDelegate
{
73 WebContentsDelegate();
75 // Opens a new URL inside the passed in WebContents (if source is 0 open
76 // in the current front-most tab), unless |disposition| indicates the url
77 // should be opened in a new tab or window.
79 // A nullptr source indicates the current tab (callers should probably use
80 // OpenURL() for these cases which does it for you).
82 // Returns the WebContents the URL is opened in, or nullptr if the URL wasn't
83 // opened immediately.
84 virtual WebContents
* OpenURLFromTab(WebContents
* source
,
85 const OpenURLParams
& params
);
87 // Called to inform the delegate that the WebContents's navigation state
88 // changed. The |changed_flags| indicates the parts of the navigation state
89 // that have been updated.
90 virtual void NavigationStateChanged(WebContents
* source
,
91 InvalidateTypes changed_flags
) {}
93 // Called to inform the delegate that the WebContent's visible SSL state (as
94 // defined by SSLStatus) changed.
95 virtual void VisibleSSLStateChanged(const WebContents
* source
) {}
97 // Creates a new tab with the already-created WebContents 'new_contents'.
98 // The window for the added contents should be reparented correctly when this
99 // method returns. If |disposition| is NEW_POPUP, |initial_rect| should hold
100 // the initial position and size. If |was_blocked| is non-nullptr, then
101 // |*was_blocked| will be set to true if the popup gets blocked, and left
102 // unchanged otherwise.
103 virtual void AddNewContents(WebContents
* source
,
104 WebContents
* new_contents
,
105 WindowOpenDisposition disposition
,
106 const gfx::Rect
& initial_rect
,
108 bool* was_blocked
) {}
110 // Selects the specified contents, bringing its container to the front.
111 virtual void ActivateContents(WebContents
* contents
) {}
113 // Deactivates the specified contents by deactivating its container and
114 // potentialy moving it to the back of the Z order.
115 virtual void DeactivateContents(WebContents
* contents
) {}
117 // Notifies the delegate that this contents is starting or is done loading
118 // some resource. The delegate should use this notification to represent
119 // loading feedback. See WebContents::IsLoading()
120 // |to_different_document| will be true unless the load is a fragment
121 // navigation, or triggered by history.pushState/replaceState.
122 virtual void LoadingStateChanged(WebContents
* source
,
123 bool to_different_document
) {}
125 // Notifies the delegate that the page has made some progress loading.
126 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
128 virtual void LoadProgressChanged(WebContents
* source
,
131 // Request the delegate to close this web contents, and do whatever cleanup
133 virtual void CloseContents(WebContents
* source
) {}
135 // Informs the delegate that the underlying RenderViewHost has been swapped
136 // out so it can perform any cleanup necessary.
137 virtual void SwappedOut(WebContents
* source
) {}
139 // Request the delegate to move this WebContents to the specified position
140 // in screen coordinates.
141 virtual void MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {}
143 // Called to determine if the WebContents is contained in a popup window
144 // or a panel window.
145 virtual bool IsPopupOrPanel(const WebContents
* source
) const;
147 // Notification that the target URL has changed.
148 virtual void UpdateTargetURL(WebContents
* source
,
151 // Notification that there was a mouse event, along with the absolute
152 // coordinates of the mouse pointer and whether it was a normal motion event
153 // (otherwise, the pointer left the contents area).
154 virtual void ContentsMouseEvent(WebContents
* source
,
155 const gfx::Point
& location
,
158 // Request the delegate to change the zoom level of the current tab.
159 virtual void ContentsZoomChange(bool zoom_in
) {}
161 // Called to determine if the WebContents can be overscrolled with touch/wheel
163 virtual bool CanOverscrollContent() const;
165 // Callback that allows vertical overscroll activies to be communicated to the
166 // delegate. |delta_y| is the total amount of overscroll.
167 virtual void OverscrollUpdate(float delta_y
) {}
169 // Invoked when a vertical overscroll completes.
170 virtual void OverscrollComplete() {}
172 // Return the rect where to display the resize corner, if any, otherwise
174 virtual gfx::Rect
GetRootWindowResizerRect() const;
176 // Invoked prior to showing before unload handler confirmation dialog.
177 virtual void WillRunBeforeUnloadConfirm() {}
179 // Returns true if javascript dialogs and unload alerts are suppressed.
181 virtual bool ShouldSuppressDialogs(WebContents
* source
);
183 // Returns whether pending NavigationEntries for aborted browser-initiated
184 // navigations should be preserved (and thus returned from GetVisibleURL).
185 // Defaults to false.
186 virtual bool ShouldPreserveAbortedURLs(WebContents
* source
);
188 // Add a message to the console. Returning true indicates that the delegate
189 // handled the message. If false is returned the default logging mechanism
190 // will be used for the message.
191 virtual bool AddMessageToConsole(WebContents
* source
,
193 const base::string16
& message
,
195 const base::string16
& source_id
);
197 // Tells us that we've finished firing this tab's beforeunload event.
198 // The proceed bool tells us whether the user chose to proceed closing the
199 // tab. Returns true if the tab can continue on firing its unload event.
200 // If we're closing the entire browser, then we'll want to delay firing
201 // unload events until all the beforeunload events have fired.
202 virtual void BeforeUnloadFired(WebContents
* tab
,
204 bool* proceed_to_fire_unload
);
206 // Returns true if the location bar should be focused by default rather than
207 // the page contents. NOTE: this is only used if WebContents can't determine
208 // for itself whether the location bar should be focused by default. For a
209 // complete check, you should use WebContents::FocusLocationBarByDefault().
210 virtual bool ShouldFocusLocationBarByDefault(WebContents
* source
);
212 // Sets focus to the location bar or some other place that is appropriate.
213 // This is called when the tab wants to encourage user input, like for the
215 virtual void SetFocusToLocationBar(bool select_all
) {}
217 // Returns whether the page should be focused when transitioning from crashed
218 // to live. Default is true.
219 virtual bool ShouldFocusPageAfterCrash();
221 // Returns whether the page should resume accepting requests for the new
222 // window. This is used when window creation is asynchronous
223 // and the navigations need to be delayed. Default is true.
224 virtual bool ShouldResumeRequestsForCreatedWindow();
226 // This is called when WebKit tells us that it is done tabbing through
227 // controls on the page. Provides a way for WebContentsDelegates to handle
228 // this. Returns true if the delegate successfully handled it.
229 virtual bool TakeFocus(WebContents
* source
,
232 // Invoked when the page loses mouse capture.
233 virtual void LostCapture() {}
235 // Asks the delegate if the given tab can download.
236 // Invoking the |callback| synchronously is OK.
237 virtual void CanDownload(const GURL
& url
,
238 const std::string
& request_method
,
239 const base::Callback
<void(bool)>& callback
);
241 // Returns true if the context menu operation was handled by the delegate.
242 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
);
244 // Opens source view for given WebContents that is navigated to the given
246 virtual void ViewSourceForTab(WebContents
* source
, const GURL
& page_url
);
248 // Opens source view for the given subframe.
249 virtual void ViewSourceForFrame(WebContents
* source
,
251 const PageState
& page_state
);
253 // Allows delegates to handle keyboard events before sending to the renderer.
254 // Returns true if the |event| was handled. Otherwise, if the |event| would be
255 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
256 // |*is_keyboard_shortcut| should be set to true.
257 virtual bool PreHandleKeyboardEvent(WebContents
* source
,
258 const NativeWebKeyboardEvent
& event
,
259 bool* is_keyboard_shortcut
);
261 // Allows delegates to handle unhandled keyboard messages coming back from
263 virtual void HandleKeyboardEvent(WebContents
* source
,
264 const NativeWebKeyboardEvent
& event
) {}
266 // Allows delegates to handle gesture events before sending to the renderer.
267 // Returns true if the |event| was handled and thus shouldn't be processed
268 // by the renderer's event handler. Note that the touch events that create
269 // the gesture are always passed to the renderer since the gesture is created
270 // and dispatched after the touches return without being "preventDefault()"ed.
271 virtual bool PreHandleGestureEvent(
273 const blink::WebGestureEvent
& event
);
275 // Called when an external drag event enters the web contents window. Return
276 // true to allow dragging and dropping on the web contents window or false to
277 // cancel the operation. This method is used by Chromium Embedded Framework.
278 virtual bool CanDragEnter(WebContents
* source
,
279 const DropData
& data
,
280 blink::WebDragOperationsMask operations_allowed
);
282 // Shows the repost form confirmation dialog box.
283 virtual void ShowRepostFormWarningDialog(WebContents
* source
) {}
285 // Allows delegate to override navigation to the history entries.
286 // Returns true to allow WebContents to continue with the default processing.
287 virtual bool OnGoToEntryOffset(int offset
);
289 // Allows delegate to control whether a WebContents will be created. Returns
290 // true to allow the creation. Default is to allow it. In cases where the
291 // delegate handles the creation/navigation itself, it will use |target_url|.
292 // The embedder has to synchronously adopt |route_id| or else the view will
294 virtual bool ShouldCreateWebContents(
295 WebContents
* web_contents
,
297 int main_frame_route_id
,
298 WindowContainerType window_container_type
,
299 const std::string
& frame_name
,
300 const GURL
& target_url
,
301 const std::string
& partition_id
,
302 SessionStorageNamespace
* session_storage_namespace
);
304 // Notifies the delegate about the creation of a new WebContents. This
305 // typically happens when popups are created.
306 virtual void WebContentsCreated(WebContents
* source_contents
,
307 int opener_render_frame_id
,
308 const std::string
& frame_name
,
309 const GURL
& target_url
,
310 WebContents
* new_contents
) {}
312 // Notification that the tab is hung.
313 virtual void RendererUnresponsive(WebContents
* source
) {}
315 // Notification that the tab is no longer hung.
316 virtual void RendererResponsive(WebContents
* source
) {}
318 // Invoked when a main fram navigation occurs.
319 virtual void DidNavigateMainFramePostCommit(WebContents
* source
) {}
321 // Returns a pointer to a service to manage JavaScript dialogs. May return
322 // nullptr in which case dialogs aren't shown.
323 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager(
324 WebContents
* source
);
326 // Called when color chooser should open. Returns the opened color chooser.
327 // Returns nullptr if we failed to open the color chooser (e.g. when there is
328 // a ColorChooserDialog already open on Windows). Ownership of the returned
329 // pointer is transferred to the caller.
330 virtual ColorChooser
* OpenColorChooser(
331 WebContents
* web_contents
,
333 const std::vector
<ColorSuggestion
>& suggestions
);
335 // Called when a file selection is to be done.
336 virtual void RunFileChooser(WebContents
* web_contents
,
337 const FileChooserParams
& params
) {}
339 // Request to enumerate a directory. This is equivalent to running the file
340 // chooser in directory-enumeration mode and having the user select the given
342 virtual void EnumerateDirectory(WebContents
* web_contents
,
344 const base::FilePath
& path
) {}
346 // Shows a chooser for the user to select a nearby Bluetooth device. The
347 // observer must live at least as long as the returned chooser object.
348 virtual scoped_ptr
<BluetoothChooser
> RunBluetoothChooser(
349 WebContents
* web_contents
,
350 const BluetoothChooser::EventHandler
& event_handler
,
353 // Returns true if the delegate will embed a WebContents-owned fullscreen
354 // render widget. In this case, the delegate may access the widget by calling
355 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
356 // WebContents will be responsible for showing the fullscreen widget.
357 virtual bool EmbedsFullscreenWidget() const;
359 // Called when the renderer puts a tab into fullscreen mode.
360 // |origin| is the origin of the initiating frame inside the |web_contents|.
361 // |origin| can be empty in which case the |web_contents| last committed
362 // URL's origin should be used.
363 virtual void EnterFullscreenModeForTab(WebContents
* web_contents
,
364 const GURL
& origin
) {}
366 // Called when the renderer puts a tab out of fullscreen mode.
367 virtual void ExitFullscreenModeForTab(WebContents
*) {}
369 virtual bool IsFullscreenForTabOrPending(
370 const WebContents
* web_contents
) const;
372 // Returns the actual display mode of the top-level browsing context.
373 // For example, it should return 'blink::WebDisplayModeFullscreen' whenever
374 // the browser window is put to fullscreen mode (either by the end user,
375 // or HTML API or from a web manifest setting).
376 // See http://w3c.github.io/manifest/#dfn-display-mode
377 virtual blink::WebDisplayMode
GetDisplayMode(
378 const WebContents
* web_contents
) const;
380 // Register a new handler for URL requests with the given scheme.
381 // |user_gesture| is true if the registration is made in the context of a user
383 virtual void RegisterProtocolHandler(WebContents
* web_contents
,
384 const std::string
& protocol
,
386 bool user_gesture
) {}
388 // Unregister the registered handler for URL requests with the given scheme.
389 // |user_gesture| is true if the registration is made in the context of a user
391 virtual void UnregisterProtocolHandler(WebContents
* web_contents
,
392 const std::string
& protocol
,
394 bool user_gesture
) {}
396 // Result of string search in the page. This includes the number of matches
397 // found and the selection rect (in screen coordinates) for the string found.
398 // If |final_update| is false, it indicates that more results follow.
399 virtual void FindReply(WebContents
* web_contents
,
401 int number_of_matches
,
402 const gfx::Rect
& selection_rect
,
403 int active_match_ordinal
,
404 bool final_update
) {}
406 #if defined(OS_ANDROID)
407 // Provides the rects of the current find-in-page matches.
408 // Sent as a reply to RequestFindMatchRects.
409 virtual void FindMatchRectsReply(WebContents
* web_contents
,
411 const std::vector
<gfx::RectF
>& rects
,
412 const gfx::RectF
& active_rect
) {}
415 // Invoked when the preferred size of the contents has been changed.
416 virtual void UpdatePreferredSize(WebContents
* web_contents
,
417 const gfx::Size
& pref_size
) {}
419 // Invoked when the contents auto-resized and the container should match it.
420 virtual void ResizeDueToAutoResize(WebContents
* web_contents
,
421 const gfx::Size
& new_size
) {}
423 // Notification message from HTML UI.
424 virtual void WebUISend(WebContents
* web_contents
,
425 const GURL
& source_url
,
426 const std::string
& name
,
427 const base::ListValue
& args
) {}
429 // Requests to lock the mouse. Once the request is approved or rejected,
430 // GotResponseToLockMouseRequest() will be called on the requesting tab
432 virtual void RequestToLockMouse(WebContents
* web_contents
,
434 bool last_unlocked_by_target
) {}
436 // Notification that the page has lost the mouse lock.
437 virtual void LostMouseLock() {}
439 // Asks permission to use the camera and/or microphone. If permission is
440 // granted, a call should be made to |callback| with the devices. If the
441 // request is denied, a call should be made to |callback| with an empty list
442 // of devices. |request| has the details of the request (e.g. which of audio
443 // and/or video devices are requested, and lists of available devices).
444 virtual void RequestMediaAccessPermission(
445 WebContents
* web_contents
,
446 const MediaStreamRequest
& request
,
447 const MediaResponseCallback
& callback
);
449 // Checks if we have permission to access the microphone or camera. Note that
450 // this does not query the user. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE
451 // or MEDIA_DEVICE_VIDEO_CAPTURE.
452 virtual bool CheckMediaAccessPermission(WebContents
* web_contents
,
453 const GURL
& security_origin
,
454 MediaStreamType type
);
456 // Requests permission to access the PPAPI broker. The delegate should return
457 // true and call the passed in |callback| with the result, or return false
458 // to indicate that it does not support asking for permission.
459 virtual bool RequestPpapiBrokerPermission(
460 WebContents
* web_contents
,
462 const base::FilePath
& plugin_path
,
463 const base::Callback
<void(bool)>& callback
);
465 // Returns the size for the new render view created for the pending entry in
466 // |web_contents|; if there's no size, returns an empty size.
467 // This is optional for implementations of WebContentsDelegate; if the
468 // delegate doesn't provide a size, the current WebContentsView's size will be
470 virtual gfx::Size
GetSizeForNewRenderView(WebContents
* web_contents
) const;
472 // Notification that validation of a form displayed by the |web_contents|
473 // has failed. There can only be one message per |web_contents| at a time.
474 virtual void ShowValidationMessage(WebContents
* web_contents
,
475 const gfx::Rect
& anchor_in_root_view
,
476 const base::string16
& main_text
,
477 const base::string16
& sub_text
) {}
479 // Notification that the delegate should hide any showing form validation
481 virtual void HideValidationMessage(WebContents
* web_contents
) {}
483 // Notification that the form element that triggered the validation failure
485 virtual void MoveValidationMessage(WebContents
* web_contents
,
486 const gfx::Rect
& anchor_in_root_view
) {}
488 // Returns true if the WebContents is never visible.
489 virtual bool IsNeverVisible(WebContents
* web_contents
);
491 // Called in response to a request to save a frame. If this returns true, the
492 // default behavior is suppressed.
493 virtual bool SaveFrame(const GURL
& url
, const Referrer
& referrer
);
495 // Can be overridden by a delegate to return the security style of the
496 // given |web_contents|, populating |security_style_explanations| to
497 // explain why the SecurityStyle was downgraded. Returns
498 // SECURITY_STYLE_UNKNOWN if not overriden.
499 virtual SecurityStyle
GetSecurityStyle(
500 WebContents
* web_contents
,
501 SecurityStyleExplanations
* security_style_explanations
);
503 // Displays platform-specific (OS) dialog with the certificate details.
504 virtual void ShowCertificateViewerInDevTools(
505 WebContents
* web_contents
,
509 virtual ~WebContentsDelegate();
512 friend class WebContentsImpl
;
514 // Called when |this| becomes the WebContentsDelegate for |source|.
515 void Attach(WebContents
* source
);
517 // Called when |this| is no longer the WebContentsDelegate for |source|.
518 void Detach(WebContents
* source
);
520 // The WebContents that this is currently a delegate for.
521 std::set
<WebContents
*> attached_contents_
;
524 } // namespace content
526 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_