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..d47e3ddfbff4ba1b2db440dbf2831fb94d1a7a6b |
| --- /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 "components/copresence/copresence_switches.h" |
| +#include "content/public/browser/browser_context.h" |
|
willchan no longer on Chromium
2014/08/08 01:05:58
Where is this used?
Charlie
2014/08/08 21:57:52
It's outdated. Deleted.
|
| +#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 kTracingTokenField[] = "trace"; |
| +const char kApiKeyField[] = "key"; |
| + |
| +} // namespace |
| + |
| +HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
| + const std::string& server_host, |
| + const std::string& rpc_name, |
| + const google::protobuf::MessageLite& request_proto, |
| + const ResponseCallback& response_callback) |
| + : response_callback_(response_callback) { |
| + // Create the base URL to call. |
| + GURL url(server_host + "/" + rpc_name); |
| + |
| + // Add the Chrome API key. |
| + // TODO(ckehoe): Replace this with an app-specific key |
| + // once the server API supports it. |
| + DCHECK(google_apis::HasKeysConfigured()); |
| + url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); |
| + |
| + // Add the tracing token, if specified. |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
|
willchan no longer on Chromium
2014/08/08 01:05:58
Only my personal preference (so you should feel fr
rkc
2014/08/08 01:29:07
We'd have to access this global object 'somewhere'
willchan no longer on Chromium
2014/08/08 18:01:20
Replying just to your point, but again, it's just
Charlie
2014/08/08 21:57:52
I don't get your argument. Why should main() "own"
willchan no longer on Chromium
2014/08/08 22:31:01
But the point is that it's not localized :) The co
|
| + if (command_line->HasSwitch(switches::kCopresenceTracingToken)) { |
| + url = net::AppendQueryParameter( |
|
willchan no longer on Chromium
2014/08/08 18:01:20
I don't see a unit test for this. Can you add one?
Charlie
2014/08/08 21:57:52
Done.
|
| + 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::Cancel() { |
| + // This deletes the url_fetcher_, which aborts the request. |
| + delete this; |
| +} |
| + |
| +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, this); |
| + delete this; |
| +} |
| + |
| +} // namespace copresence |