OLD | NEW |
| (Empty) |
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/files/file_util.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/values.h" | |
14 #include "net/url_request/url_fetcher.h" | |
15 #include "net/url_request/url_fetcher_delegate.h" | |
16 #include "net/url_request/url_request_context_getter.h" | |
17 #include "url/gurl.h" | |
18 | |
19 namespace base { | |
20 class FilePath; | |
21 } | |
22 | |
23 namespace local_discovery { | |
24 | |
25 // Privet-specific URLFetcher adapter. Currently supports only the subset | |
26 // of HTTP features required by Privet for GCP 1.5 | |
27 // (/privet/info and /privet/register). | |
28 class PrivetURLFetcher : public net::URLFetcherDelegate { | |
29 public: | |
30 enum ErrorType { | |
31 JSON_PARSE_ERROR, | |
32 REQUEST_CANCELED, | |
33 RESPONSE_CODE_ERROR, | |
34 TOKEN_ERROR, | |
35 UNKNOWN_ERROR, | |
36 }; | |
37 | |
38 typedef base::Callback<void(const std::string& /*token*/)> TokenCallback; | |
39 | |
40 class Delegate { | |
41 public: | |
42 virtual ~Delegate() {} | |
43 | |
44 // If you do not implement this method for PrivetV1 callers, you will always | |
45 // get a TOKEN_ERROR error when your token is invalid. | |
46 virtual void OnNeedPrivetToken( | |
47 PrivetURLFetcher* fetcher, | |
48 const TokenCallback& callback); | |
49 | |
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 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | |
68 Delegate* delegate); | |
69 | |
70 ~PrivetURLFetcher() override; | |
71 | |
72 // net::URLFetcherDelegate methods. | |
73 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 SetMaxRetries(int max_retries); | |
81 | |
82 void DoNotRetryOnTransientError(); | |
83 | |
84 void SendEmptyPrivetToken(); | |
85 | |
86 // Set the contents of the Range header. |OnRawData| must return true if this | |
87 // is called. | |
88 void SetByteRange(int start, int end); | |
89 | |
90 // Save the response to a file. |OnRawData| must return true if this is | |
91 // called. | |
92 void SaveResponseToFile(); | |
93 | |
94 void Start(); | |
95 | |
96 void SetUploadData(const std::string& upload_content_type, | |
97 const std::string& upload_data); | |
98 | |
99 void SetUploadFilePath(const std::string& upload_content_type, | |
100 const base::FilePath& upload_file_path); | |
101 | |
102 const GURL& url() const { | |
103 return url_fetcher_ ? url_fetcher_->GetOriginalURL() : url_; | |
104 } | |
105 int response_code() const { | |
106 return url_fetcher_ ? url_fetcher_->GetResponseCode() : -1; | |
107 } | |
108 | |
109 private: | |
110 void OnURLFetchCompleteParseData(const net::URLFetcher* source); | |
111 bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source); | |
112 | |
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); | |
120 | |
121 const GURL url_; | |
122 net::URLFetcher::RequestType request_type_; | |
123 scoped_refptr<net::URLRequestContextGetter> context_getter_; | |
124 Delegate* delegate_; | |
125 | |
126 int max_retries_; | |
127 bool do_not_retry_on_transient_error_; | |
128 bool send_empty_privet_token_; | |
129 bool has_byte_range_; | |
130 bool make_response_file_; | |
131 | |
132 int byte_range_start_; | |
133 int byte_range_end_; | |
134 | |
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_; | |
140 | |
141 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_; | |
142 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher); | |
143 }; | |
144 | |
145 } // namespace local_discovery | |
146 | |
147 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ | |
OLD | NEW |