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

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: Adding cleanup logic for HttpPost. Also rebased to master. 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
« no previous file with comments | « components/copresence/rpc/http_post.h ('k') | components/copresence/rpc/http_post_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
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));
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::Cancel() {
74 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.
75 delete this;
76 }
77
78 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) {
79 DCHECK_EQ(url_fetcher_.get(), source);
80
81 // Gather response info.
82 std::string response;
83 source->GetResponseAsString(&response);
84 int response_code = source->GetResponseCode();
85
86 // Log any errors.
87 if (response_code < 0) {
88 LOG(ERROR) << "Couldn't contact the Copresence server at "
89 << source->GetURL();
90 } else if (response_code != net::HTTP_OK) {
91 LOG(ERROR) << "Copresence request got HTTP response code " << response_code
92 << ". Response:\n" << response;
93 }
94
95 // Return the response.
96 response_callback_.Run(response_code, response, this);
97 delete this;
98 }
99
100 } // namespace copresence
OLDNEW
« no previous file with comments | « components/copresence/rpc/http_post.h ('k') | components/copresence/rpc/http_post_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698