privet_http.h revision 3551c9c881056c480085172ff9840cab31610854
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.
4
5#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "chrome/browser/local_discovery/privet_url_fetcher.h"
12#include "net/base/host_port_pair.h"
13
14namespace local_discovery {
15
16class PrivetHTTPClient;
17
18// Represents a request to /privet/info. Will store a cached response and token
19// in the PrivetHTTPClient that created.
20class PrivetInfoOperation {
21 public:
22  class Delegate {
23   public:
24    virtual ~Delegate() {}
25
26    // In case of non-HTTP errors, |http_code| will be -1.
27    virtual void OnPrivetInfoDone(
28        PrivetInfoOperation* operation,
29        int http_code,
30        const base::DictionaryValue* json_value) = 0;
31  };
32
33  virtual ~PrivetInfoOperation() {}
34
35  virtual void Start() = 0;
36
37  virtual PrivetHTTPClient* GetHTTPClient() = 0;
38};
39
40// Represents a full registration flow (/privet/register), normally consisting
41// of calling the start action, the getClaimToken action, and calling the
42// complete action. Some intervention from the caller is required to display the
43// claim URL to the user (noted in OnPrivetRegisterClaimURL).
44class PrivetRegisterOperation {
45 public:
46  enum FailureReason {
47    FAILURE_NETWORK,
48    FAILURE_HTTP_ERROR,
49    FAILURE_JSON_ERROR,
50    FAILURE_MALFORMED_RESPONSE
51  };
52
53  class Delegate {
54   public:
55    ~Delegate() {}
56
57    // Called when a user needs to claim the printer by visiting the given URL.
58    virtual void OnPrivetRegisterClaimToken(
59        PrivetRegisterOperation* operation,
60        const std::string& token,
61        const GURL& url) = 0;
62
63    // Called in case of an error while registering.  |action| is the
64    // registration action taken during the error. |reason| is the reason for
65    // the failure. |printer_http_code| is the http code returned from the
66    // printer. If it is -1, an internal error occurred while trying to complete
67    // the request. |json| may be null if printer_http_code signifies an error.
68    virtual void OnPrivetRegisterError(PrivetRegisterOperation* operation,
69                                       const std::string& action,
70                                       FailureReason reason,
71                                       int printer_http_code,
72                                       const DictionaryValue* json) = 0;
73
74    // Called when the registration is done.
75    virtual void OnPrivetRegisterDone(PrivetRegisterOperation* operation,
76                                      const std::string& device_id) = 0;
77  };
78
79  virtual ~PrivetRegisterOperation() {}
80
81  virtual void Start() = 0;
82  // Owner SHOULD call explicitly before destroying operation.
83  virtual void Cancel() = 0;
84  virtual void CompleteRegistration() = 0;
85
86  virtual PrivetHTTPClient* GetHTTPClient() = 0;
87};
88
89// Privet HTTP client. Must not outlive the operations it creates.
90class PrivetHTTPClient {
91 public:
92  virtual ~PrivetHTTPClient() {}
93  virtual const base::DictionaryValue* GetCachedInfo() const = 0;
94
95  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
96      const std::string& user,
97      PrivetRegisterOperation::Delegate* delegate) = 0;
98  virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
99      PrivetInfoOperation::Delegate* delegate) = 0;
100
101  // A name for the HTTP client, e.g. the device name for the privet device.
102  virtual const std::string& GetName() = 0;
103};
104
105}  // namespace local_discovery
106#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
107