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/invalidate_type.h"
16 #include "content/public/browser/navigation_type.h"
17 #include "content/public/common/media_stream_request.h"
18 #include "content/public/common/security_style.h"
19 #include "content/public/common/window_container_type.h"
20 #include "third_party/WebKit/public/platform/WebDisplayMode.h"
21 #include "third_party/WebKit/public/web/WebDragOperation.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/base/window_open_disposition.h"
24 #include "ui/gfx/geometry/rect_f.h"
25 #include "ui/gfx/native_widget_types.h"
38 class JavaScriptDialogManager
;
41 class SessionStorageNamespace
;
43 class WebContentsImpl
;
44 struct ColorSuggestion
;
45 struct ContextMenuParams
;
47 struct FileChooserParams
;
48 struct NativeWebKeyboardEvent
;
50 struct SecurityStyleExplanations
;
61 class WebGestureEvent
;
68 // Objects implement this interface to get notified about changes in the
69 // WebContents and to provide necessary functionality.
70 class CONTENT_EXPORT WebContentsDelegate
{
72 WebContentsDelegate();
74 // Opens a new URL inside the passed in WebContents (if source is 0 open
75 // in the current front-most tab), unless |disposition| indicates the url
76 // should be opened in a new tab or window.
78 // A nullptr source indicates the current tab (callers should probably use
79 // OpenURL() for these cases which does it for you).
81 // Returns the WebContents the URL is opened in, or nullptr if the URL wasn't
82 // opened immediately.
83 virtual WebContents
* OpenURLFromTab(WebContents
* source
,
84 const OpenURLParams
& params
);
86 // Called to inform the delegate that the WebContents's navigation state
87 // changed. The |changed_flags| indicates the parts of the navigation state
88 // that have been updated.
89 virtual void NavigationStateChanged(WebContents
* source
,
90 InvalidateTypes changed_flags
) {}
92 // Called to inform the delegate that the WebContent's visible SSL state (as
93 // defined by SSLStatus) changed.
94 virtual void VisibleSSLStateChanged(const WebContents
* source
) {}
96 // Creates a new tab with the already-created WebContents 'new_contents'.
97 // The window for the added contents should be reparented correctly when this
98 // method returns. If |disposition| is NEW_POPUP, |initial_rect| should hold
99 // the initial position and size. If |was_blocked| is non-nullptr, then
100 // |*was_blocked| will be set to true if the popup gets blocked, and left
101 // unchanged otherwise.
102 virtual void AddNewContents(WebContents
* source
,
103 WebContents
* new_contents
,
104 WindowOpenDisposition disposition
,
105 const gfx::Rect
& initial_rect
,
107 bool* was_blocked
) {}
109 // Selects the specified contents, bringing its container to the front.
110 virtual void ActivateContents(WebContents
* contents
) {}
112 // Deactivates the specified contents by deactivating its container and
113 // potentialy moving it to the back of the Z order.
114 virtual void DeactivateContents(WebContents
* contents
) {}
116 // Notifies the delegate that this contents is starting or is done loading
117 // some resource. The delegate should use this notification to represent
118 // loading feedback. See WebContents::IsLoading()
119 // |to_different_document| will be true unless the load is a fragment
120 // navigation, or triggered by history.pushState/replaceState.
121 virtual void LoadingStateChanged(WebContents
* source
,
122 bool to_different_document
) {}
124 // Notifies the delegate that the page has made some progress loading.
125 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
127 virtual void LoadProgressChanged(WebContents
* source
,
130 // Request the delegate to close this web contents, and do whatever cleanup
132 virtual void CloseContents(WebContents
* source
) {}
134 // Informs the delegate that the underlying RenderViewHost has been swapped
135 // out so it can perform any cleanup necessary.
136 virtual void SwappedOut(WebContents
* source
) {}
138 // Request the delegate to move this WebContents to the specified position
139 // in screen coordinates.
140 virtual void MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {}
142 // Called to determine if the WebContents is contained in a popup window
143 // or a panel window.
144 virtual bool IsPopupOrPanel(const WebContents
* source
) const;
146 // Notification that the target URL has changed.
147 virtual void UpdateTargetURL(WebContents
* source
,
150 // Notification that there was a mouse event, along with the absolute
151 // coordinates of the mouse pointer and whether it was a normal motion event
152 // (otherwise, the pointer left the contents area).
153 virtual void ContentsMouseEvent(WebContents
* source
,
154 const gfx::Point
& location
,
157 // Request the delegate to change the zoom level of the current tab.
158 virtual void ContentsZoomChange(bool zoom_in
) {}
160 // Called to determine if the WebContents can be overscrolled with touch/wheel
162 virtual bool CanOverscrollContent() const;
164 // Callback that allows vertical overscroll activies to be communicated to the
165 // delegate. |delta_y| is the total amount of overscroll.
166 virtual void OverscrollUpdate(float delta_y
) {}
168 // Invoked when a vertical overscroll completes.
169 virtual void OverscrollComplete() {}
171 // Return the rect where to display the resize corner, if any, otherwise
173 virtual gfx::Rect
GetRootWindowResizerRect() const;
175 // Invoked prior to showing before unload handler confirmation dialog.
176 virtual void WillRunBeforeUnloadConfirm() {}
178 // Returns true if javascript dialogs and unload alerts are suppressed.
180 virtual bool ShouldSuppressDialogs(WebContents
* source
);
182 // Returns whether pending NavigationEntries for aborted browser-initiated
183 // navigations should be preserved (and thus returned from GetVisibleURL).
184 // Defaults to false.
185 virtual bool ShouldPreserveAbortedURLs(WebContents
* source
);
187 // Add a message to the console. Returning true indicates that the delegate
188 // handled the message. If false is returned the default logging mechanism
189 // will be used for the message.
190 virtual bool AddMessageToConsole(WebContents
* source
,
192 const base::string16
& message
,
194 const base::string16
& source_id
);
196 // Tells us that we've finished firing this tab's beforeunload event.
197 // The proceed bool tells us whether the user chose to proceed closing the
198 // tab. Returns true if the tab can continue on firing its unload event.
199 // If we're closing the entire browser, then we'll want to delay firing
200 // unload events until all the beforeunload events have fired.
201 virtual void BeforeUnloadFired(WebContents
* tab
,
203 bool* proceed_to_fire_unload
);
205 // Returns true if the location bar should be focused by default rather than
206 // the page contents. NOTE: this is only used if WebContents can't determine
207 // for itself whether the location bar should be focused by default. For a
208 // complete check, you should use WebContents::FocusLocationBarByDefault().
209 virtual bool ShouldFocusLocationBarByDefault(WebContents
* source
);
211 // Sets focus to the location bar or some other place that is appropriate.
212 // This is called when the tab wants to encourage user input, like for the
214 virtual void SetFocusToLocationBar(bool select_all
) {}
216 // Returns whether the page should be focused when transitioning from crashed
217 // to live. Default is true.
218 virtual bool ShouldFocusPageAfterCrash();
220 // Returns whether the page should resume accepting requests for the new
221 // window. This is used when window creation is asynchronous
222 // and the navigations need to be delayed. Default is true.
223 virtual bool ShouldResumeRequestsForCreatedWindow();
225 // This is called when WebKit tells us that it is done tabbing through
226 // controls on the page. Provides a way for WebContentsDelegates to handle
227 // this. Returns true if the delegate successfully handled it.
228 virtual bool TakeFocus(WebContents
* source
,
231 // Invoked when the page loses mouse capture.
232 virtual void LostCapture() {}
234 // Asks the delegate if the given tab can download.
235 // Invoking the |callback| synchronously is OK.
236 virtual void CanDownload(const GURL
& url
,
237 const std::string
& request_method
,
238 const base::Callback
<void(bool)>& callback
);
240 // Returns true if the context menu operation was handled by the delegate.
241 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
);
243 // Opens source view for given WebContents that is navigated to the given
245 virtual void ViewSourceForTab(WebContents
* source
, const GURL
& page_url
);
247 // Opens source view for the given subframe.
248 virtual void ViewSourceForFrame(WebContents
* source
,
250 const PageState
& page_state
);
252 // Allows delegates to handle keyboard events before sending to the renderer.
253 // Returns true if the |event| was handled. Otherwise, if the |event| would be
254 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
255 // |*is_keyboard_shortcut| should be set to true.
256 virtual bool PreHandleKeyboardEvent(WebContents
* source
,
257 const NativeWebKeyboardEvent
& event
,
258 bool* is_keyboard_shortcut
);
260 // Allows delegates to handle unhandled keyboard messages coming back from
262 virtual void HandleKeyboardEvent(WebContents
* source
,
263 const NativeWebKeyboardEvent
& event
) {}
265 // Allows delegates to handle gesture events before sending to the renderer.
266 // Returns true if the |event| was handled and thus shouldn't be processed
267 // by the renderer's event handler. Note that the touch events that create
268 // the gesture are always passed to the renderer since the gesture is created
269 // and dispatched after the touches return without being "preventDefault()"ed.
270 virtual bool PreHandleGestureEvent(
272 const blink::WebGestureEvent
& event
);
274 // Called when an external drag event enters the web contents window. Return
275 // true to allow dragging and dropping on the web contents window or false to
276 // cancel the operation. This method is used by Chromium Embedded Framework.
277 virtual bool CanDragEnter(WebContents
* source
,
278 const DropData
& data
,
279 blink::WebDragOperationsMask operations_allowed
);
281 // Shows the repost form confirmation dialog box.
282 virtual void ShowRepostFormWarningDialog(WebContents
* source
) {}
284 // Allows delegate to override navigation to the history entries.
285 // Returns true to allow WebContents to continue with the default processing.
286 virtual bool OnGoToEntryOffset(int offset
);
288 // Allows delegate to control whether a WebContents will be created. Returns
289 // true to allow the creation. Default is to allow it. In cases where the
290 // delegate handles the creation/navigation itself, it will use |target_url|.
291 // The embedder has to synchronously adopt |route_id| or else the view will
293 virtual bool ShouldCreateWebContents(
294 WebContents
* web_contents
,
296 int main_frame_route_id
,
297 WindowContainerType window_container_type
,
298 const std::string
& frame_name
,
299 const GURL
& target_url
,
300 const std::string
& partition_id
,
301 SessionStorageNamespace
* session_storage_namespace
);
303 // Notifies the delegate about the creation of a new WebContents. This
304 // typically happens when popups are created.
305 virtual void WebContentsCreated(WebContents
* source_contents
,
306 int opener_render_frame_id
,
307 const std::string
& frame_name
,
308 const GURL
& target_url
,
309 WebContents
* new_contents
) {}
311 // Notification that the tab is hung.
312 virtual void RendererUnresponsive(WebContents
* source
) {}
314 // Notification that the tab is no longer hung.
315 virtual void RendererResponsive(WebContents
* source
) {}
317 // Invoked when a main fram navigation occurs.
318 virtual void DidNavigateMainFramePostCommit(WebContents
* source
) {}
320 // Returns a pointer to a service to manage JavaScript dialogs. May return
321 // nullptr in which case dialogs aren't shown.
322 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager(
323 WebContents
* source
);
325 // Called when color chooser should open. Returns the opened color chooser.
326 // Returns nullptr if we failed to open the color chooser (e.g. when there is
327 // a ColorChooserDialog already open on Windows). Ownership of the returned
328 // pointer is transferred to the caller.
329 virtual ColorChooser
* OpenColorChooser(
330 WebContents
* web_contents
,
332 const std::vector
<ColorSuggestion
>& suggestions
);
334 // Called when a file selection is to be done.
335 virtual void RunFileChooser(WebContents
* web_contents
,
336 const FileChooserParams
& params
) {}
338 // Request to enumerate a directory. This is equivalent to running the file
339 // chooser in directory-enumeration mode and having the user select the given
341 virtual void EnumerateDirectory(WebContents
* web_contents
,
343 const base::FilePath
& path
) {}
345 // Returns true if the delegate will embed a WebContents-owned fullscreen
346 // render widget. In this case, the delegate may access the widget by calling
347 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
348 // WebContents will be responsible for showing the fullscreen widget.
349 virtual bool EmbedsFullscreenWidget() const;
351 // Called when the renderer puts a tab into fullscreen mode.
352 // |origin| is the origin of the initiating frame inside the |web_contents|.
353 // |origin| can be empty in which case the |web_contents| last committed
354 // URL's origin should be used.
355 virtual void EnterFullscreenModeForTab(WebContents
* web_contents
,
356 const GURL
& origin
) {}
358 // Called when the renderer puts a tab out of fullscreen mode.
359 virtual void ExitFullscreenModeForTab(WebContents
*) {}
361 virtual bool IsFullscreenForTabOrPending(
362 const WebContents
* web_contents
) const;
364 // Returns the actual display mode of the top-level browsing context.
365 // For example, it should return 'blink::WebDisplayModeFullscreen' whenever
366 // the browser window is put to fullscreen mode (either by the end user,
367 // or HTML API or from a web manifest setting).
368 // See http://w3c.github.io/manifest/#dfn-display-mode
369 virtual blink::WebDisplayMode
GetDisplayMode(
370 const WebContents
* web_contents
) const;
372 // Register a new handler for URL requests with the given scheme.
373 // |user_gesture| is true if the registration is made in the context of a user
375 virtual void RegisterProtocolHandler(WebContents
* web_contents
,
376 const std::string
& protocol
,
378 bool user_gesture
) {}
380 // Unregister the registered 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 UnregisterProtocolHandler(WebContents
* web_contents
,
384 const std::string
& protocol
,
386 bool user_gesture
) {}
388 // Result of string search in the page. This includes the number of matches
389 // found and the selection rect (in screen coordinates) for the string found.
390 // If |final_update| is false, it indicates that more results follow.
391 virtual void FindReply(WebContents
* web_contents
,
393 int number_of_matches
,
394 const gfx::Rect
& selection_rect
,
395 int active_match_ordinal
,
396 bool final_update
) {}
398 #if defined(OS_ANDROID)
399 // Provides the rects of the current find-in-page matches.
400 // Sent as a reply to RequestFindMatchRects.
401 virtual void FindMatchRectsReply(WebContents
* web_contents
,
403 const std::vector
<gfx::RectF
>& rects
,
404 const gfx::RectF
& active_rect
) {}
407 // Invoked when the preferred size of the contents has been changed.
408 virtual void UpdatePreferredSize(WebContents
* web_contents
,
409 const gfx::Size
& pref_size
) {}
411 // Invoked when the contents auto-resized and the container should match it.
412 virtual void ResizeDueToAutoResize(WebContents
* web_contents
,
413 const gfx::Size
& new_size
) {}
415 // Notification message from HTML UI.
416 virtual void WebUISend(WebContents
* web_contents
,
417 const GURL
& source_url
,
418 const std::string
& name
,
419 const base::ListValue
& args
) {}
421 // Requests to lock the mouse. Once the request is approved or rejected,
422 // GotResponseToLockMouseRequest() will be called on the requesting tab
424 virtual void RequestToLockMouse(WebContents
* web_contents
,
426 bool last_unlocked_by_target
) {}
428 // Notification that the page has lost the mouse lock.
429 virtual void LostMouseLock() {}
431 // Asks permission to use the camera and/or microphone. If permission is
432 // granted, a call should be made to |callback| with the devices. If the
433 // request is denied, a call should be made to |callback| with an empty list
434 // of devices. |request| has the details of the request (e.g. which of audio
435 // and/or video devices are requested, and lists of available devices).
436 virtual void RequestMediaAccessPermission(
437 WebContents
* web_contents
,
438 const MediaStreamRequest
& request
,
439 const MediaResponseCallback
& callback
);
441 // Checks if we have permission to access the microphone or camera. Note that
442 // this does not query the user. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE
443 // or MEDIA_DEVICE_VIDEO_CAPTURE.
444 virtual bool CheckMediaAccessPermission(WebContents
* web_contents
,
445 const GURL
& security_origin
,
446 MediaStreamType type
);
448 // Requests permission to access the PPAPI broker. The delegate should return
449 // true and call the passed in |callback| with the result, or return false
450 // to indicate that it does not support asking for permission.
451 virtual bool RequestPpapiBrokerPermission(
452 WebContents
* web_contents
,
454 const base::FilePath
& plugin_path
,
455 const base::Callback
<void(bool)>& callback
);
457 // Returns the size for the new render view created for the pending entry in
458 // |web_contents|; if there's no size, returns an empty size.
459 // This is optional for implementations of WebContentsDelegate; if the
460 // delegate doesn't provide a size, the current WebContentsView's size will be
462 virtual gfx::Size
GetSizeForNewRenderView(WebContents
* web_contents
) const;
464 // Notification that validation of a form displayed by the |web_contents|
465 // has failed. There can only be one message per |web_contents| at a time.
466 virtual void ShowValidationMessage(WebContents
* web_contents
,
467 const gfx::Rect
& anchor_in_root_view
,
468 const base::string16
& main_text
,
469 const base::string16
& sub_text
) {}
471 // Notification that the delegate should hide any showing form validation
473 virtual void HideValidationMessage(WebContents
* web_contents
) {}
475 // Notification that the form element that triggered the validation failure
477 virtual void MoveValidationMessage(WebContents
* web_contents
,
478 const gfx::Rect
& anchor_in_root_view
) {}
480 // Returns true if the WebContents is never visible.
481 virtual bool IsNeverVisible(WebContents
* web_contents
);
483 // Called in response to a request to save a frame. If this returns true, the
484 // default behavior is suppressed.
485 virtual bool SaveFrame(const GURL
& url
, const Referrer
& referrer
);
487 // Can be overridden by a delegate to return the security style of the
488 // given |web_contents|, populating |security_style_explanations| to
489 // explain why the SecurityStyle was downgraded. Returns
490 // SECURITY_STYLE_UNKNOWN if not overriden.
491 virtual SecurityStyle
GetSecurityStyle(
492 WebContents
* web_contents
,
493 SecurityStyleExplanations
* security_style_explanations
);
496 virtual ~WebContentsDelegate();
499 friend class WebContentsImpl
;
501 // Called when |this| becomes the WebContentsDelegate for |source|.
502 void Attach(WebContents
* source
);
504 // Called when |this| is no longer the WebContentsDelegate for |source|.
505 void Detach(WebContents
* source
);
507 // The WebContents that this is currently a delegate for.
508 std::set
<WebContents
*> attached_contents_
;
511 } // namespace content
513 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_