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

Side by Side 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: Fixing RpcHandlerTest.Initialize 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 unified diff | Download patch
OLDNEW
(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 // TODO(ckehoe): Support third-party protobufs too.
8 #include <google/protobuf/message_lite.h>
9
10 #include "base/bind.h"
11 #include "google_apis/google_api_keys.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/url_util.h"
14 #include "net/http/http_status_code.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_context_getter.h"
17
18 namespace copresence {
19
20 // TODO(ckehoe): Come up with a better fix for Chromium.
21 #ifdef GOOGLE_CHROME_BUILD
22 namespace {
23
24 const char kApiKeyField[] = "key";
25
26 } // namespace
27 #endif
28
29 const char HttpPost::kTracingTokenField[] = "trace";
30
31 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter,
32 const std::string& server_host,
33 const std::string& rpc_name,
34 const std::string& tracing_token,
35 const google::protobuf::MessageLite& request_proto) {
36 // Create the base URL to call.
37 GURL url(server_host + "/" + rpc_name);
38
39 // Add the Chrome API key.
40 // TODO(ckehoe): Replace this with an app-specific key once the
41 // server API supports it. Also fix the Chromium case.
42 #ifdef GOOGLE_CHROME_BUILD
43 DCHECK(google_apis::HasKeysConfigured());
44 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey());
45 #endif
46
47 // Add the tracing token, if specified.
48 if (!tracing_token.empty()) {
49 url = net::AppendQueryParameter(
50 url, kTracingTokenField, "token:" + tracing_token);
51 }
52
53 // Serialize the proto for transmission.
54 std::string request_data;
55 bool serialize_success = request_proto.SerializeToString(&request_data);
56 DCHECK(serialize_success);
57
58 // Configure and send the request.
59 url_fetcher_.reset(net::URLFetcher::Create(
60 kUrlFetcherId, url, net::URLFetcher::POST, this));
61 url_fetcher_->SetRequestContext(url_context_getter);
62 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE |
63 net::LOAD_DISABLE_CACHE |
64 net::LOAD_DO_NOT_SAVE_COOKIES |
65 net::LOAD_DO_NOT_SEND_COOKIES |
66 net::LOAD_DO_NOT_SEND_AUTH_DATA);
67 url_fetcher_->SetUploadData("application/x-protobuf", request_data);
68 }
69
70 HttpPost::~HttpPost() {}
71
72 void HttpPost::Start(const ResponseCallback& response_callback) {
73 response_callback_ = response_callback;
74 url_fetcher_->Start();
75 }
76
77 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) {
78 DCHECK_EQ(url_fetcher_.get(), source);
79
80 // Gather response info.
81 std::string response;
82 source->GetResponseAsString(&response);
83 int response_code = source->GetResponseCode();
84
85 // Log any errors.
86 if (response_code < 0) {
87 LOG(WARNING) << "Couldn't contact the Copresence server at "
88 << source->GetURL();
89 } else if (response_code != net::HTTP_OK) {
90 LOG(WARNING) << "Copresence request got HTTP response code "
91 << response_code << ". Response:\n" << response;
92 }
93
94 // Return the response.
95 response_callback_.Run(response_code, response);
96 }
97
98 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698