BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_url_fetcher.h
blobb468c24b55b28a009be4101f19d37ac654fd5ce7
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/files/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 // Privet-specific URLFetcher adapter. Currently supports only the subset
25 // of HTTP features required by Privet for GCP 1.5
26 // (/privet/info and /privet/register).
27 class PrivetURLFetcher : public net::URLFetcherDelegate {
28 public:
29 enum ErrorType {
30 JSON_PARSE_ERROR,
31 URL_FETCH_ERROR,
32 RESPONSE_CODE_ERROR,
33 RETRY_ERROR,
34 TOKEN_ERROR
37 typedef base::Callback<void(const std::string& /*token*/)> TokenCallback;
39 class Delegate {
40 public:
41 virtual ~Delegate() {}
43 // If you do not implement this method for PrivetV1 callers, you will always
44 // get a TOKEN_ERROR error when your token is invalid.
45 virtual void OnNeedPrivetToken(
46 PrivetURLFetcher* fetcher,
47 const TokenCallback& callback);
49 // If this returns the empty string, will not send an auth token.
50 virtual std::string GetAuthToken();
52 virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0;
53 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
54 const base::DictionaryValue& value,
55 bool has_error) = 0;
57 // If this method is returns true, the data will not be parsed as JSON, and
58 // |OnParsedJson| will not be called. Otherwise, |OnParsedJson| will be
59 // called.
60 virtual bool OnRawData(PrivetURLFetcher* fetcher,
61 bool response_is_file,
62 const std::string& data_string,
63 const base::FilePath& data_file);
66 PrivetURLFetcher(
67 const GURL& url,
68 net::URLFetcher::RequestType request_type,
69 net::URLRequestContextGetter* request_context,
70 Delegate* delegate);
72 ~PrivetURLFetcher() override;
74 // net::URLFetcherDelegate methods.
75 void OnURLFetchComplete(const net::URLFetcher* source) override;
77 static void SetTokenForHost(const std::string& host,
78 const std::string& token);
80 static void ResetTokenMapForTests();
82 void DoNotRetryOnTransientError();
84 void SendEmptyPrivetToken();
86 void V3Mode();
88 // Set the contents of the Range header. |OnRawData| must return true if this
89 // is called.
90 void SetByteRange(int start, int end);
92 // Save the response to a file. |OnRawData| must return true if this is
93 // called.
94 void SaveResponseToFile();
96 void Start();
98 void SetUploadData(const std::string& upload_content_type,
99 const std::string& upload_data);
101 void SetUploadFilePath(const std::string& upload_content_type,
102 const base::FilePath& upload_file_path);
104 const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
105 int response_code() const { return url_fetcher_->GetResponseCode(); }
107 private:
108 void OnURLFetchCompleteParseData(const net::URLFetcher* source);
109 bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source);
111 std::string GetHostString(); // Get string representing the host.
112 std::string GetPrivetAccessToken();
113 void Try();
114 void ScheduleRetry(int timeout_seconds);
115 bool PrivetErrorTransient(const std::string& error);
116 void RequestTokenRefresh();
117 void RefreshToken(const std::string& token);
119 GURL url_;
120 net::URLFetcher::RequestType request_type_;
121 scoped_refptr<net::URLRequestContextGetter> request_context_;
122 Delegate* delegate_;
124 bool do_not_retry_on_transient_error_;
125 bool send_empty_privet_token_;
126 bool has_byte_range_;
127 bool make_response_file_;
128 bool v3_mode_;
130 int byte_range_start_;
131 int byte_range_end_;
133 int tries_;
134 std::string upload_data_;
135 std::string upload_content_type_;
136 base::FilePath upload_file_path_;
137 scoped_ptr<net::URLFetcher> url_fetcher_;
139 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_;
140 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
143 } // namespace local_discovery
145 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_