privet_url_fetcher.h revision 116680a4aac90f2aa7413d9095a592090648e557
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_URL_FETCHER_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
7
8#include <string>
9
10#include "base/file_util.h"
11#include "base/memory/weak_ptr.h"
12#include "base/values.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#include "url/gurl.h"
17
18namespace base {
19class FilePath;
20}
21
22namespace local_discovery {
23
24const int kPrivetHTTPCodeInternalFailure = -1;
25
26// Privet-specific URLFetcher adapter. Currently supports only the subset
27// of HTTP features required by Privet for GCP 1.5
28// (/privet/info and /privet/register).
29class PrivetURLFetcher : public net::URLFetcherDelegate {
30 public:
31  enum ErrorType {
32    JSON_PARSE_ERROR,
33    URL_FETCH_ERROR,
34    RESPONSE_CODE_ERROR,
35    RETRY_ERROR,
36    TOKEN_ERROR
37  };
38
39  typedef base::Callback<void(const std::string& /*token*/)> TokenCallback;
40
41  class Delegate {
42   public:
43    virtual ~Delegate() {}
44
45    // If you do not implement this method, you will always get a
46    // TOKEN_ERROR error when your token is invalid.
47    virtual void OnNeedPrivetToken(
48        PrivetURLFetcher* fetcher,
49        const TokenCallback& callback);
50    virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0;
51    virtual void OnParsedJson(PrivetURLFetcher* fetcher,
52                              const base::DictionaryValue& value,
53                              bool has_error) = 0;
54
55    // If this method is returns true, the data will not be parsed as JSON, and
56    // |OnParsedJson| will not be called. Otherwise, |OnParsedJson| will be
57    // called.
58    virtual bool OnRawData(PrivetURLFetcher* fetcher,
59                           bool response_is_file,
60                           const std::string& data_string,
61                           const base::FilePath& data_file);
62  };
63
64  PrivetURLFetcher(
65      const GURL& url,
66      net::URLFetcher::RequestType request_type,
67      net::URLRequestContextGetter* request_context,
68      Delegate* delegate);
69
70  virtual ~PrivetURLFetcher();
71
72  // net::URLFetcherDelegate methods.
73  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
74
75  static void SetTokenForHost(const std::string& host,
76                              const std::string& token);
77
78  static void ResetTokenMapForTests();
79
80  void DoNotRetryOnTransientError();
81
82  void SendEmptyPrivetToken();
83
84  // Set the contents of the Range header. |OnRawData| must return true if this
85  // is called.
86  void SetByteRange(int start, int end);
87
88  // Save the response to a file. |OnRawData| must return true if this is
89  // called.
90  void SaveResponseToFile();
91
92  void Start();
93
94  void SetUploadData(const std::string& upload_content_type,
95                     const std::string& upload_data);
96
97  void SetUploadFilePath(const std::string& upload_content_type,
98                         const base::FilePath& upload_file_path);
99
100  const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
101  int response_code() const { return url_fetcher_->GetResponseCode(); }
102
103 private:
104  void OnURLFetchCompleteParseData(const net::URLFetcher* source);
105  bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source);
106
107  std::string GetHostString();  // Get string representing the host.
108  std::string GetPrivetAccessToken();
109  void Try();
110  void ScheduleRetry(int timeout_seconds);
111  bool PrivetErrorTransient(const std::string& error);
112  void RequestTokenRefresh();
113  void RefreshToken(const std::string& token);
114
115  GURL url_;
116  net::URLFetcher::RequestType request_type_;
117  scoped_refptr<net::URLRequestContextGetter> request_context_;
118  Delegate* delegate_;
119
120  bool do_not_retry_on_transient_error_;
121  bool send_empty_privet_token_;
122  bool has_byte_range_;
123  bool make_response_file_;
124
125  int byte_range_start_;
126  int byte_range_end_;
127
128  int tries_;
129  std::string upload_data_;
130  std::string upload_content_type_;
131  base::FilePath upload_file_path_;
132  scoped_ptr<net::URLFetcher> url_fetcher_;
133
134  base::WeakPtrFactory<PrivetURLFetcher> weak_factory_;
135  DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
136};
137
138}  // namespace local_discovery
139
140#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
141