Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_base_api_flow.h
blob64915b235080a9731b187916c1535b76851392c9
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 CHROME_BROWSER_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_
8 #include <string>
10 #include "chrome/browser/local_discovery/privet_constants.h"
11 #include "chrome/browser/local_discovery/privet_http.h"
12 #include "google_apis/gaia/oauth2_token_service.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
15 #include "net/url_request/url_request_context_getter.h"
17 namespace local_discovery {
19 // API call flow for communicating with cloud print.
20 class CloudPrintBaseApiFlow : public net::URLFetcherDelegate,
21 public OAuth2TokenService::Consumer {
22 public:
23 // TODO(noamsml): Better error model for this class.
24 enum Status {
25 SUCCESS,
26 ERROR_TOKEN,
27 ERROR_NETWORK,
28 ERROR_HTTP_CODE,
29 ERROR_FROM_SERVER,
30 ERROR_MALFORMED_RESPONSE
33 class Delegate {
34 public:
35 virtual ~Delegate() {}
37 virtual void OnCloudPrintAPIFlowError(CloudPrintBaseApiFlow* flow,
38 Status status) = 0;
39 virtual void OnCloudPrintAPIFlowComplete(
40 CloudPrintBaseApiFlow* flow,
41 const base::DictionaryValue* value) = 0;
44 // Create an OAuth2-based confirmation.
45 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context,
46 OAuth2TokenService* token_service_,
47 const std::string& account_id,
48 const GURL& automated_claim_url,
49 Delegate* delegate);
51 // Create a cookie-based confirmation.
52 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context,
53 int user_index,
54 const std::string& xsrf_token,
55 const GURL& automated_claim_url,
56 Delegate* delegate);
58 // Create a cookie-based confirmation with no XSRF token (for requests that
59 // don't need an XSRF token).
60 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context,
61 int user_index,
62 const GURL& automated_claim_url,
63 Delegate* delegate);
66 virtual ~CloudPrintBaseApiFlow();
68 void Start();
71 // net::URLFetcherDelegate implementation:
72 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
74 // OAuth2TokenService::Consumer implementation:
75 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
76 const std::string& access_token,
77 const base::Time& expiration_time) OVERRIDE;
78 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
79 const GoogleServiceAuthError& error) OVERRIDE;
81 // Return the user index or kAccountIndexUseOAuth2 if none is available.
82 int user_index() { return user_index_; }
84 private:
85 bool UseOAuth2() { return user_index_ == kAccountIndexUseOAuth2; }
87 void CreateRequest(const GURL& url);
89 scoped_ptr<net::URLFetcher> url_fetcher_;
90 scoped_ptr<OAuth2TokenService::Request> oauth_request_;
91 scoped_refptr<net::URLRequestContextGetter> request_context_;
92 OAuth2TokenService* token_service_;
93 std::string account_id_;
94 int user_index_;
95 std::string xsrf_token_;
96 GURL url_;
97 Delegate* delegate_;
100 } // namespace local_discovery
102 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_