1 // Copyright 2015 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_CHROME_BROWSER_SIGNIN_GAIA_AUTH_FETCHER_IOS_PRIVATE_H_
6 #define IOS_CHROME_BROWSER_SIGNIN_GAIA_AUTH_FETCHER_IOS_PRIVATE_H_
8 #import <Webkit/Webkit.h>
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/macros.h"
12 #include "ios/web/public/active_state_manager.h"
14 class GaiaAuthFetcherIOS
;
21 // Navigation delegate attached to a WKWebView used for URL fetches.
22 @interface GaiaAuthFetcherNavigationDelegate
: NSObject
<WKNavigationDelegate
>
25 // Bridge between the GaiaAuthFetcherIOS and the webview (and its navigation
26 // delegate) used to actually do the network fetch.
27 class GaiaAuthFetcherIOSBridge
: web::ActiveStateManager::Observer
{
29 GaiaAuthFetcherIOSBridge(GaiaAuthFetcherIOS
* fetcher
,
30 web::BrowserState
* browser_state
);
31 virtual ~GaiaAuthFetcherIOSBridge();
33 // Starts a network fetch.
34 // * |url| is the URL to fetch.
35 // * |headers| are the HTTP headers to add to the request.
36 // * |body| is the HTTP body to add to the request. If not empty, the fetch
37 // will be a POST request.
38 void Fetch(const GURL
& url
,
39 const std::string
& headers
,
40 const std::string
& body
);
42 // Cancels the current fetch.
45 // Informs the bridge of the success of the URL fetch.
46 // * |data| is the body of the HTTP response.
47 // URLFetchSuccess and URLFetchFailure are no-op if one of them was already
49 void URLFetchSuccess(const std::string
& data
);
51 // Informs the bridge of the failure of the URL fetch.
52 // * |is_cancelled| whether the fetch failed because it was cancelled.
53 // URLFetchSuccess and URLFetchFailure are no-op if one of them was already
55 void URLFetchFailure(bool is_cancelled
);
58 friend class GaiaAuthFetcherIOSTest
;
60 // A network request that needs to be fetched.
63 Request(const GURL
& url
,
64 const std::string
& headers
,
65 const std::string
& body
);
66 // Whether the request is pending (i.e. awaiting to be processed or
67 // currently being processed).
71 // HTTP headers to add to the request.
73 // HTTP body to add to the request.
77 // Fetches the pending request if it exists.
78 void FetchPendingRequest();
79 // Finishes the pending request and cleans up its associated state. Returns
80 // the URL of the request.
81 GURL
FinishPendingRequest();
83 // Returns the cached WKWebView if it exists, or creates one if necessary.
84 // Can return nil if the browser state is not active.
85 WKWebView
* GetWKWebView();
86 // Actually creates a WKWebView. Virtual for testing.
87 virtual WKWebView
* CreateWKWebView();
88 // Stops any page loading in the WKWebView currently in use and releases it.
89 void ResetWKWebView();
91 // ActiveStateManager::Observer implementation.
92 void OnActive() override
;
93 void OnInactive() override
;
95 // Browser state associated with the bridge, used to create WKWebViews.
96 web::BrowserState
* browser_state_
;
97 // Fetcher owning this bridge.
98 GaiaAuthFetcherIOS
* fetcher_
;
99 // Request currently processed by the bridge.
101 // Navigation delegate of |web_view_| that informs the bridge of relevant
102 // navigation events.
103 base::scoped_nsobject
<GaiaAuthFetcherNavigationDelegate
> navigation_delegate_
;
104 // Web view used to do the network requests.
105 base::scoped_nsobject
<WKWebView
> web_view_
;
107 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcherIOSBridge
);
110 #endif // IOS_CHROME_BROWSER_SIGNIN_GAIA_AUTH_FETCHER_IOS_PRIVATE_H_