Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Unified Diff: components/copresence/rpc/http_post.cc

Issue 433283002: Adding the Copresence RpcHandler and HttpPost helper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@directive-handler
Patch Set: Addressing nits Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..963f1921f7b66a84fac30c5e765285764906219d
--- /dev/null
+++ b/components/copresence/rpc/http_post.cc
@@ -0,0 +1,94 @@
+// 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>
willchan no longer on Chromium 2014/08/08 01:05:59 Sometimes we use the system protobufs and sometime
Charlie 2014/08/08 21:57:53 Ok, but why? That seems like a mess.
+
+#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));
willchan no longer on Chromium 2014/08/08 01:05:59 Isn't this a bug? You're only going to serialize t
rkc 2014/08/08 01:29:07 This is a bug.
Charlie 2014/08/08 21:57:53 Fixed.
+
+ // 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 "
willchan no longer on Chromium 2014/08/08 01:05:59 This is a WARNING. There are plenty of valid reaso
Charlie 2014/08/08 21:57:53 Done.
+ << 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);
+}
+
+} // namespace copresence

Powered by Google App Engine
This is Rietveld 408576698