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_
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"
25 @
class CRWJSInjectionReceiver
;
26 @protocol CRWWebViewProxy
;
27 typedef id
<CRWWebViewProxy
> CRWWebViewProxyType
;
30 class CRWJSInjectionReceiver
;
31 typedef void* CRWWebViewProxyType
;
33 #endif // defined(__OBJC__)
36 class DictionaryValue
;
42 class NavigationManager
;
43 class WebInterstitial
;
44 class WebStateObserver
;
46 // Core interface for interaction with the web.
47 class WebState
: public base::SupportsUserData
{
49 // Parameters for the OpenURL() method.
50 struct OpenURLParams
{
51 OpenURLParams(const GURL
& url
,
52 const Referrer
& referrer
,
53 WindowOpenDisposition disposition
,
54 ui::PageTransition transition
,
55 bool is_renderer_initiated
);
58 // The URL/referrer to be opened.
62 // The disposition requested by the navigation source.
63 WindowOpenDisposition disposition
;
65 // The transition type of navigation.
66 ui::PageTransition transition
;
68 // Whether this navigation is initiated by the renderer process.
69 bool is_renderer_initiated
;
72 // Callback for |DownloadImage()|.
73 typedef base::Callback
<void(
75 int, /* HTTP status code */
76 const GURL
&, /* image_url */
77 const std::vector
<SkBitmap
>&, /* bitmaps */
78 /* The sizes in pixel of the bitmaps before they were resized due to the
79 max bitmap size passed to DownloadImage(). Each entry in the bitmaps
80 vector corresponds to an entry in the sizes vector. If a bitmap was
81 resized, there should be a single returned bitmap. */
82 const std::vector
<gfx::Size
>&)>
83 ImageDownloadCallback
;
85 ~WebState() override
{}
87 // The view containing the contents of the current web page. If the view has
88 // been purged due to low memory, this will recreate it. It is up to the
89 // caller to size the view.
90 virtual UIView
* GetView() = 0;
92 // Returns the type of the web view associated with this WebState.
93 virtual WebViewType
GetWebViewType() const = 0;
95 // Gets the BrowserState associated with this WebState. Can never return null.
96 virtual BrowserState
* GetBrowserState() const = 0;
98 // Opens a URL with the given disposition. The transition specifies how this
99 // navigation should be recorded in the history system (for example, typed).
100 virtual void OpenURL(const OpenURLParams
& params
) = 0;
102 // Gets the NavigationManager associated with this WebState. Can never return
104 virtual NavigationManager
* GetNavigationManager() = 0;
106 // Gets the CRWJSInjectionReceiver associated with this WebState.
107 virtual CRWJSInjectionReceiver
* GetJSInjectionReceiver() const = 0;
109 // Gets the contents MIME type.
110 virtual const std::string
& GetContentsMimeType() const = 0;
112 // Gets the value of the "Content-Language" HTTP header.
113 virtual const std::string
& GetContentLanguageHeader() const = 0;
115 // Returns true if the current view is a web view with HTML.
116 virtual bool ContentIsHTML() const = 0;
118 // Gets the URL currently being displayed in the URL bar, if there is one.
119 // This URL might be a pending navigation that hasn't committed yet, so it is
120 // not guaranteed to match the current page in this WebState. A typical
121 // example of this is interstitials, which show the URL of the new/loading
122 // page (active) but the security context is of the old page (last committed).
123 virtual const GURL
& GetVisibleURL() const = 0;
125 // Gets the last committed URL. It represents the current page that is
126 // displayed in this WebState. It represents the current security context.
127 virtual const GURL
& GetLastCommittedURL() const = 0;
129 // Returns the WebState view of the current URL. Moreover, this method
130 // will set the trustLevel enum to the appropriate level from a security point
131 // of view. The caller has to handle the case where |trust_level| is not
133 // TODO(stuartmorgan): Figure out a clean API for this.
134 // See http://crbug.com/457679
135 virtual GURL
GetCurrentURL(URLVerificationTrustLevel
* trust_level
) const = 0;
137 // Returns true if a WebInterstitial is currently displayed.
138 virtual bool IsShowingWebInterstitial() const = 0;
140 // Returns the currently visible WebInterstitial if one is shown.
141 virtual WebInterstitial
* GetWebInterstitial() const = 0;
143 // Callback used to handle script commands.
144 // The callback must return true if the command was handled, and false
146 // In particular the callback must return false if the command is unexpected
148 // The first parameter is the content of the command, the second parameter is
149 // the URL of the page, and the third parameter is a bool indicating if the
150 // user is currently interacting with the page.
151 typedef base::Callback
<bool(const base::DictionaryValue
&, const GURL
&, bool)>
152 ScriptCommandCallback
;
154 // Registers a callback that will be called when a command matching
155 // |command_prefix| is received.
156 virtual void AddScriptCommandCallback(const ScriptCommandCallback
& callback
,
157 const std::string
& command_prefix
) = 0;
159 // Removes the callback associated with |command_prefix|.
160 virtual void RemoveScriptCommandCallback(
161 const std::string
& command_prefix
) = 0;
163 // Returns the current CRWWebViewProxy object.
164 virtual CRWWebViewProxyType
GetWebViewProxy() const = 0;
166 // Sends a request to download the given image |url| and returns the unique
167 // id of the download request. When the download is finished, |callback| will
168 // be called with the bitmaps received from the renderer.
169 // If |is_favicon| is true, the cookies are not sent and not accepted during
171 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out
172 // from the bitmap results. If there are no bitmap results <=
173 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| and
174 // is the only result. A |max_bitmap_size| of 0 means unlimited.
175 // If |bypass_cache| is true, |url| is requested from the server even if it
176 // is present in the browser cache.
177 virtual int DownloadImage(const GURL
& url
,
179 uint32_t max_bitmap_size
,
181 const ImageDownloadCallback
& callback
) = 0;
184 friend class WebStateObserver
;
186 // Adds and removes observers for page navigation notifications. The order in
187 // which notifications are sent to observers is undefined. Clients must be
188 // sure to remove the observer before they go away.
189 // TODO(droger): Move these methods to WebStateImpl once it is in ios/.
190 virtual void AddObserver(WebStateObserver
* observer
) = 0;
191 virtual void RemoveObserver(WebStateObserver
* observer
) = 0;
198 #endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_H_