| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/copresence/rpc/http_post.h" | 5 #include "components/copresence/rpc/http_post.h" |
| 6 | 6 |
| 7 #include <google/protobuf/message_lite.h> | 7 #include <google/protobuf/message_lite.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kTracingTokenField[] = "trace"; | 24 const char kTracingTokenField[] = "trace"; |
| 25 const char kApiKeyField[] = "key"; | 25 const char kApiKeyField[] = "key"; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | 29 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| 30 const std::string& server_host, | 30 const std::string& server_host, |
| 31 const std::string& rpc_name, | 31 const std::string& rpc_name, |
| 32 const google::protobuf::MessageLite& request_proto, | 32 const google::protobuf::MessageLite& request_proto) { |
| 33 const ResponseCallback& response_callback) | |
| 34 : response_callback_(response_callback) { | |
| 35 // Create the base URL to call. | 33 // Create the base URL to call. |
| 36 GURL url(server_host + "/" + rpc_name); | 34 GURL url(server_host + "/" + rpc_name); |
| 37 | 35 |
| 38 // Add the Chrome API key. | 36 // Add the Chrome API key. |
| 39 // TODO(ckehoe): Replace this with an app-specific key | 37 // TODO(ckehoe): Replace this with an app-specific key |
| 40 // once the server API supports it. | 38 // once the server API supports it. |
| 41 DCHECK(google_apis::HasKeysConfigured()); | 39 DCHECK(google_apis::HasKeysConfigured()); |
| 42 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); | 40 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); |
| 43 | 41 |
| 44 // Add the tracing token, if specified. | 42 // Add the tracing token, if specified. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 58 // Configure and send the request. | 56 // Configure and send the request. |
| 59 url_fetcher_.reset(net::URLFetcher::Create( | 57 url_fetcher_.reset(net::URLFetcher::Create( |
| 60 kUrlFetcherId, url, net::URLFetcher::POST, this)); | 58 kUrlFetcherId, url, net::URLFetcher::POST, this)); |
| 61 url_fetcher_->SetRequestContext(url_context_getter); | 59 url_fetcher_->SetRequestContext(url_context_getter); |
| 62 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 60 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 63 net::LOAD_DISABLE_CACHE | | 61 net::LOAD_DISABLE_CACHE | |
| 64 net::LOAD_DO_NOT_SAVE_COOKIES | | 62 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 65 net::LOAD_DO_NOT_SEND_COOKIES | | 63 net::LOAD_DO_NOT_SEND_COOKIES | |
| 66 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 64 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 67 url_fetcher_->SetUploadData("application/x-protobuf", request_data); | 65 url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| 66 } |
| 67 |
| 68 void HttpPost::Start(const ResponseCallback& response_callback) { |
| 69 response_callback_ = response_callback; |
| 68 url_fetcher_->Start(); | 70 url_fetcher_->Start(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 HttpPost::~HttpPost() {} | 73 HttpPost::~HttpPost() {} |
| 72 | 74 |
| 73 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { | 75 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { |
| 74 DCHECK_EQ(url_fetcher_.get(), source); | 76 DCHECK_EQ(url_fetcher_.get(), source); |
| 75 | 77 |
| 76 // Gather response info. | 78 // Gather response info. |
| 77 std::string response; | 79 std::string response; |
| 78 source->GetResponseAsString(&response); | 80 source->GetResponseAsString(&response); |
| 79 int response_code = source->GetResponseCode(); | 81 int response_code = source->GetResponseCode(); |
| 80 | 82 |
| 81 // Log any errors. | 83 // Log any errors. |
| 82 if (response_code < 0) { | 84 if (response_code < 0) { |
| 83 LOG(ERROR) << "Couldn't contact the Copresence server at " | 85 LOG(ERROR) << "Couldn't contact the Copresence server at " |
| 84 << source->GetURL(); | 86 << source->GetURL(); |
| 85 } else if (response_code != net::HTTP_OK) { | 87 } else if (response_code != net::HTTP_OK) { |
| 86 LOG(ERROR) << "Copresence request got HTTP response code " << response_code | 88 LOG(ERROR) << "Copresence request got HTTP response code " << response_code |
| 87 << ". Response:\n" << response; | 89 << ". Response:\n" << response; |
| 88 } | 90 } |
| 89 | 91 |
| 90 // Return the response. | 92 // Return the response. |
| 91 response_callback_.Run(response_code, response, this); | 93 response_callback_.Run(response_code, response); |
| 92 } | 94 } |
| 93 | 95 |
| 94 } // namespace copresence | 96 } // namespace copresence |
| OLD | NEW |