web_contents.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_H_
6#define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_H_
7
8#include "base/basictypes.h"
9#include "base/callback_forward.h"
10#include "base/process_util.h"
11#include "base/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/gfx/native_widget_types.h"
21#include "webkit/glue/window_open_disposition.h"
22
23namespace base {
24class TimeTicks;
25}
26
27namespace gfx {
28class Rect;
29class Size;
30}
31
32namespace net {
33struct LoadStateWithParam;
34}
35
36namespace content {
37
38class BrowserContext;
39class InterstitialPage;
40class RenderProcessHost;
41class RenderViewHost;
42class RenderWidgetHostView;
43class SiteInstance;
44class WebContentsDelegate;
45class WebContentsView;
46struct RendererPreferences;
47
48// Describes what goes in the main content area of a tab.
49class WebContents : public PageNavigator,
50                    public IPC::Sender,
51                    public base::SupportsUserData {
52 public:
53  // |base_web_contents| is used if we want to size the new WebContents's view
54  // based on the view of an existing WebContents.  This can be NULL if not
55  // needed.
56  CONTENT_EXPORT static WebContents* Create(
57      BrowserContext* browser_context,
58      SiteInstance* site_instance,
59      int routing_id,
60      const WebContents* base_web_contents);
61
62  // Similar to Create() above but should be used when you need to prepopulate
63  // the SessionStorageNamespaceMap of the WebContents. This can happen if
64  // you duplicate a WebContents, try to reconstitute it from a saved state,
65  // or when you create a new WebContents based on another one (eg., when
66  // servicing a window.open() call).
67  //
68  // You do not want to call this. If you think you do, make sure you completely
69  // understand when SessionStorageNamespace objects should be cloned, why
70  // they should not be shared by multiple WebContents, and what bad things
71  // can happen if you share the object.
72  CONTENT_EXPORT static WebContents* CreateWithSessionStorage(
73      BrowserContext* browser_context,
74      SiteInstance* site_instance,
75      int routing_id,
76      const WebContents* base_web_contents,
77      const SessionStorageNamespaceMap& session_storage_namespace_map);
78
79  // Returns a WebContents that wraps the RenderViewHost, or NULL if the
80  // render view host's delegate isn't a WebContents.
81  CONTENT_EXPORT static WebContents* FromRenderViewHost(
82      const RenderViewHost* rvh);
83
84  virtual ~WebContents() {}
85
86  // Intrinsic tab state -------------------------------------------------------
87
88  // Gets/Sets the delegate.
89  virtual WebContentsDelegate* GetDelegate() = 0;
90  virtual void SetDelegate(WebContentsDelegate* delegate) = 0;
91
92  // Gets the controller for this WebContents.
93  virtual NavigationController& GetController() = 0;
94  virtual const NavigationController& GetController() const = 0;
95
96  // Returns the user browser context associated with this WebContents (via the
97  // NavigationController).
98  virtual content::BrowserContext* GetBrowserContext() const = 0;
99
100  // Gets the URL that is currently being displayed, if there is one.
101  virtual const GURL& GetURL() const = 0;
102
103  // Return the currently active RenderProcessHost and RenderViewHost. Each of
104  // these may change over time.
105  virtual RenderProcessHost* GetRenderProcessHost() const = 0;
106
107  // Gets the current RenderViewHost for this tab.
108  virtual RenderViewHost* GetRenderViewHost() const = 0;
109
110  // Gets the current RenderViewHost's routing id. Returns
111  // MSG_ROUTING_NONE when there is no RenderViewHost.
112  virtual int GetRoutingID() const = 0;
113
114  // Returns the currently active RenderWidgetHostView. This may change over
115  // time and can be NULL (during setup and teardown).
116  virtual content::RenderWidgetHostView* GetRenderWidgetHostView() const = 0;
117
118  // The WebContentsView will never change and is guaranteed non-NULL.
119  virtual WebContentsView* GetView() const = 0;
120
121  // Create a WebUI page for the given url. In most cases, this doesn't need to
122  // be called by embedders since content will create its own WebUI objects as
123  // necessary. However if the embedder wants to create its own WebUI object and
124  // keep track of it manually, it can use this.
125  virtual WebUI* CreateWebUI(const GURL& url) = 0;
126
127  // Returns the committed WebUI if one exists, otherwise the pending one.
128  // Callers who want to use the pending WebUI for the pending navigation entry
129  // should use GetWebUIForCurrentState instead.
130  virtual WebUI* GetWebUI() const = 0;
131  virtual WebUI* GetCommittedWebUI() const = 0;
132
133  // Allows overriding the user agent used for NavigationEntries it owns.
134  virtual void SetUserAgentOverride(const std::string& override) = 0;
135  virtual const std::string& GetUserAgentOverride() const = 0;
136
137  // Tab navigation state ------------------------------------------------------
138
139  // Returns the current navigation properties, which if a navigation is
140  // pending may be provisional (e.g., the navigation could result in a
141  // download, in which case the URL would revert to what it was previously).
142  virtual const string16& GetTitle() const = 0;
143
144  // The max page ID for any page that the current SiteInstance has loaded in
145  // this WebContents.  Page IDs are specific to a given SiteInstance and
146  // WebContents, corresponding to a specific RenderView in the renderer.
147  // Page IDs increase with each new page that is loaded by a tab.
148  virtual int32 GetMaxPageID() = 0;
149
150  // The max page ID for any page that the given SiteInstance has loaded in
151  // this WebContents.
152  virtual int32 GetMaxPageIDForSiteInstance(SiteInstance* site_instance) = 0;
153
154  // Returns the SiteInstance associated with the current page.
155  virtual SiteInstance* GetSiteInstance() const = 0;
156
157  // Returns the SiteInstance for the pending navigation, if any.  Otherwise
158  // returns the current SiteInstance.
159  virtual SiteInstance* GetPendingSiteInstance() const = 0;
160
161  // Return whether this WebContents is loading a resource.
162  virtual bool IsLoading() const = 0;
163
164  // Returns whether this WebContents is waiting for a first-response for the
165  // main resource of the page.
166  virtual bool IsWaitingForResponse() const = 0;
167
168  // Return the current load state and the URL associated with it.
169  virtual const net::LoadStateWithParam& GetLoadState() const = 0;
170  virtual const string16& GetLoadStateHost() const = 0;
171
172  // Return the upload progress.
173  virtual uint64 GetUploadSize() const = 0;
174  virtual uint64 GetUploadPosition() const = 0;
175
176  // Return the character encoding of the page.
177  virtual const std::string& GetEncoding() const = 0;
178
179  // True if this is a secure page which displayed insecure content.
180  virtual bool DisplayedInsecureContent() const = 0;
181
182  // Internal state ------------------------------------------------------------
183
184  // This flag indicates whether the WebContents is currently being
185  // screenshotted.
186  virtual void SetCapturingContents(bool cap) = 0;
187
188  // Indicates whether this tab should be considered crashed. The setter will
189  // also notify the delegate when the flag is changed.
190  virtual bool IsCrashed() const  = 0;
191  virtual void SetIsCrashed(base::TerminationStatus status, int error_code) = 0;
192
193  virtual base::TerminationStatus GetCrashedStatus() const = 0;
194
195  // Whether the tab is in the process of being destroyed.
196  virtual bool IsBeingDestroyed() const = 0;
197
198  // Convenience method for notifying the delegate of a navigation state
199  // change. See InvalidateType enum.
200  virtual void NotifyNavigationStateChanged(unsigned changed_flags) = 0;
201
202  // Get the last time that the WebContents was made visible with WasShown()
203  virtual base::TimeTicks GetLastSelectedTime() const = 0;
204
205  // Invoked when the WebContents becomes shown/hidden.
206  virtual void WasShown() = 0;
207  virtual void WasHidden() = 0;
208
209  // Returns true if the before unload and unload listeners need to be
210  // fired. The value of this changes over time. For example, if true and the
211  // before unload listener is executed and allows the user to exit, then this
212  // returns false.
213  virtual bool NeedToFireBeforeUnload() = 0;
214
215  // Commands ------------------------------------------------------------------
216
217  // Stop any pending navigation.
218  virtual void Stop() = 0;
219
220  // Creates a new WebContents with the same state as this one. The returned
221  // heap-allocated pointer is owned by the caller.
222  virtual WebContents* Clone() = 0;
223
224  // Views and focus -----------------------------------------------------------
225  // TODO(brettw): Most of these should be removed and the caller should call
226  // the view directly.
227
228  // Returns the actual window that is focused when this WebContents is shown.
229  virtual gfx::NativeView GetContentNativeView() const = 0;
230
231  // Returns the NativeView associated with this WebContents. Outside of
232  // automation in the context of the UI, this is required to be implemented.
233  virtual gfx::NativeView GetNativeView() const = 0;
234
235  // Returns the bounds of this WebContents in the screen coordinate system.
236  virtual void GetContainerBounds(gfx::Rect* out) const = 0;
237
238  // Makes the tab the focused window.
239  virtual void Focus() = 0;
240
241  // Focuses the first (last if |reverse| is true) element in the page.
242  // Invoked when this tab is getting the focus through tab traversal (|reverse|
243  // is true when using Shift-Tab).
244  virtual void FocusThroughTabTraversal(bool reverse) = 0;
245
246  // Interstitials -------------------------------------------------------------
247
248  // Various other systems need to know about our interstitials.
249  virtual bool ShowingInterstitialPage() const = 0;
250
251  // Returns the currently showing interstitial, NULL if no interstitial is
252  // showing.
253  virtual InterstitialPage* GetInterstitialPage() const = 0;
254
255  // Misc state & callbacks ----------------------------------------------------
256
257  // Check whether we can do the saving page operation this page given its MIME
258  // type.
259  virtual bool IsSavable() = 0;
260
261  // Prepare for saving the current web page to disk.
262  virtual void OnSavePage() = 0;
263
264  // Save page with the main HTML file path, the directory for saving resources,
265  // and the save type: HTML only or complete web page. Returns true if the
266  // saving process has been initiated successfully.
267  virtual bool SavePage(const FilePath& main_file,
268                        const FilePath& dir_path,
269                        SavePageType save_type) = 0;
270
271  // Generate an MHTML representation of the current page in the given file.
272  virtual void GenerateMHTML(
273      const FilePath& file,
274      const base::Callback<void(const FilePath& /* path to the MHTML file */,
275                                int64 /* size of the file */)>& callback) = 0;
276
277  // Returns true if the active NavigationEntry's page_id equals page_id.
278  virtual bool IsActiveEntry(int32 page_id) = 0;
279
280  // Returns the contents MIME type after a navigation.
281  virtual const std::string& GetContentsMimeType() const = 0;
282
283  // Returns true if this WebContents will notify about disconnection.
284  virtual bool WillNotifyDisconnection() const = 0;
285
286  // Override the encoding and reload the page by sending down
287  // ViewMsg_SetPageEncoding to the renderer. |UpdateEncoding| is kinda
288  // the opposite of this, by which 'browser' is notified of
289  // the encoding of the current tab from 'renderer' (determined by
290  // auto-detect, http header, meta, bom detection, etc).
291  virtual void SetOverrideEncoding(const std::string& encoding) = 0;
292
293  // Remove any user-defined override encoding and reload by sending down
294  // ViewMsg_ResetPageEncodingToDefault to the renderer.
295  virtual void ResetOverrideEncoding() = 0;
296
297  // Returns the settings which get passed to the renderer.
298  virtual content::RendererPreferences* GetMutableRendererPrefs() = 0;
299
300  // Set the time when we started to create the new tab page.  This time is
301  // from before we created this WebContents.
302  virtual void SetNewTabStartTime(const base::TimeTicks& time) = 0;
303  virtual base::TimeTicks GetNewTabStartTime() const = 0;
304
305  // Tells the tab to close now. The tab will take care not to close until it's
306  // out of nested message loops.
307  virtual void Close() = 0;
308
309  // Notification that tab closing has started.  This can be called multiple
310  // times, subsequent calls are ignored.
311  virtual void OnCloseStarted() = 0;
312
313  // Returns true if underlying WebContentsView should accept drag-n-drop.
314  virtual bool ShouldAcceptDragAndDrop() const = 0;
315
316  // A render view-originated drag has ended. Informs the render view host and
317  // WebContentsDelegate.
318  virtual void SystemDragEnded() = 0;
319
320  // Notification the user has made a gesture while focus was on the
321  // page. This is used to avoid uninitiated user downloads (aka carpet
322  // bombing), see DownloadRequestLimiter for details.
323  virtual void UserGestureDone() = 0;
324
325  // Indicates if this tab was explicitly closed by the user (control-w, close
326  // tab menu item...). This is false for actions that indirectly close the tab,
327  // such as closing the window.  The setter is maintained by TabStripModel, and
328  // the getter only useful from within TAB_CLOSED notification
329  virtual void SetClosedByUserGesture(bool value) = 0;
330  virtual bool GetClosedByUserGesture() const = 0;
331
332  // Gets the zoom level for this tab.
333  virtual double GetZoomLevel() const = 0;
334
335  // Gets the zoom percent for this tab.
336  virtual int GetZoomPercent(bool* enable_increment,
337                             bool* enable_decrement) const = 0;
338
339  // Opens view-source tab for this contents.
340  virtual void ViewSource() = 0;
341
342  virtual void ViewFrameSource(const GURL& url,
343                               const std::string& content_state)= 0;
344
345  // Gets the minimum/maximum zoom percent.
346  virtual int GetMinimumZoomPercent() const = 0;
347  virtual int GetMaximumZoomPercent() const = 0;
348
349  // Gets the preferred size of the contents.
350  virtual gfx::Size GetPreferredSize() const = 0;
351
352  // Get the content restrictions (see content::ContentRestriction).
353  virtual int GetContentRestrictions() const = 0;
354
355  // Query the WebUIFactory for the TypeID for the current URL.
356  virtual WebUI::TypeID GetWebUITypeForCurrentState() = 0;
357
358  // Returns the WebUI for the current state of the tab. This will either be
359  // the pending WebUI, the committed WebUI, or NULL.
360  virtual WebUI* GetWebUIForCurrentState()= 0;
361
362  // Called when the reponse to a pending mouse lock request has arrived.
363  // Returns true if |allowed| is true and the mouse has been successfully
364  // locked.
365  virtual bool GotResponseToLockMouseRequest(bool allowed) = 0;
366
367  // Called when the user has selected a color in the color chooser.
368  virtual void DidChooseColorInColorChooser(int color_chooser_id,
369                                            SkColor color) = 0;
370
371  // Called when the color chooser has ended.
372  virtual void DidEndColorChooser(int color_chooser_id) = 0;
373
374  // Returns true if the location bar should be focused by default rather than
375  // the page contents. The view calls this function when the tab is focused
376  // to see what it should do.
377  virtual bool FocusLocationBarByDefault() = 0;
378
379  // Focuses the location bar.
380  virtual void SetFocusToLocationBar(bool select_all) = 0;
381
382  // Does this have an opener associated with it?
383  virtual bool HasOpener() const = 0;
384};
385
386}  // namespace content
387
388#endif  // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_H_
389