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

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: 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 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 #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.
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "components/copresence/copresence_switches.h"
12 #include "content/public/browser/browser_context.h"
13 #include "google_apis/google_api_keys.h"
14 #include "net/base/load_flags.h"
15 #include "net/base/url_util.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h"
19
20 namespace copresence {
21
22 namespace {
23
24 const char kTracingTokenField[] = "trace";
25 const char kApiKeyField[] = "key";
26
27 } // namespace
28
29 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter,
30 const std::string& server_host,
31 const std::string& rpc_name,
32 const google::protobuf::MessageLite& request_proto,
33 const ResponseCallback& response_callback)
34 : response_callback_(response_callback) {
35 // Create the base URL to call.
36 GURL url(server_host + "/" + rpc_name);
37
38 // Add the Chrome API key.
39 // TODO(ckehoe): Replace this with an app-specific key
40 // once the server API supports it.
41 DCHECK(google_apis::HasKeysConfigured());
42 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey());
43
44 // Add the tracing token, if specified.
45 CommandLine* command_line = CommandLine::ForCurrentProcess();
46 if (command_line->HasSwitch(switches::kCopresenceTracingToken)) {
47 url = net::AppendQueryParameter(
48 url,
49 kTracingTokenField,
50 "token:" + command_line->GetSwitchValueASCII(
51 switches::kCopresenceTracingToken));
52 }
53
54 // Serialize the proto for transmission.
55 std::string request_data;
56 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.
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 url_fetcher_->Start();
69 }
70
71 HttpPost::~HttpPost() {}
72
73 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) {
74 DCHECK_EQ(url_fetcher_.get(), source);
75
76 // Gather response info.
77 std::string response;
78 source->GetResponseAsString(&response);
79 int response_code = source->GetResponseCode();
80
81 // Log any errors.
82 if (response_code < 0) {
83 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.
84 << source->GetURL();
85 } else if (response_code != net::HTTP_OK) {
86 LOG(ERROR) << "Copresence request got HTTP response code " << response_code
87 << ". Response:\n" << response;
88 }
89
90 // Return the response.
91 response_callback_.Run(response_code, response, this);
92 }
93
94 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698