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_API_FLOW_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_GCD_API_FLOW_H_
7
8#include <string>
9
10#include "google_apis/gaia/oauth2_token_service.h"
11#include "net/url_request/url_fetcher.h"
12#include "net/url_request/url_request_context_getter.h"
13
14namespace local_discovery {
15
16// API flow for communicating with cloud print and cloud devices.
17class GCDApiFlow {
18 public:
19  // TODO(noamsml): Better error model for this class.
20  enum Status {
21    SUCCESS,
22    ERROR_TOKEN,
23    ERROR_NETWORK,
24    ERROR_HTTP_CODE,
25    ERROR_FROM_SERVER,
26    ERROR_MALFORMED_RESPONSE
27  };
28
29  // Provides GCDApiFlowImpl with parameters required to make request.
30  // Parses results of requests.
31  class Request {
32   public:
33    Request();
34    virtual ~Request();
35
36    virtual void OnGCDAPIFlowError(Status status) = 0;
37
38    virtual void OnGCDAPIFlowComplete(const base::DictionaryValue& value) = 0;
39
40    virtual GURL GetURL() = 0;
41
42    virtual std::string GetOAuthScope() = 0;
43
44    virtual net::URLFetcher::RequestType GetRequestType();
45
46    virtual std::vector<std::string> GetExtraRequestHeaders() = 0;
47
48    // If there is no data, set upload_type and upload_data to ""
49    virtual void GetUploadData(std::string* upload_type,
50                               std::string* upload_data);
51
52   private:
53    DISALLOW_COPY_AND_ASSIGN(Request);
54  };
55
56  GCDApiFlow();
57  virtual ~GCDApiFlow();
58
59  static scoped_ptr<GCDApiFlow> Create(
60      net::URLRequestContextGetter* request_context,
61      OAuth2TokenService* token_service,
62      const std::string& account_id);
63
64  virtual void Start(scoped_ptr<Request> request) = 0;
65
66 private:
67  DISALLOW_COPY_AND_ASSIGN(GCDApiFlow);
68};
69
70class GCDApiFlowRequest : public GCDApiFlow::Request {
71 public:
72  GCDApiFlowRequest();
73  virtual ~GCDApiFlowRequest();
74
75  // GCDApiFlowRequest implementation
76  virtual std::string GetOAuthScope() OVERRIDE;
77  virtual std::vector<std::string> GetExtraRequestHeaders() OVERRIDE;
78
79 private:
80  DISALLOW_COPY_AND_ASSIGN(GCDApiFlowRequest);
81};
82
83class CloudPrintApiFlowRequest : public GCDApiFlow::Request {
84 public:
85  CloudPrintApiFlowRequest();
86  virtual ~CloudPrintApiFlowRequest();
87
88  // GCDApiFlowRequest implementation
89  virtual std::string GetOAuthScope() OVERRIDE;
90  virtual std::vector<std::string> GetExtraRequestHeaders() OVERRIDE;
91
92 private:
93  DISALLOW_COPY_AND_ASSIGN(CloudPrintApiFlowRequest);
94};
95
96}  // namespace local_discovery
97
98#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_GCD_API_FLOW_H_
99