OLD | NEW |
---|---|
(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 // This deletes the url_fetcher_, which aborts the request. | |
rkc
2014/08/07 18:01:04
Nit: The comment is not necessary :)
Charlie
2014/08/07 18:05:01
Technically no comments are necessary unless the s
rkc
2014/08/07 19:25:06
It's a nit so feel free to address it how you want
willchan no longer on Chromium
2014/08/07 20:27:05
It's a little odd that HttpPost::Cancel() does `de
Charlie
2014/08/07 22:40:58
Notice that the HttpPost destructor is private. If
willchan no longer on Chromium
2014/08/07 22:47:52
But isn't this weird? This sounds like:
class Foo
Charlie
2014/08/07 23:33:04
I guess that makes sense. If HttpPost deletes itse
| |
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 | |
OLD | NEW |