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