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_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/process_util.h"
11 #include "base/strings/string16.h"
12 #include "base/supports_user_data.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/page_navigator.h"
16 #include "content/public/browser/save_page_type.h"
17 #include "content/public/browser/web_ui.h"
18 #include "ipc/ipc_sender.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "ui/base/window_open_disposition.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/size.h"
34 struct LoadStateWithParam
;
40 class InterstitialPage
;
42 class RenderProcessHost
;
44 class RenderWidgetHostView
;
46 class WebContentsDelegate
;
47 class WebContentsView
;
48 struct RendererPreferences
;
50 // WebContents is the core class in content/. A WebContents renders web content
51 // (usually HTML) in a rectangular area.
53 // Instantiating one is simple:
54 // scoped_ptr<content::WebContents> web_contents(
55 // content::WebContents::Create(
56 // content::WebContents::CreateParams(browser_context)));
57 // gfx::NativeView view = web_contents->GetView()->GetNativeView();
58 // // |view| is an HWND, NSView*, GtkWidget*, etc.; insert it into the view
59 // // hierarchy wherever it needs to go.
61 // That's it; go to your kitchen, grab a scone, and chill. WebContents will do
62 // all the multi-process stuff behind the scenes. More details are at
63 // http://www.chromium.org/developers/design-documents/multi-process-architecture .
65 // Each WebContents has exactly one NavigationController; each
66 // NavigationController belongs to one WebContents. The NavigationController can
67 // be obtained from GetController(), and is used to load URLs into the
68 // WebContents, navigate it backwards/forwards, etc. See navigation_controller.h
70 class WebContents
: public PageNavigator
,
72 public base::SupportsUserData
{
74 struct CONTENT_EXPORT CreateParams
{
75 explicit CreateParams(BrowserContext
* context
);
76 CreateParams(BrowserContext
* context
, SiteInstance
* site
);
78 BrowserContext
* browser_context
;
79 SiteInstance
* site_instance
;
81 int main_frame_routing_id
;
83 // Initial size of the new WebContent's view. Can be (0, 0) if not needed.
84 gfx::Size initial_size
;
86 // Used to specify the location context which display the new view should
87 // belong. This can be NULL if not needed.
88 gfx::NativeView context
;
91 // Creates a new WebContents.
92 CONTENT_EXPORT
static WebContents
* Create(const CreateParams
& params
);
94 // Similar to Create() above but should be used when you need to prepopulate
95 // the SessionStorageNamespaceMap of the WebContents. This can happen if
96 // you duplicate a WebContents, try to reconstitute it from a saved state,
97 // or when you create a new WebContents based on another one (eg., when
98 // servicing a window.open() call).
100 // You do not want to call this. If you think you do, make sure you completely
101 // understand when SessionStorageNamespace objects should be cloned, why
102 // they should not be shared by multiple WebContents, and what bad things
103 // can happen if you share the object.
104 CONTENT_EXPORT
static WebContents
* CreateWithSessionStorage(
105 const CreateParams
& params
,
106 const SessionStorageNamespaceMap
& session_storage_namespace_map
);
108 // Adds/removes a callback called on creation of each new WebContents.
109 typedef base::Callback
<void(WebContents
*)> CreatedCallback
;
110 CONTENT_EXPORT
static void AddCreatedCallback(
111 const CreatedCallback
& callback
);
112 CONTENT_EXPORT
static void RemoveCreatedCallback(
113 const CreatedCallback
& callback
);
115 // Returns a WebContents that wraps the RenderViewHost, or NULL if the
116 // render view host's delegate isn't a WebContents.
117 CONTENT_EXPORT
static WebContents
* FromRenderViewHost(
118 const RenderViewHost
* rvh
);
120 virtual ~WebContents() {}
122 // Intrinsic tab state -------------------------------------------------------
124 // Gets/Sets the delegate.
125 virtual WebContentsDelegate
* GetDelegate() = 0;
126 virtual void SetDelegate(WebContentsDelegate
* delegate
) = 0;
128 // Gets the controller for this WebContents.
129 virtual NavigationController
& GetController() = 0;
130 virtual const NavigationController
& GetController() const = 0;
132 // Returns the user browser context associated with this WebContents (via the
133 // NavigationController).
134 virtual content::BrowserContext
* GetBrowserContext() const = 0;
136 // Gets the URL that is currently being displayed, if there is one.
137 // This method is deprecated. DO NOT USE! Pick either |GetActiveURL| or
138 // |GetLastCommittedURL| as appropriate.
139 virtual const GURL
& GetURL() const = 0;
141 // Gets the URL currently being displayed in the URL bar, if there is one.
142 // This URL might be a pending navigation that hasn't committed yet, so it is
143 // not guaranteed to match the current page in this WebContents. A typical
144 // example of this is interstitials, which show the URL of the new/loading
145 // page (active) but the security context is of the old page (last committed).
146 virtual const GURL
& GetActiveURL() const = 0;
148 // Gets the last committed URL. It represents the current page that is
149 // displayed in this WebContents. It represents the current security
151 virtual const GURL
& GetLastCommittedURL() const = 0;
153 // Return the currently active RenderProcessHost and RenderViewHost. Each of
154 // these may change over time.
155 virtual RenderProcessHost
* GetRenderProcessHost() const = 0;
157 // Gets the current RenderViewHost for this tab.
158 virtual RenderViewHost
* GetRenderViewHost() const = 0;
160 typedef base::Callback
<void(RenderViewHost
* /* render_view_host */,
162 int /* y */)> GetRenderViewHostCallback
;
163 // Gets the RenderViewHost at coordinates (|x|, |y|) for this WebContents via
165 // This can be different than the current RenderViewHost if there is a
166 // BrowserPlugin at the specified position.
167 virtual void GetRenderViewHostAtPosition(
170 const GetRenderViewHostCallback
& callback
) = 0;
172 // Returns the WebContents embedding this WebContents, if any.
173 // If this is a top-level WebContents then it returns NULL.
174 virtual WebContents
* GetEmbedderWebContents() const = 0;
176 // Gets the instance ID of the current WebContents if it is embedded
177 // within a BrowserPlugin. The instance ID of a WebContents uniquely
178 // identifies it within its embedder WebContents.
179 virtual int GetEmbeddedInstanceID() const = 0;
181 // Gets the current RenderViewHost's routing id. Returns
182 // MSG_ROUTING_NONE when there is no RenderViewHost.
183 virtual int GetRoutingID() const = 0;
185 // Returns the currently active RenderWidgetHostView. This may change over
186 // time and can be NULL (during setup and teardown).
187 virtual content::RenderWidgetHostView
* GetRenderWidgetHostView() const = 0;
189 // The WebContentsView will never change and is guaranteed non-NULL.
190 virtual WebContentsView
* GetView() const = 0;
192 // Create a WebUI page for the given url. In most cases, this doesn't need to
193 // be called by embedders since content will create its own WebUI objects as
194 // necessary. However if the embedder wants to create its own WebUI object and
195 // keep track of it manually, it can use this.
196 virtual WebUI
* CreateWebUI(const GURL
& url
) = 0;
198 // Returns the committed WebUI if one exists, otherwise the pending one.
199 virtual WebUI
* GetWebUI() const = 0;
200 virtual WebUI
* GetCommittedWebUI() const = 0;
202 // Allows overriding the user agent used for NavigationEntries it owns.
203 virtual void SetUserAgentOverride(const std::string
& override
) = 0;
204 virtual const std::string
& GetUserAgentOverride() const = 0;
206 #if defined(OS_WIN) && defined(USE_AURA)
207 virtual void SetParentNativeViewAccessible(
208 gfx::NativeViewAccessible accessible_parent
) = 0;
211 // Tab navigation state ------------------------------------------------------
213 // Returns the current navigation properties, which if a navigation is
214 // pending may be provisional (e.g., the navigation could result in a
215 // download, in which case the URL would revert to what it was previously).
216 virtual const string16
& GetTitle() const = 0;
218 // The max page ID for any page that the current SiteInstance has loaded in
219 // this WebContents. Page IDs are specific to a given SiteInstance and
220 // WebContents, corresponding to a specific RenderView in the renderer.
221 // Page IDs increase with each new page that is loaded by a tab.
222 virtual int32
GetMaxPageID() = 0;
224 // The max page ID for any page that the given SiteInstance has loaded in
226 virtual int32
GetMaxPageIDForSiteInstance(SiteInstance
* site_instance
) = 0;
228 // Returns the SiteInstance associated with the current page.
229 virtual SiteInstance
* GetSiteInstance() const = 0;
231 // Returns the SiteInstance for the pending navigation, if any. Otherwise
232 // returns the current SiteInstance.
233 virtual SiteInstance
* GetPendingSiteInstance() const = 0;
235 // Return whether this WebContents is loading a resource.
236 virtual bool IsLoading() const = 0;
238 // Returns whether this WebContents is waiting for a first-response for the
239 // main resource of the page.
240 virtual bool IsWaitingForResponse() const = 0;
242 // Return the current load state and the URL associated with it.
243 virtual const net::LoadStateWithParam
& GetLoadState() const = 0;
244 virtual const string16
& GetLoadStateHost() const = 0;
246 // Return the upload progress.
247 virtual uint64
GetUploadSize() const = 0;
248 virtual uint64
GetUploadPosition() const = 0;
250 // Returns a set of the site URLs currently committed in this tab.
251 virtual std::set
<GURL
> GetSitesInTab() const = 0;
253 // Return the character encoding of the page.
254 virtual const std::string
& GetEncoding() const = 0;
256 // True if this is a secure page which displayed insecure content.
257 virtual bool DisplayedInsecureContent() const = 0;
259 // Internal state ------------------------------------------------------------
261 // Indicates whether the WebContents is being captured (e.g., for screenshots
262 // or mirroring). Increment calls must be balanced with an equivalent number
263 // of decrement calls.
264 virtual void IncrementCapturerCount() = 0;
265 virtual void DecrementCapturerCount() = 0;
267 // Indicates whether this tab should be considered crashed. The setter will
268 // also notify the delegate when the flag is changed.
269 virtual bool IsCrashed() const = 0;
270 virtual void SetIsCrashed(base::TerminationStatus status
, int error_code
) = 0;
272 virtual base::TerminationStatus
GetCrashedStatus() const = 0;
274 // Whether the tab is in the process of being destroyed.
275 virtual bool IsBeingDestroyed() const = 0;
277 // Convenience method for notifying the delegate of a navigation state
278 // change. See InvalidateType enum.
279 virtual void NotifyNavigationStateChanged(unsigned changed_flags
) = 0;
281 // Get the last time that the WebContents was made visible with WasShown()
282 virtual base::TimeTicks
GetLastSelectedTime() const = 0;
284 // Invoked when the WebContents becomes shown/hidden.
285 virtual void WasShown() = 0;
286 virtual void WasHidden() = 0;
288 // Returns true if the before unload and unload listeners need to be
289 // fired. The value of this changes over time. For example, if true and the
290 // before unload listener is executed and allows the user to exit, then this
292 virtual bool NeedToFireBeforeUnload() = 0;
294 // Commands ------------------------------------------------------------------
296 // Stop any pending navigation.
297 virtual void Stop() = 0;
299 // Creates a new WebContents with the same state as this one. The returned
300 // heap-allocated pointer is owned by the caller.
301 virtual WebContents
* Clone() = 0;
303 // Views and focus -----------------------------------------------------------
304 // Focuses the first (last if |reverse| is true) element in the page.
305 // Invoked when this tab is getting the focus through tab traversal (|reverse|
306 // is true when using Shift-Tab).
307 virtual void FocusThroughTabTraversal(bool reverse
) = 0;
309 // Interstitials -------------------------------------------------------------
311 // Various other systems need to know about our interstitials.
312 virtual bool ShowingInterstitialPage() const = 0;
314 // Returns the currently showing interstitial, NULL if no interstitial is
316 virtual InterstitialPage
* GetInterstitialPage() const = 0;
318 // Misc state & callbacks ----------------------------------------------------
320 // Check whether we can do the saving page operation this page given its MIME
322 virtual bool IsSavable() = 0;
324 // Prepare for saving the current web page to disk.
325 virtual void OnSavePage() = 0;
327 // Save page with the main HTML file path, the directory for saving resources,
328 // and the save type: HTML only or complete web page. Returns true if the
329 // saving process has been initiated successfully.
330 virtual bool SavePage(const base::FilePath
& main_file
,
331 const base::FilePath
& dir_path
,
332 SavePageType save_type
) = 0;
334 // Generate an MHTML representation of the current page in the given file.
335 virtual void GenerateMHTML(
336 const base::FilePath
& file
,
337 const base::Callback
<void(
338 const base::FilePath
& /* path to the MHTML file */,
339 int64
/* size of the file */)>& callback
) = 0;
341 // Returns true if the active NavigationEntry's page_id equals page_id.
342 virtual bool IsActiveEntry(int32 page_id
) = 0;
344 // Returns the contents MIME type after a navigation.
345 virtual const std::string
& GetContentsMimeType() const = 0;
347 // Returns true if this WebContents will notify about disconnection.
348 virtual bool WillNotifyDisconnection() const = 0;
350 // Override the encoding and reload the page by sending down
351 // ViewMsg_SetPageEncoding to the renderer. |UpdateEncoding| is kinda
352 // the opposite of this, by which 'browser' is notified of
353 // the encoding of the current tab from 'renderer' (determined by
354 // auto-detect, http header, meta, bom detection, etc).
355 virtual void SetOverrideEncoding(const std::string
& encoding
) = 0;
357 // Remove any user-defined override encoding and reload by sending down
358 // ViewMsg_ResetPageEncodingToDefault to the renderer.
359 virtual void ResetOverrideEncoding() = 0;
361 // Returns the settings which get passed to the renderer.
362 virtual content::RendererPreferences
* GetMutableRendererPrefs() = 0;
364 // Tells the tab to close now. The tab will take care not to close until it's
365 // out of nested message loops.
366 virtual void Close() = 0;
368 // A render view-originated drag has ended. Informs the render view host and
369 // WebContentsDelegate.
370 virtual void SystemDragEnded() = 0;
372 // Notification the user has made a gesture while focus was on the
373 // page. This is used to avoid uninitiated user downloads (aka carpet
374 // bombing), see DownloadRequestLimiter for details.
375 virtual void UserGestureDone() = 0;
377 // Indicates if this tab was explicitly closed by the user (control-w, close
378 // tab menu item...). This is false for actions that indirectly close the tab,
379 // such as closing the window. The setter is maintained by TabStripModel, and
380 // the getter only useful from within TAB_CLOSED notification
381 virtual void SetClosedByUserGesture(bool value
) = 0;
382 virtual bool GetClosedByUserGesture() const = 0;
384 // Gets the zoom level for this tab.
385 virtual double GetZoomLevel() const = 0;
387 // Gets the zoom percent for this tab.
388 virtual int GetZoomPercent(bool* enable_increment
,
389 bool* enable_decrement
) const = 0;
391 // Opens view-source tab for this contents.
392 virtual void ViewSource() = 0;
394 virtual void ViewFrameSource(const GURL
& url
,
395 const PageState
& page_state
)= 0;
397 // Gets the minimum/maximum zoom percent.
398 virtual int GetMinimumZoomPercent() const = 0;
399 virtual int GetMaximumZoomPercent() const = 0;
401 // Gets the preferred size of the contents.
402 virtual gfx::Size
GetPreferredSize() const = 0;
404 // Get the content restrictions (see content::ContentRestriction).
405 virtual int GetContentRestrictions() const = 0;
407 // Called when the reponse to a pending mouse lock request has arrived.
408 // Returns true if |allowed| is true and the mouse has been successfully
410 virtual bool GotResponseToLockMouseRequest(bool allowed
) = 0;
412 // Called when the user has selected a color in the color chooser.
413 virtual void DidChooseColorInColorChooser(SkColor color
) = 0;
415 // Called when the color chooser has ended.
416 virtual void DidEndColorChooser() = 0;
418 // Returns true if the location bar should be focused by default rather than
419 // the page contents. The view calls this function when the tab is focused
420 // to see what it should do.
421 virtual bool FocusLocationBarByDefault() = 0;
423 // Does this have an opener associated with it?
424 virtual bool HasOpener() const = 0;
426 typedef base::Callback
<void(int, /* id */
427 int, /* HTTP status code */
428 const GURL
&, /* image_url */
429 int, /* requested_size */
430 const std::vector
<SkBitmap
>& /* bitmaps*/)>
431 ImageDownloadCallback
;
433 // Sends a request to download the given image |url| and returns the unique
434 // id of the download request. When the download is finished, |callback| will
435 // be called with the bitmaps received from the renderer. If |is_favicon| is
436 // true, the cookies are not sent and not accepted during download. Note that
437 // |preferred_image_size| is a hint for images with multiple sizes. The
438 // downloaded image is not resized to the given image_size. If 0 is passed,
439 // the first frame of the image is returned.
440 // |max_image_size| is the maximal size of the returned image. It will be
441 // resized if needed. If 0 is passed, the maximal size is unlimited.
442 virtual int DownloadImage(const GURL
& url
,
444 uint32_t preferred_image_size
,
445 uint32_t max_image_size
,
446 const ImageDownloadCallback
& callback
) = 0;
449 // This interface should only be implemented inside content.
450 friend class WebContentsImpl
;
454 } // namespace content
456 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_H_