Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / gcd_base_api_flow.h
1 // Copyright 2014 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 CHROME_BROWSER_LOCAL_DISCOVERY_GCD_BASE_API_FLOW_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_GCD_BASE_API_FLOW_H_
7
8 #include <string>
9
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"
16
17 namespace local_discovery {
18
19 // API call flow for communicating with cloud print.
20 class GCDBaseApiFlow : 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
31   };
32
33   class Delegate {
34    public:
35     virtual ~Delegate() {}
36
37     virtual void OnGCDAPIFlowError(GCDBaseApiFlow* flow, Status status) = 0;
38
39     virtual void OnGCDAPIFlowComplete(GCDBaseApiFlow* flow,
40                                       const base::DictionaryValue* value) = 0;
41
42     // Return 1 if flow is for a Cloud Print device
43     virtual bool GCDIsCloudPrint() = 0;
44
45     virtual net::URLFetcher::RequestType GetRequestType();
46
47     // If there is no data, set upload_type and upload_data to ""
48     virtual void GetUploadData(std::string* upload_type,
49                                std::string* upload_data);
50   };
51
52   // Create an OAuth2-based confirmation.
53   GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
54                  OAuth2TokenService* token_service,
55                  const std::string& account_id,
56                  const GURL& automated_claim_url,
57                  Delegate* delegate);
58
59   // Create a cookie-based confirmation.
60   GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
61                  int user_index,
62                  const std::string& xsrf_token,
63                  const GURL& automated_claim_url,
64                  Delegate* delegate);
65
66   // Create a cookie-based confirmation with no XSRF token (for requests that
67   // don't need an XSRF token).
68   GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
69                  int user_index,
70                  const GURL& automated_claim_url,
71                  Delegate* delegate);
72
73   virtual ~GCDBaseApiFlow();
74
75   void Start();
76
77   // net::URLFetcherDelegate implementation:
78   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
79
80   // OAuth2TokenService::Consumer implementation:
81   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
82                                  const std::string& access_token,
83                                  const base::Time& expiration_time) OVERRIDE;
84   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
85                                  const GoogleServiceAuthError& error) OVERRIDE;
86
87  private:
88   void CreateRequest(const GURL& url);
89
90   scoped_ptr<net::URLFetcher> url_fetcher_;
91   scoped_ptr<OAuth2TokenService::Request> oauth_request_;
92   scoped_refptr<net::URLRequestContextGetter> request_context_;
93   OAuth2TokenService* token_service_;
94   std::string account_id_;
95   GURL url_;
96   Delegate* delegate_;
97 };
98
99 }  // namespace local_discovery
100
101 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_GCD_BASE_API_FLOW_H_