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..ffb160747b424b7b70d11e6ba70df61f99f1f98f |
--- /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" |
+#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(); |
+ 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::Cancel() { |
+ url_fetcher_.reset(); |
Daniel Erat
2014/08/07 17:55:22
why do you need to explicitly reset this? it's a s
Charlie
2014/08/07 18:00:03
Good point. Replacing with a comment.
|
+ 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 |