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 #include "chrome/browser/local_discovery/privet_url_fetcher.h" | 5 #include "chrome/browser/local_discovery/privet_url_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/local_discovery/privet_constants.h" | 12 #include "chrome/browser/local_discovery/privet_constants.h" |
| 13 #include "content/public/browser/browser_thread.h" |
13 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
14 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
15 | 16 |
16 namespace local_discovery { | 17 namespace local_discovery { |
17 | 18 |
18 namespace { | 19 namespace { |
19 const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; | 20 const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; |
20 const char kXPrivetEmptyToken[] = "\"\""; | 21 const char kXPrivetEmptyToken[] = "\"\""; |
21 const int kPrivetMaxRetries = 20; | 22 const int kPrivetMaxRetries = 20; |
22 const int kPrivetTimeoutOnError = 5; | 23 const int kPrivetTimeoutOnError = 5; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 59 |
59 if (token.empty()) | 60 if (token.empty()) |
60 token = kXPrivetEmptyToken; | 61 token = kXPrivetEmptyToken; |
61 | 62 |
62 url_fetcher_.reset(net::URLFetcher::Create(url_, request_type_, this)); | 63 url_fetcher_.reset(net::URLFetcher::Create(url_, request_type_, this)); |
63 url_fetcher_->SetRequestContext(request_context_); | 64 url_fetcher_->SetRequestContext(request_context_); |
64 url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) + | 65 url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) + |
65 token); | 66 token); |
66 | 67 |
67 // URLFetcher requires us to set upload data for POST requests. | 68 // URLFetcher requires us to set upload data for POST requests. |
68 if (request_type_ == net::URLFetcher::POST) | 69 if (request_type_ == net::URLFetcher::POST) { |
69 url_fetcher_->SetUploadData(upload_content_type_, upload_data_); | 70 if (!upload_file_path_.empty()) { |
| 71 url_fetcher_->SetUploadFilePath( |
| 72 upload_content_type_, |
| 73 upload_file_path_, |
| 74 0 /*offset*/, |
| 75 kuint64max /*length*/, |
| 76 content::BrowserThread::GetMessageLoopProxyForThread( |
| 77 content::BrowserThread::FILE)); |
| 78 } else { |
| 79 url_fetcher_->SetUploadData(upload_content_type_, upload_data_); |
| 80 } |
| 81 } |
70 | 82 |
71 url_fetcher_->Start(); | 83 url_fetcher_->Start(); |
72 } else { | 84 } else { |
73 delegate_->OnError(this, RETRY_ERROR); | 85 delegate_->OnError(this, RETRY_ERROR); |
74 } | 86 } |
75 } | 87 } |
76 | 88 |
77 void PrivetURLFetcher::Start() { | 89 void PrivetURLFetcher::Start() { |
78 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. | 90 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. |
79 | 91 |
80 if (privet_access_token_.empty() && !allow_empty_privet_token_) { | 92 if (privet_access_token_.empty() && !allow_empty_privet_token_) { |
81 RequestTokenRefresh(); | 93 RequestTokenRefresh(); |
82 } else { | 94 } else { |
83 Try(); | 95 Try(); |
84 } | 96 } |
85 } | 97 } |
86 | 98 |
87 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, | 99 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, |
88 const std::string& upload_data) { | 100 const std::string& upload_data) { |
| 101 DCHECK(upload_file_path_.empty()); |
89 upload_content_type_ = upload_content_type; | 102 upload_content_type_ = upload_content_type; |
90 upload_data_ = upload_data; | 103 upload_data_ = upload_data; |
91 } | 104 } |
92 | 105 |
| 106 void PrivetURLFetcher::SetUploadFilePath( |
| 107 const std::string& upload_content_type, |
| 108 const base::FilePath& upload_file_path) { |
| 109 DCHECK(upload_data_.empty()); |
| 110 upload_content_type_ = upload_content_type; |
| 111 upload_file_path_ = upload_file_path; |
| 112 } |
| 113 |
93 void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 114 void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
94 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | 115 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
95 source->GetResponseCode() == net::HTTP_SERVICE_UNAVAILABLE) { | 116 source->GetResponseCode() == net::HTTP_SERVICE_UNAVAILABLE) { |
96 ScheduleRetry(kPrivetTimeoutOnError); | 117 ScheduleRetry(kPrivetTimeoutOnError); |
97 return; | 118 return; |
98 } | 119 } |
99 | 120 |
100 if (source->GetResponseCode() != net::HTTP_OK) { | 121 if (source->GetResponseCode() != net::HTTP_OK) { |
101 delegate_->OnError(this, RESPONSE_CODE_ERROR); | 122 delegate_->OnError(this, RESPONSE_CODE_ERROR); |
102 return; | 123 return; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 214 |
194 scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher( | 215 scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher( |
195 const GURL& url, net::URLFetcher::RequestType request_type, | 216 const GURL& url, net::URLFetcher::RequestType request_type, |
196 PrivetURLFetcher::Delegate* delegate) const { | 217 PrivetURLFetcher::Delegate* delegate) const { |
197 return scoped_ptr<PrivetURLFetcher>( | 218 return scoped_ptr<PrivetURLFetcher>( |
198 new PrivetURLFetcher(token_, url, request_type, request_context_.get(), | 219 new PrivetURLFetcher(token_, url, request_type, request_context_.get(), |
199 delegate)); | 220 delegate)); |
200 } | 221 } |
201 | 222 |
202 } // namespace local_discovery | 223 } // namespace local_discovery |
OLD | NEW |