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 | |
3 // that can be 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 "chrome/common/chrome_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 kDefaultCopresenceServer[] = | |
25 "https://www.googleapis.com/copresence/v2/copresence"; | |
26 const char kTracingTokenField[] = "trace"; | |
27 const char kApiKeyField[] = "key"; | |
28 | |
29 } // namespace | |
30 | |
31 // Send a request to the Copresence server. | |
Daniel Erat
2014/08/06 00:44:46
put comments like these in the header instead of t
Charlie
2014/08/06 19:32:18
Done.
| |
32 // After calling the callback, this object will delete itself. | |
33 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | |
34 const std::string& rpc_name, | |
35 scoped_ptr<google::protobuf::MessageLite> request_proto, | |
36 const ResponseCallback& response_callback) | |
37 : response_callback_(response_callback) { | |
38 // Create the base URL to call. | |
39 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
40 const std::string copresence_server_url = | |
41 command_line->HasSwitch(switches::kCopresenceServer) ? | |
42 command_line->GetSwitchValueASCII(switches::kCopresenceServer) : | |
43 kDefaultCopresenceServer; | |
44 GURL url(copresence_server_url + "/" + rpc_name); | |
45 | |
46 // Add the Chrome API key. | |
47 DCHECK(google_apis::HasKeysConfigured()); | |
48 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); | |
49 | |
50 // Add the tracing token, if specified. | |
51 if (command_line->HasSwitch(switches::kCopresenceTracingToken)) { | |
52 url = net::AppendQueryParameter( | |
53 url, | |
54 kTracingTokenField, | |
55 "token:" + command_line->GetSwitchValueASCII( | |
56 switches::kCopresenceTracingToken)); | |
57 } | |
58 | |
59 // Serialize the proto for transmission. | |
60 std::string request_data; | |
61 DCHECK(request_proto->SerializeToString(&request_data)); | |
62 | |
63 // Configure and send the request. | |
64 url_fetcher_.reset(net::URLFetcher::Create( | |
65 kUrlFetcherId, url, net::URLFetcher::POST, this)); | |
66 url_fetcher_->SetRequestContext(url_context_getter); | |
67 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | |
68 net::LOAD_DISABLE_CACHE | | |
69 net::LOAD_DO_NOT_SAVE_COOKIES | | |
70 net::LOAD_DO_NOT_SEND_COOKIES | | |
71 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
72 url_fetcher_->SetUploadData("application/x-protobuf", request_data); | |
73 url_fetcher_->Start(); | |
74 } | |
75 | |
76 HttpPost::~HttpPost() {} | |
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); | |
97 delete this; | |
Daniel Erat
2014/08/06 00:44:46
i'm not sure whether it's safe to leak these if th
Charlie
2014/08/06 19:32:18
Ok. Can you clarify how you'd like us to handle th
Daniel Erat
2014/08/06 21:35:17
i'm not entirely sure whether this is a problem, b
Charlie
2014/08/06 22:36:23
Thanks. Added a note to the header file.
| |
98 } | |
99 | |
100 } // namespace copresence | |
OLD | NEW |