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/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"
36 class JavaScriptDialogManager
;
39 class SessionStorageNamespace
;
41 class WebContentsImpl
;
42 struct ColorSuggestion
;
43 struct ContextMenuParams
;
45 struct FileChooserParams
;
46 struct NativeWebKeyboardEvent
;
58 class WebGestureEvent
;
60 struct WebWindowFeatures
;
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 NULL 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 NULL 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(const 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_pos| should hold
98 // the initial position. If |was_blocked| is non-NULL, then |*was_blocked|
99 // will be set to true if the popup gets blocked, and left unchanged
101 virtual void AddNewContents(WebContents
* source
,
102 WebContents
* new_contents
,
103 WindowOpenDisposition disposition
,
104 const gfx::Rect
& initial_pos
,
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
,
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(int 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();
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 // This is called when WebKit tells us that it is done tabbing through
221 // controls on the page. Provides a way for WebContentsDelegates to handle
222 // this. Returns true if the delegate successfully handled it.
223 virtual bool TakeFocus(WebContents
* source
,
226 // Invoked when the page loses mouse capture.
227 virtual void LostCapture() {}
229 // Notification that |contents| has gained focus.
230 virtual void WebContentsFocused(WebContents
* contents
) {}
232 // Asks the delegate if the given tab can download.
233 // Invoking the |callback| synchronously is OK.
234 virtual void CanDownload(RenderViewHost
* render_view_host
,
236 const std::string
& request_method
,
237 const base::Callback
<void(bool)>& callback
);
239 // Return much extra vertical space should be allotted to the
240 // render view widget during various animations (e.g. infobar closing).
241 // This is used to make painting look smoother.
242 virtual int GetExtraRenderViewHeight() const;
244 // Returns true if the context menu operation was handled by the delegate.
245 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
);
247 // Opens source view for given WebContents that is navigated to the given
249 virtual void ViewSourceForTab(WebContents
* source
, const GURL
& page_url
);
251 // Opens source view for the given subframe.
252 virtual void ViewSourceForFrame(WebContents
* source
,
254 const PageState
& page_state
);
256 // Allows delegates to handle keyboard events before sending to the renderer.
257 // Returns true if the |event| was handled. Otherwise, if the |event| would be
258 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
259 // |*is_keyboard_shortcut| should be set to true.
260 virtual bool PreHandleKeyboardEvent(WebContents
* source
,
261 const NativeWebKeyboardEvent
& event
,
262 bool* is_keyboard_shortcut
);
264 // Allows delegates to handle unhandled keyboard messages coming back from
266 virtual void HandleKeyboardEvent(WebContents
* source
,
267 const NativeWebKeyboardEvent
& event
) {}
269 virtual void HandleMouseDown() {}
270 virtual void HandleMouseUp() {}
272 // Handles activation resulting from a pointer event (e.g. when mouse is
273 // pressed, or a touch-gesture begins).
274 virtual void HandlePointerActivate() {}
276 // Allows delegates to handle gesture events before sending to the renderer.
277 // Returns true if the |event| was handled and thus shouldn't be processed
278 // by the renderer's event handler. Note that the touch events that create
279 // the gesture are always passed to the renderer since the gesture is created
280 // and dispatched after the touches return without being "preventDefault()"ed.
281 virtual bool PreHandleGestureEvent(
283 const blink::WebGestureEvent
& event
);
285 virtual void HandleGestureBegin() {}
286 virtual void HandleGestureEnd() {}
288 // Called when an external drag event enters the web contents window. Return
289 // true to allow dragging and dropping on the web contents window or false to
290 // cancel the operation. This method is used by Chromium Embedded Framework.
291 virtual bool CanDragEnter(WebContents
* source
,
292 const DropData
& data
,
293 blink::WebDragOperationsMask operations_allowed
);
295 // Render view drag n drop ended.
296 virtual void DragEnded() {}
298 // Shows the repost form confirmation dialog box.
299 virtual void ShowRepostFormWarningDialog(WebContents
* source
) {}
301 // Allows delegate to override navigation to the history entries.
302 // Returns true to allow WebContents to continue with the default processing.
303 virtual bool OnGoToEntryOffset(int offset
);
305 // Allows delegate to control whether a WebContents will be created. Returns
306 // true to allow the creation. Default is to allow it. In cases where the
307 // delegate handles the creation/navigation itself, it will use |target_url|.
308 // The embedder has to synchronously adopt |route_id| or else the view will
310 virtual bool ShouldCreateWebContents(
311 WebContents
* web_contents
,
313 WindowContainerType window_container_type
,
314 const base::string16
& frame_name
,
315 const GURL
& target_url
,
316 const std::string
& partition_id
,
317 SessionStorageNamespace
* session_storage_namespace
);
319 // Notifies the delegate about the creation of a new WebContents. This
320 // typically happens when popups are created.
321 virtual void WebContentsCreated(WebContents
* source_contents
,
322 int opener_render_frame_id
,
323 const base::string16
& frame_name
,
324 const GURL
& target_url
,
325 WebContents
* new_contents
) {}
327 // Notification that the tab is hung.
328 virtual void RendererUnresponsive(WebContents
* source
) {}
330 // Notification that the tab is no longer hung.
331 virtual void RendererResponsive(WebContents
* source
) {}
333 // Notification that a worker associated with this tab has crashed.
334 virtual void WorkerCrashed(WebContents
* source
) {}
336 // Invoked when a main fram navigation occurs.
337 virtual void DidNavigateMainFramePostCommit(WebContents
* source
) {}
339 // Invoked when navigating to a pending entry. When invoked the
340 // NavigationController has configured its pending entry, but it has not yet
342 virtual void DidNavigateToPendingEntry(WebContents
* source
) {}
344 // Returns a pointer to a service to manage JavaScript dialogs. May return
345 // NULL in which case dialogs aren't shown.
346 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager();
348 // Called when color chooser should open. Returns the opened color chooser.
349 // Returns NULL if we failed to open the color chooser (e.g. when there is a
350 // ColorChooserDialog already open on Windows). Ownership of the returned
351 // pointer is transferred to the caller.
352 virtual ColorChooser
* OpenColorChooser(
353 WebContents
* web_contents
,
355 const std::vector
<ColorSuggestion
>& suggestions
);
357 // Called when a file selection is to be done.
358 virtual void RunFileChooser(WebContents
* web_contents
,
359 const FileChooserParams
& params
) {}
361 // Request to enumerate a directory. This is equivalent to running the file
362 // chooser in directory-enumeration mode and having the user select the given
364 virtual void EnumerateDirectory(WebContents
* web_contents
,
366 const base::FilePath
& path
) {}
368 // Returns true if the delegate will embed a WebContents-owned fullscreen
369 // render widget. In this case, the delegate may access the widget by calling
370 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
371 // WebContents will be responsible for showing the fullscreen widget.
372 virtual bool EmbedsFullscreenWidget() const;
374 // Called when the renderer puts a tab into or out of fullscreen mode.
375 virtual void ToggleFullscreenModeForTab(WebContents
* web_contents
,
376 bool enter_fullscreen
) {}
377 virtual bool IsFullscreenForTabOrPending(
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
);
492 virtual ~WebContentsDelegate();
495 friend class WebContentsImpl
;
497 // Called when |this| becomes the WebContentsDelegate for |source|.
498 void Attach(WebContents
* source
);
500 // Called when |this| is no longer the WebContentsDelegate for |source|.
501 void Detach(WebContents
* source
);
503 // The WebContents that this is currently a delegate for.
504 std::set
<WebContents
*> attached_contents_
;
507 } // namespace content
509 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_