Chromium Code Reviews| Index: components/copresence/rpc/http_post.cc |
| diff --git a/components/copresence/rpc/http_post.cc b/components/copresence/rpc/http_post.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9f965b0e18cbfd4211a420c211afee4a86f8a5b9 |
| --- /dev/null |
| +++ b/components/copresence/rpc/http_post.cc |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license |
| +// that can be found in the LICENSE file. |
| + |
| +#include "components/copresence/rpc/http_post.h" |
| + |
| +#include <google/protobuf/message_lite.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "google_apis/google_api_keys.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/base/url_util.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +namespace copresence { |
| + |
| +namespace { |
| + |
| +const char kDefaultCopresenceServer[] = |
| + "https://www.googleapis.com/copresence/v2/copresence"; |
| +const char kTracingTokenField[] = "trace"; |
| +const char kApiKeyField[] = "key"; |
| + |
| +} // namespace |
| + |
| +// Send a request to the Copresence server. |
|
Daniel Erat
2014/08/06 00:44:46
put comments like these in the header instead of t
Charlie
2014/08/06 19:32:18
Done.
|
| +// After calling the callback, this object will delete itself. |
| +HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| + const std::string& rpc_name, |
| + scoped_ptr<google::protobuf::MessageLite> request_proto, |
| + const ResponseCallback& response_callback) |
| + : response_callback_(response_callback) { |
| + // Create the base URL to call. |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + const std::string copresence_server_url = |
| + command_line->HasSwitch(switches::kCopresenceServer) ? |
| + command_line->GetSwitchValueASCII(switches::kCopresenceServer) : |
| + kDefaultCopresenceServer; |
| + GURL url(copresence_server_url + "/" + rpc_name); |
| + |
| + // Add the Chrome API key. |
| + DCHECK(google_apis::HasKeysConfigured()); |
| + url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); |
| + |
| + // Add the tracing token, if specified. |
| + if (command_line->HasSwitch(switches::kCopresenceTracingToken)) { |
| + url = net::AppendQueryParameter( |
| + url, |
| + kTracingTokenField, |
| + "token:" + command_line->GetSwitchValueASCII( |
| + switches::kCopresenceTracingToken)); |
| + } |
| + |
| + // Serialize the proto for transmission. |
| + std::string request_data; |
| + DCHECK(request_proto->SerializeToString(&request_data)); |
| + |
| + // Configure and send the request. |
| + url_fetcher_.reset(net::URLFetcher::Create( |
| + kUrlFetcherId, url, net::URLFetcher::POST, this)); |
| + url_fetcher_->SetRequestContext(url_context_getter); |
| + url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| + net::LOAD_DISABLE_CACHE | |
| + net::LOAD_DO_NOT_SAVE_COOKIES | |
| + net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| + url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| + url_fetcher_->Start(); |
| +} |
| + |
| +HttpPost::~HttpPost() {} |
| + |
| +void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { |
| + DCHECK_EQ(url_fetcher_.get(), source); |
| + |
| + // Gather response info. |
| + std::string response; |
| + source->GetResponseAsString(&response); |
| + int response_code = source->GetResponseCode(); |
| + |
| + // Log any errors. |
| + if (response_code < 0) { |
| + LOG(ERROR) << "Couldn't contact the Copresence server at " |
| + << source->GetURL(); |
| + } else if (response_code != net::HTTP_OK) { |
| + LOG(ERROR) << "Copresence request got HTTP response code " << response_code |
| + << ". Response:\n" << response; |
| + } |
| + |
| + // Return the response. |
| + response_callback_.Run(response_code, response); |
| + delete this; |
|
Daniel Erat
2014/08/06 00:44:46
i'm not sure whether it's safe to leak these if th
Charlie
2014/08/06 19:32:18
Ok. Can you clarify how you'd like us to handle th
Daniel Erat
2014/08/06 21:35:17
i'm not entirely sure whether this is a problem, b
Charlie
2014/08/06 22:36:23
Thanks. Added a note to the header file.
|
| +} |
| + |
| +} // namespace copresence |