| 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 // TODO(ckehoe): Support third-party protobufs too. | 7 // TODO(ckehoe): Support third-party protobufs too. |
| 8 #include <google/protobuf/message_lite.h> | 8 #include <google/protobuf/message_lite.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "google_apis/google_api_keys.h" | 11 #include "google_apis/google_api_keys.h" |
| 12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 13 #include "net/base/net_errors.h" |
| 13 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
| 14 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 namespace copresence { | 20 namespace copresence { |
| 20 | 21 |
| 21 const char HttpPost::kApiKeyField[] = "key"; | 22 const char HttpPost::kApiKeyField[] = "key"; |
| 22 const char HttpPost::kTracingTokenField[] = "trace"; | 23 const char HttpPost::kTracingField[] = "trace"; |
| 23 | 24 |
| 24 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | 25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| 25 const std::string& server_host, | 26 const std::string& server_host, |
| 26 const std::string& rpc_name, | 27 const std::string& rpc_name, |
| 27 const std::string& tracing_token, | 28 const std::string& tracing_token, |
| 28 std::string api_key, | 29 std::string api_key, |
| 29 const google::protobuf::MessageLite& request_proto) { | 30 const google::protobuf::MessageLite& request_proto) { |
| 30 // Create the base URL to call. | 31 // Create the base URL to call. |
| 31 GURL url(server_host + "/" + rpc_name); | 32 GURL url(server_host + "/" + rpc_name); |
| 32 | 33 |
| 33 // Add the tracing token, if specified. | 34 // Add the tracing token, if specified. |
| 34 if (!tracing_token.empty()) { | 35 if (!tracing_token.empty()) { |
| 35 url = net::AppendQueryParameter( | 36 url = net::AppendQueryParameter( |
| 36 url, kTracingTokenField, "token:" + tracing_token); | 37 url, kTracingField, "token:" + tracing_token); |
| 37 } | 38 } |
| 38 | 39 |
| 39 // If no API key is specified, use the Chrome API key. | 40 // If no API key is specified, use the Chrome API key. |
| 40 if (api_key.empty()) { | 41 if (api_key.empty()) { |
| 41 #ifdef GOOGLE_CHROME_BUILD | 42 #ifdef GOOGLE_CHROME_BUILD |
| 42 DCHECK(google_apis::HasKeysConfigured()); | 43 DCHECK(google_apis::HasKeysConfigured()); |
| 43 api_key = google_apis::GetAPIKey(); | 44 api_key = google_apis::GetAPIKey(); |
| 44 #else | 45 #else |
| 45 LOG(ERROR) << "No Copresence API key provided"; | 46 LOG(ERROR) << "No Copresence API key provided"; |
| 46 #endif | 47 #endif |
| (...skipping 14 matching lines...) Expand all Loading... |
| 61 net::LOAD_DO_NOT_SAVE_COOKIES | | 62 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 62 net::LOAD_DO_NOT_SEND_COOKIES | | 63 net::LOAD_DO_NOT_SEND_COOKIES | |
| 63 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 64 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 64 url_fetcher_->SetUploadData("application/x-protobuf", request_data); | 65 url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| 65 } | 66 } |
| 66 | 67 |
| 67 HttpPost::~HttpPost() {} | 68 HttpPost::~HttpPost() {} |
| 68 | 69 |
| 69 void HttpPost::Start(const ResponseCallback& response_callback) { | 70 void HttpPost::Start(const ResponseCallback& response_callback) { |
| 70 response_callback_ = response_callback; | 71 response_callback_ = response_callback; |
| 72 DVLOG(3) << "Sending Copresence request to " |
| 73 << url_fetcher_->GetOriginalURL().spec(); |
| 71 url_fetcher_->Start(); | 74 url_fetcher_->Start(); |
| 72 } | 75 } |
| 73 | 76 |
| 74 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { | 77 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { |
| 75 DCHECK_EQ(url_fetcher_.get(), source); | 78 DCHECK_EQ(url_fetcher_.get(), source); |
| 76 | 79 |
| 77 // Gather response info. | 80 // Gather response info. |
| 78 std::string response; | 81 std::string response; |
| 79 source->GetResponseAsString(&response); | 82 source->GetResponseAsString(&response); |
| 80 int response_code = source->GetResponseCode(); | 83 int response_code = source->GetResponseCode(); |
| 81 | 84 |
| 82 // Log any errors. | 85 // Log any errors. |
| 83 if (response_code < 0) { | 86 if (response_code < 0) { |
| 87 net::URLRequestStatus status = source->GetStatus(); |
| 84 LOG(WARNING) << "Couldn't contact the Copresence server at " | 88 LOG(WARNING) << "Couldn't contact the Copresence server at " |
| 85 << source->GetURL(); | 89 << source->GetURL() << ". Status code " << status.status(); |
| 90 LOG_IF(WARNING, status.error()) |
| 91 << "Network error: " << net::ErrorToString(status.error()); |
| 92 LOG_IF(WARNING, !response.empty()) << "HTTP response: " << response; |
| 86 } else if (response_code != net::HTTP_OK) { | 93 } else if (response_code != net::HTTP_OK) { |
| 87 LOG(WARNING) << "Copresence request got HTTP response code " | 94 LOG(WARNING) << "Copresence request got HTTP response code " |
| 88 << response_code << ". Response:\n" << response; | 95 << response_code << ". Response:\n" << response; |
| 89 } | 96 } |
| 90 | 97 |
| 91 // Return the response. | 98 // Return the response. |
| 92 response_callback_.Run(response_code, response); | 99 response_callback_.Run(response_code, response); |
| 93 } | 100 } |
| 94 | 101 |
| 95 } // namespace copresence | 102 } // namespace copresence |
| OLD | NEW |