Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_url_fetcher.h
blob698ca09208d151afd010c7ecfb10efc43c5db098
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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
8 #include <string>
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"
18 namespace base {
19 class FilePath;
22 namespace local_discovery {
24 const int kPrivetHTTPCodeInternalFailure = -1;
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).
29 class 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
39 typedef base::Callback<void(const std::string& /*token*/)> TokenCallback;
41 class Delegate {
42 public:
43 virtual ~Delegate() {}
45 // If you do not implement this method for PrivetV1 callers, you will always
46 // get a TOKEN_ERROR error when your token is invalid.
47 virtual void OnNeedPrivetToken(
48 PrivetURLFetcher* fetcher,
49 const TokenCallback& callback);
51 // If this returns the empty string, will not send an auth token.
52 virtual std::string GetAuthToken();
54 virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0;
55 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
56 const base::DictionaryValue& value,
57 bool has_error) = 0;
59 // If this method is returns true, the data will not be parsed as JSON, and
60 // |OnParsedJson| will not be called. Otherwise, |OnParsedJson| will be
61 // called.
62 virtual bool OnRawData(PrivetURLFetcher* fetcher,
63 bool response_is_file,
64 const std::string& data_string,
65 const base::FilePath& data_file);
68 PrivetURLFetcher(
69 const GURL& url,
70 net::URLFetcher::RequestType request_type,
71 net::URLRequestContextGetter* request_context,
72 Delegate* delegate);
74 virtual ~PrivetURLFetcher();
76 // net::URLFetcherDelegate methods.
77 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
79 static void SetTokenForHost(const std::string& host,
80 const std::string& token);
82 static void ResetTokenMapForTests();
84 void DoNotRetryOnTransientError();
86 void SendEmptyPrivetToken();
88 void V3Mode();
90 // Set the contents of the Range header. |OnRawData| must return true if this
91 // is called.
92 void SetByteRange(int start, int end);
94 // Save the response to a file. |OnRawData| must return true if this is
95 // called.
96 void SaveResponseToFile();
98 void Start();
100 void SetUploadData(const std::string& upload_content_type,
101 const std::string& upload_data);
103 void SetUploadFilePath(const std::string& upload_content_type,
104 const base::FilePath& upload_file_path);
106 const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
107 int response_code() const { return url_fetcher_->GetResponseCode(); }
109 private:
110 void OnURLFetchCompleteParseData(const net::URLFetcher* source);
111 bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source);
113 std::string GetHostString(); // Get string representing the host.
114 std::string GetPrivetAccessToken();
115 void Try();
116 void ScheduleRetry(int timeout_seconds);
117 bool PrivetErrorTransient(const std::string& error);
118 void RequestTokenRefresh();
119 void RefreshToken(const std::string& token);
121 GURL url_;
122 net::URLFetcher::RequestType request_type_;
123 scoped_refptr<net::URLRequestContextGetter> request_context_;
124 Delegate* delegate_;
126 bool do_not_retry_on_transient_error_;
127 bool send_empty_privet_token_;
128 bool has_byte_range_;
129 bool make_response_file_;
130 bool v3_mode_;
132 int byte_range_start_;
133 int byte_range_end_;
135 int tries_;
136 std::string upload_data_;
137 std::string upload_content_type_;
138 base::FilePath upload_file_path_;
139 scoped_ptr<net::URLFetcher> url_fetcher_;
141 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_;
142 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
145 } // namespace local_discovery
147 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_