privet_http_impl.h revision 58537e28ecd584eab876aee8be7156509866d23a
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_IMPL_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/weak_ptr.h"
12#include "chrome/browser/local_discovery/privet_http.h"
13
14namespace local_discovery {
15
16class PrivetHTTPClientImpl;
17
18class PrivetInfoOperationImpl : public PrivetInfoOperation,
19                                public PrivetURLFetcher::Delegate {
20 public:
21  PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client,
22                          PrivetInfoOperation::Delegate* delegate);
23  virtual ~PrivetInfoOperationImpl();
24
25  virtual void Start() OVERRIDE;
26
27  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
28
29  virtual void OnError(PrivetURLFetcher* fetcher,
30                       PrivetURLFetcher::ErrorType error) OVERRIDE;
31  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
32                            const base::DictionaryValue* value,
33                            bool has_error) OVERRIDE;
34
35 private:
36  PrivetHTTPClientImpl* privet_client_;
37  PrivetInfoOperation::Delegate* delegate_;
38  scoped_ptr<PrivetURLFetcher> url_fetcher_;
39};
40
41class PrivetRegisterOperationImpl
42    : public PrivetRegisterOperation,
43      public PrivetURLFetcher::Delegate,
44      public PrivetInfoOperation::Delegate,
45      public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
46 public:
47  PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client,
48                              const std::string& user,
49                              PrivetRegisterOperation::Delegate* delegate);
50  virtual ~PrivetRegisterOperationImpl();
51
52  virtual void Start() OVERRIDE;
53  virtual void Cancel() OVERRIDE;
54  virtual void CompleteRegistration() OVERRIDE;
55
56  virtual void OnError(PrivetURLFetcher* fetcher,
57                       PrivetURLFetcher::ErrorType error) OVERRIDE;
58
59  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
60                            const base::DictionaryValue* value,
61                            bool has_error) OVERRIDE;
62
63  virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
64                                int http_code,
65                                const base::DictionaryValue* value) OVERRIDE;
66
67  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
68 private:
69  class Cancelation : public PrivetURLFetcher::Delegate {
70   public:
71    Cancelation(PrivetHTTPClientImpl* privet_client,
72                const std::string& user);
73    virtual ~Cancelation();
74
75    virtual void OnError(PrivetURLFetcher* fetcher,
76                         PrivetURLFetcher::ErrorType error) OVERRIDE;
77
78    virtual void OnParsedJson(PrivetURLFetcher* fetcher,
79                              const base::DictionaryValue* value,
80                              bool has_error) OVERRIDE;
81
82    void Cleanup();
83
84   private:
85    scoped_ptr<PrivetURLFetcher> url_fetcher_;
86  };
87
88  // Arguments is JSON value from request.
89  typedef base::Callback<void(const base::DictionaryValue&)>
90      ResponseHandler;
91
92  void StartResponse(const base::DictionaryValue& value);
93  void GetClaimTokenResponse(const base::DictionaryValue& value);
94  void CompleteResponse(const base::DictionaryValue& value);
95
96  void StartInfoOperation();
97
98  void SendRequest(const std::string& action);
99
100  bool PrivetErrorTransient(const std::string& error);
101
102  static GURL GetURLForActionAndUser(
103      PrivetHTTPClientImpl* privet_client,
104      const std::string& action,
105      const std::string& user);
106
107  std::string user_;
108  std::string current_action_;
109  scoped_ptr<PrivetURLFetcher> url_fetcher_;
110  PrivetRegisterOperation::Delegate* delegate_;
111  PrivetHTTPClientImpl* privet_client_;
112  ResponseHandler next_response_handler_;
113  // Required to ensure destroying completed register operations doesn't cause
114  // extraneous cancelations.
115  bool ongoing_;
116  scoped_ptr<PrivetInfoOperation> info_operation_;
117};
118
119class PrivetHTTPClientImpl : public PrivetHTTPClient {
120 public:
121  PrivetHTTPClientImpl(
122      const std::string& name,
123      const net::HostPortPair& host_port,
124      net::URLRequestContextGetter* request_context);
125  virtual ~PrivetHTTPClientImpl();
126
127  virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
128
129  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
130      const std::string& user,
131      PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
132
133  virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
134      PrivetInfoOperation::Delegate* delegate) OVERRIDE;
135
136  virtual const std::string& GetName() OVERRIDE;
137
138  const PrivetURLFetcherFactory& fetcher_factory() const {
139    return fetcher_factory_;
140  }
141  const net::HostPortPair& host_port() const { return host_port_; }
142
143  void CacheInfo(const base::DictionaryValue* cached_info);
144
145 private:
146  std::string name_;
147  PrivetURLFetcherFactory fetcher_factory_;
148  net::HostPortPair host_port_;
149  scoped_ptr<base::DictionaryValue> cached_info_;
150};
151
152}  // namespace local_discovery
153#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
154