privet_http_impl.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 void OnError(PrivetURLFetcher* fetcher,
28                       PrivetURLFetcher::ErrorType error) OVERRIDE;
29  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
30                            const base::DictionaryValue* value,
31                            bool has_error) OVERRIDE;
32
33 private:
34  PrivetHTTPClientImpl* privet_client_;
35  PrivetInfoOperation::Delegate* delegate_;
36  scoped_ptr<PrivetURLFetcher> url_fetcher_;
37};
38
39class PrivetRegisterOperationImpl
40    : public PrivetRegisterOperation,
41      public PrivetURLFetcher::Delegate,
42      public PrivetInfoOperation::Delegate,
43      public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
44 public:
45  PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client,
46                              const std::string& user,
47                              PrivetRegisterOperation::Delegate* delegate);
48  virtual ~PrivetRegisterOperationImpl();
49
50  virtual void Start() OVERRIDE;
51  virtual void Cancel() OVERRIDE;
52  virtual void CompleteRegistration() OVERRIDE;
53
54  virtual void OnError(PrivetURLFetcher* fetcher,
55                       PrivetURLFetcher::ErrorType error) OVERRIDE;
56
57  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
58                            const base::DictionaryValue* value,
59                            bool has_error) OVERRIDE;
60
61  virtual void OnPrivetInfoDone(int http_code,
62                                const base::DictionaryValue* value) OVERRIDE;
63 private:
64  // Arguments is JSON value from request.
65  typedef base::Callback<void(const base::DictionaryValue&)>
66      ResponseHandler;
67
68  void StartResponse(const base::DictionaryValue& value);
69  void GetClaimTokenResponse(const base::DictionaryValue& value);
70  void CompleteResponse(const base::DictionaryValue& value);
71
72  void StartInfoOperation();
73
74  void SendRequest(const std::string& action);
75
76  bool PrivetErrorTransient(const std::string& error);
77
78  std::string user_;
79  std::string current_action_;
80  scoped_ptr<PrivetURLFetcher> url_fetcher_;
81  PrivetRegisterOperation::Delegate* delegate_;
82  PrivetHTTPClientImpl* privet_client_;
83  ResponseHandler next_response_handler_;
84  // Required to ensure destroying completed register operations doesn't cause
85  // extraneous cancelations.
86  bool ongoing_;
87  scoped_ptr<PrivetInfoOperation> info_operation_;
88};
89
90class PrivetHTTPClientImpl : public PrivetHTTPClient {
91 public:
92  PrivetHTTPClientImpl(const net::HostPortPair& host_port,
93                       net::URLRequestContextGetter* request_context);
94  virtual ~PrivetHTTPClientImpl();
95
96  virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
97
98  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
99      const std::string& user,
100      PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
101
102  virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
103      PrivetInfoOperation::Delegate* delegate) OVERRIDE;
104
105  const PrivetURLFetcherFactory& fetcher_factory() const {
106    return fetcher_factory_;
107  }
108  const net::HostPortPair& host_port() const { return host_port_; }
109
110  void CacheInfo(const base::DictionaryValue* cached_info);
111
112 private:
113  PrivetURLFetcherFactory fetcher_factory_;
114  net::HostPortPair host_port_;
115  scoped_ptr<base::DictionaryValue> cached_info_;
116};
117
118}  // namespace local_discovery
119#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
120