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
;
60 class WebGestureEvent
;
67 // Objects implement this interface to get notified about changes in the
68 // WebContents and to provide necessary functionality.
69 class CONTENT_EXPORT WebContentsDelegate
{
71 WebContentsDelegate();
73 // Opens a new URL inside the passed in WebContents (if source is 0 open
74 // in the current front-most tab), unless |disposition| indicates the url
75 // should be opened in a new tab or window.
77 // A nullptr source indicates the current tab (callers should probably use
78 // OpenURL() for these cases which does it for you).
80 // Returns the WebContents the URL is opened in, or nullptr if the URL wasn't
81 // opened immediately.
82 virtual WebContents
* OpenURLFromTab(WebContents
* source
,
83 const OpenURLParams
& params
);
85 // Called to inform the delegate that the WebContents's navigation state
86 // changed. The |changed_flags| indicates the parts of the navigation state
87 // that have been updated.
88 virtual void NavigationStateChanged(WebContents
* source
,
89 InvalidateTypes changed_flags
) {}
91 // Called to inform the delegate that the WebContent's visible SSL state (as
92 // defined by SSLStatus) changed.
93 virtual void VisibleSSLStateChanged(const WebContents
* source
) {}
95 // Creates a new tab with the already-created WebContents 'new_contents'.
96 // The window for the added contents should be reparented correctly when this
97 // method returns. If |disposition| is NEW_POPUP, |initial_rect| should hold
98 // the initial position and size. If |was_blocked| is non-nullptr, then
99 // |*was_blocked| will be set to true if the popup gets blocked, and left
100 // unchanged otherwise.
101 virtual void AddNewContents(WebContents
* source
,
102 WebContents
* new_contents
,
103 WindowOpenDisposition disposition
,
104 const gfx::Rect
& initial_rect
,
106 bool* was_blocked
) {}
108 // Selects the specified contents, bringing its container to the front.
109 virtual void ActivateContents(WebContents
* contents
) {}
111 // Deactivates the specified contents by deactivating its container and
112 // potentialy moving it to the back of the Z order.
113 virtual void DeactivateContents(WebContents
* contents
) {}
115 // Notifies the delegate that this contents is starting or is done loading
116 // some resource. The delegate should use this notification to represent
117 // loading feedback. See WebContents::IsLoading()
118 // |to_different_document| will be true unless the load is a fragment
119 // navigation, or triggered by history.pushState/replaceState.
120 virtual void LoadingStateChanged(WebContents
* source
,
121 bool to_different_document
) {}
123 // Notifies the delegate that the page has made some progress loading.
124 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
126 virtual void LoadProgressChanged(WebContents
* source
,
129 // Request the delegate to close this web contents, and do whatever cleanup
131 virtual void CloseContents(WebContents
* source
) {}
133 // Informs the delegate that the underlying RenderViewHost has been swapped
134 // out so it can perform any cleanup necessary.
135 virtual void SwappedOut(WebContents
* source
) {}
137 // Request the delegate to move this WebContents to the specified position
138 // in screen coordinates.
139 virtual void MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {}
141 // Called to determine if the WebContents is contained in a popup window
142 // or a panel window.
143 virtual bool IsPopupOrPanel(const WebContents
* source
) const;
145 // Notification that the target URL has changed.
146 virtual void UpdateTargetURL(WebContents
* source
,
149 // Notification that there was a mouse event, along with the absolute
150 // coordinates of the mouse pointer and whether it was a normal motion event
151 // (otherwise, the pointer left the contents area).
152 virtual void ContentsMouseEvent(WebContents
* source
,
153 const gfx::Point
& location
,
156 // Request the delegate to change the zoom level of the current tab.
157 virtual void ContentsZoomChange(bool zoom_in
) {}
159 // Called to determine if the WebContents can be overscrolled with touch/wheel
161 virtual bool CanOverscrollContent() const;
163 // Callback that allows vertical overscroll activies to be communicated to the
164 // delegate. |delta_y| is the total amount of overscroll.
165 virtual void OverscrollUpdate(float delta_y
) {}
167 // Invoked when a vertical overscroll completes.
168 virtual void OverscrollComplete() {}
170 // Return the rect where to display the resize corner, if any, otherwise
172 virtual gfx::Rect
GetRootWindowResizerRect() const;
174 // Invoked prior to showing before unload handler confirmation dialog.
175 virtual void WillRunBeforeUnloadConfirm() {}
177 // Returns true if javascript dialogs and unload alerts are suppressed.
179 virtual bool ShouldSuppressDialogs(WebContents
* source
);
181 // Returns whether pending NavigationEntries for aborted browser-initiated
182 // navigations should be preserved (and thus returned from GetVisibleURL).
183 // Defaults to false.
184 virtual bool ShouldPreserveAbortedURLs(WebContents
* source
);
186 // Add a message to the console. Returning true indicates that the delegate
187 // handled the message. If false is returned the default logging mechanism
188 // will be used for the message.
189 virtual bool AddMessageToConsole(WebContents
* source
,
191 const base::string16
& message
,
193 const base::string16
& source_id
);
195 // Tells us that we've finished firing this tab's beforeunload event.
196 // The proceed bool tells us whether the user chose to proceed closing the
197 // tab. Returns true if the tab can continue on firing its unload event.
198 // If we're closing the entire browser, then we'll want to delay firing
199 // unload events until all the beforeunload events have fired.
200 virtual void BeforeUnloadFired(WebContents
* tab
,
202 bool* proceed_to_fire_unload
);
204 // Returns true if the location bar should be focused by default rather than
205 // the page contents. NOTE: this is only used if WebContents can't determine
206 // for itself whether the location bar should be focused by default. For a
207 // complete check, you should use WebContents::FocusLocationBarByDefault().
208 virtual bool ShouldFocusLocationBarByDefault(WebContents
* source
);
210 // Sets focus to the location bar or some other place that is appropriate.
211 // This is called when the tab wants to encourage user input, like for the
213 virtual void SetFocusToLocationBar(bool select_all
) {}
215 // Returns whether the page should be focused when transitioning from crashed
216 // to live. Default is true.
217 virtual bool ShouldFocusPageAfterCrash();
219 // Returns whether the page should resume accepting requests for the new
220 // window. This is used when window creation is asynchronous
221 // and the navigations need to be delayed. Default is true.
222 virtual bool ShouldResumeRequestsForCreatedWindow();
224 // This is called when WebKit tells us that it is done tabbing through
225 // controls on the page. Provides a way for WebContentsDelegates to handle
226 // this. Returns true if the delegate successfully handled it.
227 virtual bool TakeFocus(WebContents
* source
,
230 // Invoked when the page loses mouse capture.
231 virtual void LostCapture() {}
233 // Asks the delegate if the given tab can download.
234 // Invoking the |callback| synchronously is OK.
235 virtual void CanDownload(const GURL
& url
,
236 const std::string
& request_method
,
237 const base::Callback
<void(bool)>& callback
);
239 // Returns true if the context menu operation was handled by the delegate.
240 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
);
242 // Opens source view for given WebContents that is navigated to the given
244 virtual void ViewSourceForTab(WebContents
* source
, const GURL
& page_url
);
246 // Opens source view for the given subframe.
247 virtual void ViewSourceForFrame(WebContents
* source
,
249 const PageState
& page_state
);
251 // Allows delegates to handle keyboard events before sending to the renderer.
252 // Returns true if the |event| was handled. Otherwise, if the |event| would be
253 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
254 // |*is_keyboard_shortcut| should be set to true.
255 virtual bool PreHandleKeyboardEvent(WebContents
* source
,
256 const NativeWebKeyboardEvent
& event
,
257 bool* is_keyboard_shortcut
);
259 // Allows delegates to handle unhandled keyboard messages coming back from
261 virtual void HandleKeyboardEvent(WebContents
* source
,
262 const NativeWebKeyboardEvent
& event
) {}
264 // Allows delegates to handle gesture events before sending to the renderer.
265 // Returns true if the |event| was handled and thus shouldn't be processed
266 // by the renderer's event handler. Note that the touch events that create
267 // the gesture are always passed to the renderer since the gesture is created
268 // and dispatched after the touches return without being "preventDefault()"ed.
269 virtual bool PreHandleGestureEvent(
271 const blink::WebGestureEvent
& event
);
273 // Called when an external drag event enters the web contents window. Return
274 // true to allow dragging and dropping on the web contents window or false to
275 // cancel the operation. This method is used by Chromium Embedded Framework.
276 virtual bool CanDragEnter(WebContents
* source
,
277 const DropData
& data
,
278 blink::WebDragOperationsMask operations_allowed
);
280 // Shows the repost form confirmation dialog box.
281 virtual void ShowRepostFormWarningDialog(WebContents
* source
) {}
283 // Allows delegate to override navigation to the history entries.
284 // Returns true to allow WebContents to continue with the default processing.
285 virtual bool OnGoToEntryOffset(int offset
);
287 // Allows delegate to control whether a WebContents will be created. Returns
288 // true to allow the creation. Default is to allow it. In cases where the
289 // delegate handles the creation/navigation itself, it will use |target_url|.
290 // The embedder has to synchronously adopt |route_id| or else the view will
292 virtual bool ShouldCreateWebContents(
293 WebContents
* web_contents
,
295 int main_frame_route_id
,
296 WindowContainerType window_container_type
,
297 const base::string16
& frame_name
,
298 const GURL
& target_url
,
299 const std::string
& partition_id
,
300 SessionStorageNamespace
* session_storage_namespace
);
302 // Notifies the delegate about the creation of a new WebContents. This
303 // typically happens when popups are created.
304 virtual void WebContentsCreated(WebContents
* source_contents
,
305 int opener_render_frame_id
,
306 const base::string16
& frame_name
,
307 const GURL
& target_url
,
308 WebContents
* new_contents
) {}
310 // Notification that the tab is hung.
311 virtual void RendererUnresponsive(WebContents
* source
) {}
313 // Notification that the tab is no longer hung.
314 virtual void RendererResponsive(WebContents
* source
) {}
316 // Notification that a worker associated with this tab has crashed.
317 virtual void WorkerCrashed(WebContents
* source
) {}
319 // Invoked when a main fram navigation occurs.
320 virtual void DidNavigateMainFramePostCommit(WebContents
* source
) {}
322 // Returns a pointer to a service to manage JavaScript dialogs. May return
323 // nullptr in which case dialogs aren't shown.
324 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager(
325 WebContents
* source
);
327 // Called when color chooser should open. Returns the opened color chooser.
328 // Returns nullptr if we failed to open the color chooser (e.g. when there is
329 // a ColorChooserDialog already open on Windows). Ownership of the returned
330 // pointer is transferred to the caller.
331 virtual ColorChooser
* OpenColorChooser(
332 WebContents
* web_contents
,
334 const std::vector
<ColorSuggestion
>& suggestions
);
336 // Called when a file selection is to be done.
337 virtual void RunFileChooser(WebContents
* web_contents
,
338 const FileChooserParams
& params
) {}
340 // Request to enumerate a directory. This is equivalent to running the file
341 // chooser in directory-enumeration mode and having the user select the given
343 virtual void EnumerateDirectory(WebContents
* web_contents
,
345 const base::FilePath
& path
) {}
347 // Returns true if the delegate will embed a WebContents-owned fullscreen
348 // render widget. In this case, the delegate may access the widget by calling
349 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
350 // WebContents will be responsible for showing the fullscreen widget.
351 virtual bool EmbedsFullscreenWidget() const;
353 // Called when the renderer puts a tab into fullscreen mode.
354 // |origin| is the origin of the initiating frame inside the |web_contents|.
355 // |origin| can be empty in which case the |web_contents| last committed
356 // URL's origin should be used.
357 virtual void EnterFullscreenModeForTab(WebContents
* web_contents
,
358 const GURL
& origin
) {}
360 // Called when the renderer puts a tab out of fullscreen mode.
361 virtual void ExitFullscreenModeForTab(WebContents
*) {}
363 virtual bool IsFullscreenForTabOrPending(
364 const WebContents
* web_contents
) const;
366 // Returns the actual display mode of the top-level browsing context.
367 // For example, it should return 'blink::WebDisplayModeFullscreen' whenever
368 // the browser window is put to fullscreen mode (either by the end user,
369 // or HTML API or from a web manifest setting).
370 // See http://w3c.github.io/manifest/#dfn-display-mode
371 virtual blink::WebDisplayMode
GetDisplayMode(
372 const WebContents
* web_contents
) const;
374 // Register a new handler for URL requests with the given scheme.
375 // |user_gesture| is true if the registration is made in the context of a user
377 virtual void RegisterProtocolHandler(WebContents
* web_contents
,
378 const std::string
& protocol
,
380 bool user_gesture
) {}
382 // Unregister the registered handler for URL requests with the given scheme.
383 // |user_gesture| is true if the registration is made in the context of a user
385 virtual void UnregisterProtocolHandler(WebContents
* web_contents
,
386 const std::string
& protocol
,
388 bool user_gesture
) {}
390 // Result of string search in the page. This includes the number of matches
391 // found and the selection rect (in screen coordinates) for the string found.
392 // If |final_update| is false, it indicates that more results follow.
393 virtual void FindReply(WebContents
* web_contents
,
395 int number_of_matches
,
396 const gfx::Rect
& selection_rect
,
397 int active_match_ordinal
,
398 bool final_update
) {}
400 #if defined(OS_ANDROID)
401 // Provides the rects of the current find-in-page matches.
402 // Sent as a reply to RequestFindMatchRects.
403 virtual void FindMatchRectsReply(WebContents
* web_contents
,
405 const std::vector
<gfx::RectF
>& rects
,
406 const gfx::RectF
& active_rect
) {}
409 // Invoked when the preferred size of the contents has been changed.
410 virtual void UpdatePreferredSize(WebContents
* web_contents
,
411 const gfx::Size
& pref_size
) {}
413 // Invoked when the contents auto-resized and the container should match it.
414 virtual void ResizeDueToAutoResize(WebContents
* web_contents
,
415 const gfx::Size
& new_size
) {}
417 // Notification message from HTML UI.
418 virtual void WebUISend(WebContents
* web_contents
,
419 const GURL
& source_url
,
420 const std::string
& name
,
421 const base::ListValue
& args
) {}
423 // Requests to lock the mouse. Once the request is approved or rejected,
424 // GotResponseToLockMouseRequest() will be called on the requesting tab
426 virtual void RequestToLockMouse(WebContents
* web_contents
,
428 bool last_unlocked_by_target
) {}
430 // Notification that the page has lost the mouse lock.
431 virtual void LostMouseLock() {}
433 // Asks permission to use the camera and/or microphone. If permission is
434 // granted, a call should be made to |callback| with the devices. If the
435 // request is denied, a call should be made to |callback| with an empty list
436 // of devices. |request| has the details of the request (e.g. which of audio
437 // and/or video devices are requested, and lists of available devices).
438 virtual void RequestMediaAccessPermission(
439 WebContents
* web_contents
,
440 const MediaStreamRequest
& request
,
441 const MediaResponseCallback
& callback
);
443 // Checks if we have permission to access the microphone or camera. Note that
444 // this does not query the user. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE
445 // or MEDIA_DEVICE_VIDEO_CAPTURE.
446 virtual bool CheckMediaAccessPermission(WebContents
* web_contents
,
447 const GURL
& security_origin
,
448 MediaStreamType type
);
450 // Requests permission to access the PPAPI broker. The delegate should return
451 // true and call the passed in |callback| with the result, or return false
452 // to indicate that it does not support asking for permission.
453 virtual bool RequestPpapiBrokerPermission(
454 WebContents
* web_contents
,
456 const base::FilePath
& plugin_path
,
457 const base::Callback
<void(bool)>& callback
);
459 // Returns the size for the new render view created for the pending entry in
460 // |web_contents|; if there's no size, returns an empty size.
461 // This is optional for implementations of WebContentsDelegate; if the
462 // delegate doesn't provide a size, the current WebContentsView's size will be
464 virtual gfx::Size
GetSizeForNewRenderView(WebContents
* web_contents
) const;
466 // Notification that validation of a form displayed by the |web_contents|
467 // has failed. There can only be one message per |web_contents| at a time.
468 virtual void ShowValidationMessage(WebContents
* web_contents
,
469 const gfx::Rect
& anchor_in_root_view
,
470 const base::string16
& main_text
,
471 const base::string16
& sub_text
) {}
473 // Notification that the delegate should hide any showing form validation
475 virtual void HideValidationMessage(WebContents
* web_contents
) {}
477 // Notification that the form element that triggered the validation failure
479 virtual void MoveValidationMessage(WebContents
* web_contents
,
480 const gfx::Rect
& anchor_in_root_view
) {}
482 // Returns true if the WebContents is never visible.
483 virtual bool IsNeverVisible(WebContents
* web_contents
);
485 // Called in response to a request to save a frame. If this returns true, the
486 // default behavior is suppressed.
487 virtual bool SaveFrame(const GURL
& url
, const Referrer
& referrer
);
489 // Can be overridden by a delegate to return the security style of the given
490 // |web_contents|. Returns SECURITY_STYLE_UNKNOWN if not overriden.
491 virtual SecurityStyle
GetSecurityStyle(WebContents
* web_contents
);
494 virtual ~WebContentsDelegate();
497 friend class WebContentsImpl
;
499 // Called when |this| becomes the WebContentsDelegate for |source|.
500 void Attach(WebContents
* source
);
502 // Called when |this| is no longer the WebContentsDelegate for |source|.
503 void Detach(WebContents
* source
);
505 // The WebContents that this is currently a delegate for.
506 std::set
<WebContents
*> attached_contents_
;
509 } // namespace content
511 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_