| 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/net_errors.h" |
| 14 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
| 15 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 16 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace copresence { | 20 namespace copresence { |
| 21 | 21 |
| 22 const char HttpPost::kApiKeyField[] = "key"; | 22 const char HttpPost::kApiKeyField[] = "key"; |
| 23 const char HttpPost::kTracingField[] = "trace"; | 23 const char HttpPost::kTracingField[] = "trace"; |
| 24 | 24 |
| 25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | 25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| 26 const std::string& server_host, | 26 const std::string& server_host, |
| 27 const std::string& rpc_name, | 27 const std::string& rpc_name, |
| 28 std::string api_key, |
| 29 const std::string& auth_token, |
| 28 const std::string& tracing_token, | 30 const std::string& tracing_token, |
| 29 std::string api_key, | |
| 30 const google::protobuf::MessageLite& request_proto) { | 31 const google::protobuf::MessageLite& request_proto) { |
| 31 // Create the base URL to call. | 32 // Create the base URL to call. |
| 32 GURL url(server_host + "/" + rpc_name); | 33 GURL url(server_host + "/" + rpc_name); |
| 33 | 34 |
| 34 // Add the tracing token, if specified. | 35 // Add the tracing token, if specified. |
| 35 if (!tracing_token.empty()) { | 36 if (!tracing_token.empty()) { |
| 36 url = net::AppendQueryParameter( | 37 url = net::AppendQueryParameter( |
| 37 url, kTracingField, "token:" + tracing_token); | 38 url, kTracingField, "token:" + tracing_token); |
| 38 } | 39 } |
| 39 | 40 |
| 41 // If we have no auth token, authenticate using the API key. |
| 40 // If no API key is specified, use the Chrome API key. | 42 // If no API key is specified, use the Chrome API key. |
| 41 if (api_key.empty()) { | 43 if (auth_token.empty()) { |
| 44 if (api_key.empty()) { |
| 42 #ifdef GOOGLE_CHROME_BUILD | 45 #ifdef GOOGLE_CHROME_BUILD |
| 43 DCHECK(google_apis::HasKeysConfigured()); | 46 DCHECK(google_apis::HasKeysConfigured()); |
| 44 api_key = google_apis::GetAPIKey(); | 47 api_key = google_apis::GetAPIKey(); |
| 45 #else | 48 #else |
| 46 LOG(ERROR) << "No Copresence API key provided"; | 49 LOG(ERROR) << "No Copresence API key provided"; |
| 47 #endif | 50 #endif |
| 51 } |
| 52 url = net::AppendQueryParameter(url, kApiKeyField, api_key); |
| 48 } | 53 } |
| 49 url = net::AppendQueryParameter(url, kApiKeyField, api_key); | |
| 50 | 54 |
| 51 // Serialize the proto for transmission. | 55 // Serialize the proto for transmission. |
| 52 std::string request_data; | 56 std::string request_data; |
| 53 bool serialize_success = request_proto.SerializeToString(&request_data); | 57 bool serialize_success = request_proto.SerializeToString(&request_data); |
| 54 DCHECK(serialize_success); | 58 DCHECK(serialize_success); |
| 55 | 59 |
| 56 // Configure and send the request. | 60 // Configure and send the request. |
| 57 url_fetcher_.reset(net::URLFetcher::Create( | 61 url_fetcher_.reset(net::URLFetcher::Create( |
| 58 kUrlFetcherId, url, net::URLFetcher::POST, this)); | 62 kUrlFetcherId, url, net::URLFetcher::POST, this)); |
| 59 url_fetcher_->SetRequestContext(url_context_getter); | 63 url_fetcher_->SetRequestContext(url_context_getter); |
| 60 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 64 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 61 net::LOAD_DISABLE_CACHE | | 65 net::LOAD_DISABLE_CACHE | |
| 62 net::LOAD_DO_NOT_SAVE_COOKIES | | 66 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 63 net::LOAD_DO_NOT_SEND_COOKIES | | 67 net::LOAD_DO_NOT_SEND_COOKIES | |
| 64 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 68 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 65 url_fetcher_->SetUploadData("application/x-protobuf", request_data); | 69 url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| 70 if (!auth_token.empty()) { |
| 71 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + auth_token); |
| 72 } |
| 66 } | 73 } |
| 67 | 74 |
| 68 HttpPost::~HttpPost() {} | 75 HttpPost::~HttpPost() {} |
| 69 | 76 |
| 70 void HttpPost::Start(const ResponseCallback& response_callback) { | 77 void HttpPost::Start(const ResponseCallback& response_callback) { |
| 71 response_callback_ = response_callback; | 78 response_callback_ = response_callback; |
| 72 DVLOG(3) << "Sending Copresence request to " | 79 DVLOG(3) << "Sending Copresence request to " |
| 73 << url_fetcher_->GetOriginalURL().spec(); | 80 << url_fetcher_->GetOriginalURL().spec(); |
| 74 url_fetcher_->Start(); | 81 url_fetcher_->Start(); |
| 75 } | 82 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 93 } else if (response_code != net::HTTP_OK) { | 100 } else if (response_code != net::HTTP_OK) { |
| 94 LOG(WARNING) << "Copresence request got HTTP response code " | 101 LOG(WARNING) << "Copresence request got HTTP response code " |
| 95 << response_code << ". Response:\n" << response; | 102 << response_code << ". Response:\n" << response; |
| 96 } | 103 } |
| 97 | 104 |
| 98 // Return the response. | 105 // Return the response. |
| 99 response_callback_.Run(response_code, response); | 106 response_callback_.Run(response_code, response); |
| 100 } | 107 } |
| 101 | 108 |
| 102 } // namespace copresence | 109 } // namespace copresence |
| OLD | NEW |