Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / public / web_state / web_state.h
blob6752371a8fe7b415561143da421ff2705329192e
1 // Copyright 2013 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 IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_H_
6 #define IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/supports_user_data.h"
13 #include "ios/web/public/referrer.h"
14 #include "ios/web/public/web_state/url_verification_constants.h"
15 #include "ios/web/public/web_view_type.h"
16 #include "ui/base/page_transition_types.h"
17 #include "ui/base/window_open_disposition.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "url/gurl.h"
21 class GURL;
22 class SkBitmap;
24 #if defined(__OBJC__)
25 @class CRWJSInjectionReceiver;
26 @protocol CRWScrollableContent;
27 @protocol CRWWebViewProxy;
28 typedef id<CRWWebViewProxy> CRWWebViewProxyType;
29 @class UIView;
30 typedef UIView<CRWScrollableContent> CRWContentView;
31 #else
32 class CRWJSInjectionReceiver;
33 typedef void CRWContentView;
34 typedef void* CRWWebViewProxyType;
35 class UIView;
36 #endif // defined(__OBJC__)
38 namespace base {
39 class DictionaryValue;
42 namespace web {
44 class BrowserState;
45 class NavigationManager;
46 class WebInterstitial;
47 class WebStateObserver;
48 class WebStatePolicyDecider;
50 // Core interface for interaction with the web.
51 class WebState : public base::SupportsUserData {
52 public:
53 // Parameters for the OpenURL() method.
54 struct OpenURLParams {
55 OpenURLParams(const GURL& url,
56 const Referrer& referrer,
57 WindowOpenDisposition disposition,
58 ui::PageTransition transition,
59 bool is_renderer_initiated);
60 ~OpenURLParams();
62 // The URL/referrer to be opened.
63 GURL url;
64 Referrer referrer;
66 // The disposition requested by the navigation source.
67 WindowOpenDisposition disposition;
69 // The transition type of navigation.
70 ui::PageTransition transition;
72 // Whether this navigation is initiated by the renderer process.
73 bool is_renderer_initiated;
76 // Callback for |DownloadImage()|.
77 typedef base::Callback<void(
78 int, /* id */
79 int, /* HTTP status code */
80 const GURL&, /* image_url */
81 const std::vector<SkBitmap>&, /* bitmaps */
82 /* The sizes in pixel of the bitmaps before they were resized due to the
83 max bitmap size passed to DownloadImage(). Each entry in the bitmaps
84 vector corresponds to an entry in the sizes vector. If a bitmap was
85 resized, there should be a single returned bitmap. */
86 const std::vector<gfx::Size>&)>
87 ImageDownloadCallback;
89 ~WebState() override {}
91 // The view containing the contents of the current web page. If the view has
92 // been purged due to low memory, this will recreate it. It is up to the
93 // caller to size the view.
94 virtual UIView* GetView() = 0;
96 // Returns the type of the web view associated with this WebState.
97 virtual WebViewType GetWebViewType() const = 0;
99 // Gets the BrowserState associated with this WebState. Can never return null.
100 virtual BrowserState* GetBrowserState() const = 0;
102 // Opens a URL with the given disposition. The transition specifies how this
103 // navigation should be recorded in the history system (for example, typed).
104 virtual void OpenURL(const OpenURLParams& params) = 0;
106 // Gets the NavigationManager associated with this WebState. Can never return
107 // null.
108 virtual NavigationManager* GetNavigationManager() = 0;
110 // Gets the CRWJSInjectionReceiver associated with this WebState.
111 virtual CRWJSInjectionReceiver* GetJSInjectionReceiver() const = 0;
113 // Gets the contents MIME type.
114 virtual const std::string& GetContentsMimeType() const = 0;
116 // Gets the value of the "Content-Language" HTTP header.
117 virtual const std::string& GetContentLanguageHeader() const = 0;
119 // Returns true if the current page is a web view with HTML.
120 virtual bool ContentIsHTML() const = 0;
122 // Returns the current navigation title. This could be the title of the page
123 // if it is available or the URL.
124 virtual const base::string16& GetTitle() const = 0;
126 // Returns true if the current page is loading.
127 virtual bool IsLoading() const = 0;
129 // Gets the URL currently being displayed in the URL bar, if there is one.
130 // This URL might be a pending navigation that hasn't committed yet, so it is
131 // not guaranteed to match the current page in this WebState. A typical
132 // example of this is interstitials, which show the URL of the new/loading
133 // page (active) but the security context is of the old page (last committed).
134 virtual const GURL& GetVisibleURL() const = 0;
136 // Gets the last committed URL. It represents the current page that is
137 // displayed in this WebState. It represents the current security context.
138 virtual const GURL& GetLastCommittedURL() const = 0;
140 // Returns the WebState view of the current URL. Moreover, this method
141 // will set the trustLevel enum to the appropriate level from a security point
142 // of view. The caller has to handle the case where |trust_level| is not
143 // appropriate.
144 // TODO(stuartmorgan): Figure out a clean API for this.
145 // See http://crbug.com/457679
146 virtual GURL GetCurrentURL(URLVerificationTrustLevel* trust_level) const = 0;
148 // Resizes |content_view| to the content area's size and adds it to the
149 // hierarchy. A navigation will remove the view from the hierarchy.
150 virtual void ShowTransientContentView(CRWContentView* content_view) = 0;
152 // Returns true if a WebInterstitial is currently displayed.
153 virtual bool IsShowingWebInterstitial() const = 0;
155 // Returns the currently visible WebInterstitial if one is shown.
156 virtual WebInterstitial* GetWebInterstitial() const = 0;
158 // Callback used to handle script commands.
159 // The callback must return true if the command was handled, and false
160 // otherwise.
161 // In particular the callback must return false if the command is unexpected
162 // or ill-formatted.
163 // The first parameter is the content of the command, the second parameter is
164 // the URL of the page, and the third parameter is a bool indicating if the
165 // user is currently interacting with the page.
166 typedef base::Callback<bool(const base::DictionaryValue&, const GURL&, bool)>
167 ScriptCommandCallback;
169 // Registers a callback that will be called when a command matching
170 // |command_prefix| is received.
171 virtual void AddScriptCommandCallback(const ScriptCommandCallback& callback,
172 const std::string& command_prefix) = 0;
174 // Removes the callback associated with |command_prefix|.
175 virtual void RemoveScriptCommandCallback(
176 const std::string& command_prefix) = 0;
178 // Returns the current CRWWebViewProxy object.
179 virtual CRWWebViewProxyType GetWebViewProxy() const = 0;
181 // Sends a request to download the given image |url| and returns the unique
182 // id of the download request. When the download is finished, |callback| will
183 // be called with the bitmaps received from the renderer.
184 // If |is_favicon| is true, the cookies are not sent and not accepted during
185 // download.
186 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out
187 // from the bitmap results. If there are no bitmap results <=
188 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| and
189 // is the only result. A |max_bitmap_size| of 0 means unlimited.
190 // If |bypass_cache| is true, |url| is requested from the server even if it
191 // is present in the browser cache.
192 virtual int DownloadImage(const GURL& url,
193 bool is_favicon,
194 uint32_t max_bitmap_size,
195 bool bypass_cache,
196 const ImageDownloadCallback& callback) = 0;
198 protected:
199 friend class WebStateObserver;
200 friend class WebStatePolicyDecider;
202 // Adds and removes observers for page navigation notifications. The order in
203 // which notifications are sent to observers is undefined. Clients must be
204 // sure to remove the observer before they go away.
205 // TODO(droger): Move these methods to WebStateImpl once it is in ios/.
206 virtual void AddObserver(WebStateObserver* observer) = 0;
207 virtual void RemoveObserver(WebStateObserver* observer) = 0;
209 // Adds and removes policy deciders for navigation actions. The order in which
210 // deciders are called is undefined, and will stop on the first decider that
211 // refuses a navigation. Clients must be sure to remove the deciders before
212 // they go away.
213 virtual void AddPolicyDecider(WebStatePolicyDecider* decider) = 0;
214 virtual void RemovePolicyDecider(WebStatePolicyDecider* decider) = 0;
216 WebState() {}
219 } // namespace web
221 #endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_H_