OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ |
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ | 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // If this method is returns true, the data will not be parsed as JSON, and | 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 | 56 // |OnParsedJson| will not be called. Otherwise, |OnParsedJson| will be |
57 // called. | 57 // called. |
58 virtual bool OnRawData(PrivetURLFetcher* fetcher, | 58 virtual bool OnRawData(PrivetURLFetcher* fetcher, |
59 bool response_is_file, | 59 bool response_is_file, |
60 const std::string& data_string, | 60 const std::string& data_string, |
61 const base::FilePath& data_file); | 61 const base::FilePath& data_file); |
62 }; | 62 }; |
63 | 63 |
64 PrivetURLFetcher( | 64 PrivetURLFetcher( |
65 const std::string& token, | |
66 const GURL& url, | 65 const GURL& url, |
67 net::URLFetcher::RequestType request_type, | 66 net::URLFetcher::RequestType request_type, |
68 net::URLRequestContextGetter* request_context, | 67 net::URLRequestContextGetter* request_context, |
69 Delegate* delegate); | 68 Delegate* delegate); |
| 69 |
70 virtual ~PrivetURLFetcher(); | 70 virtual ~PrivetURLFetcher(); |
71 | 71 |
| 72 static void SetTokenForHost(const std::string& host, |
| 73 const std::string& token); |
| 74 |
| 75 static void ResetTokenMapForTests(); |
| 76 |
72 void DoNotRetryOnTransientError(); | 77 void DoNotRetryOnTransientError(); |
73 | 78 |
74 void AllowEmptyPrivetToken(); | 79 void AllowEmptyPrivetToken(); |
75 | 80 |
76 // Set the contents of the Range header. |OnRawData| must return true if this | 81 // Set the contents of the Range header. |OnRawData| must return true if this |
77 // is called. | 82 // is called. |
78 void SetByteRange(int start, int end); | 83 void SetByteRange(int start, int end); |
79 | 84 |
80 // Save the response to a file. |OnRawData| must return true if this is | 85 // Save the response to a file. |OnRawData| must return true if this is |
81 // called. | 86 // called. |
82 void SaveResponseToFile(); | 87 void SaveResponseToFile(); |
83 | 88 |
84 void Start(); | 89 void Start(); |
85 | 90 |
86 void SetUploadData(const std::string& upload_content_type, | 91 void SetUploadData(const std::string& upload_content_type, |
87 const std::string& upload_data); | 92 const std::string& upload_data); |
88 | 93 |
89 void SetUploadFilePath(const std::string& upload_content_type, | 94 void SetUploadFilePath(const std::string& upload_content_type, |
90 const base::FilePath& upload_file_path); | 95 const base::FilePath& upload_file_path); |
91 | 96 |
92 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 97 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
93 void OnURLFetchCompleteParseData(const net::URLFetcher* source); | 98 void OnURLFetchCompleteParseData(const net::URLFetcher* source); |
94 bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source); | 99 bool OnURLFetchCompleteDoNotParseData(const net::URLFetcher* source); |
95 | 100 |
96 const GURL& url() const { return url_fetcher_->GetOriginalURL(); } | 101 const GURL& url() const { return url_fetcher_->GetOriginalURL(); } |
97 int response_code() const { return url_fetcher_->GetResponseCode(); } | 102 int response_code() const { return url_fetcher_->GetResponseCode(); } |
98 | 103 |
99 private: | 104 private: |
| 105 std::string GetHostString(); // Get string representing the host. |
| 106 std::string GetPrivetAccessToken(); |
100 void Try(); | 107 void Try(); |
101 void ScheduleRetry(int timeout_seconds); | 108 void ScheduleRetry(int timeout_seconds); |
102 bool PrivetErrorTransient(const std::string& error); | 109 bool PrivetErrorTransient(const std::string& error); |
103 void RequestTokenRefresh(); | 110 void RequestTokenRefresh(); |
104 void RefreshToken(const std::string& token); | 111 void RefreshToken(const std::string& token); |
105 | 112 |
106 std::string privet_access_token_; | |
107 GURL url_; | 113 GURL url_; |
108 net::URLFetcher::RequestType request_type_; | 114 net::URLFetcher::RequestType request_type_; |
109 scoped_refptr<net::URLRequestContextGetter> request_context_; | 115 scoped_refptr<net::URLRequestContextGetter> request_context_; |
110 Delegate* delegate_; | 116 Delegate* delegate_; |
111 | 117 |
112 bool do_not_retry_on_transient_error_; | 118 bool do_not_retry_on_transient_error_; |
113 bool allow_empty_privet_token_; | 119 bool allow_empty_privet_token_; |
114 bool has_byte_range_; | 120 bool has_byte_range_; |
115 bool make_response_file_; | 121 bool make_response_file_; |
116 | 122 |
117 int byte_range_start_; | 123 int byte_range_start_; |
118 int byte_range_end_; | 124 int byte_range_end_; |
119 | 125 |
120 int tries_; | 126 int tries_; |
121 std::string upload_data_; | 127 std::string upload_data_; |
122 std::string upload_content_type_; | 128 std::string upload_content_type_; |
123 base::FilePath upload_file_path_; | 129 base::FilePath upload_file_path_; |
124 scoped_ptr<net::URLFetcher> url_fetcher_; | 130 scoped_ptr<net::URLFetcher> url_fetcher_; |
125 | 131 |
126 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_; | 132 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_; |
127 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher); | 133 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher); |
128 }; | 134 }; |
129 | 135 |
130 class PrivetURLFetcherFactory { | |
131 public: | |
132 explicit PrivetURLFetcherFactory( | |
133 net::URLRequestContextGetter* request_context); | |
134 ~PrivetURLFetcherFactory(); | |
135 | |
136 scoped_ptr<PrivetURLFetcher> CreateURLFetcher( | |
137 const GURL& url, | |
138 net::URLFetcher::RequestType request_type, | |
139 PrivetURLFetcher::Delegate* delegate) const; | |
140 | |
141 void set_token(const std::string& token) { token_ = token; } | |
142 const std::string& get_token() const { return token_; } | |
143 | |
144 private: | |
145 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
146 std::string token_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory); | |
149 }; | |
150 | |
151 } // namespace local_discovery | 136 } // namespace local_discovery |
152 | 137 |
153 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ | 138 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_ |
OLD | NEW |