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