OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/copresence/rpc/http_post.h" |
| 6 |
| 7 #include <google/protobuf/message_lite.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "components/copresence/copresence_switches.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "google_apis/google_api_keys.h" |
| 14 #include "net/base/load_flags.h" |
| 15 #include "net/base/url_util.h" |
| 16 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_context_getter.h" |
| 19 |
| 20 namespace copresence { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kTracingTokenField[] = "trace"; |
| 25 const char kApiKeyField[] = "key"; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| 30 const std::string& server_host, |
| 31 const std::string& rpc_name, |
| 32 const google::protobuf::MessageLite& request_proto, |
| 33 const ResponseCallback& response_callback) |
| 34 : response_callback_(response_callback) { |
| 35 // Create the base URL to call. |
| 36 GURL url(server_host + "/" + rpc_name); |
| 37 |
| 38 // Add the Chrome API key. |
| 39 DCHECK(google_apis::HasKeysConfigured()); |
| 40 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); |
| 41 |
| 42 // Add the tracing token, if specified. |
| 43 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 44 if (command_line->HasSwitch(switches::kCopresenceTracingToken)) { |
| 45 url = net::AppendQueryParameter( |
| 46 url, |
| 47 kTracingTokenField, |
| 48 "token:" + command_line->GetSwitchValueASCII( |
| 49 switches::kCopresenceTracingToken)); |
| 50 } |
| 51 |
| 52 // Serialize the proto for transmission. |
| 53 std::string request_data; |
| 54 DCHECK(request_proto.SerializeToString(&request_data)); |
| 55 |
| 56 // Configure and send the request. |
| 57 url_fetcher_.reset(net::URLFetcher::Create( |
| 58 kUrlFetcherId, url, net::URLFetcher::POST, this)); |
| 59 url_fetcher_->SetRequestContext(url_context_getter); |
| 60 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 61 net::LOAD_DISABLE_CACHE | |
| 62 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 63 net::LOAD_DO_NOT_SEND_COOKIES | |
| 64 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 65 url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| 66 url_fetcher_->Start(); |
| 67 } |
| 68 |
| 69 HttpPost::~HttpPost() {} |
| 70 |
| 71 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { |
| 72 DCHECK_EQ(url_fetcher_.get(), source); |
| 73 |
| 74 // Gather response info. |
| 75 std::string response; |
| 76 source->GetResponseAsString(&response); |
| 77 int response_code = source->GetResponseCode(); |
| 78 |
| 79 // Log any errors. |
| 80 if (response_code < 0) { |
| 81 LOG(ERROR) << "Couldn't contact the Copresence server at " |
| 82 << source->GetURL(); |
| 83 } else if (response_code != net::HTTP_OK) { |
| 84 LOG(ERROR) << "Copresence request got HTTP response code " << response_code |
| 85 << ". Response:\n" << response; |
| 86 } |
| 87 |
| 88 // Return the response. |
| 89 response_callback_.Run(response_code, response); |
| 90 delete this; |
| 91 } |
| 92 |
| 93 } // namespace copresence |
OLD | NEW |