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 #include "url/gurl.h" |
17 | 18 |
18 namespace copresence { | 19 namespace copresence { |
19 | 20 |
20 // TODO(ckehoe): Come up with a better fix for Chromium. | 21 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"; | 22 const char HttpPost::kTracingTokenField[] = "trace"; |
30 | 23 |
31 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | 24 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
32 const std::string& server_host, | 25 const std::string& server_host, |
33 const std::string& rpc_name, | 26 const std::string& rpc_name, |
34 const std::string& tracing_token, | 27 const std::string& tracing_token, |
| 28 std::string api_key, |
35 const google::protobuf::MessageLite& request_proto) { | 29 const google::protobuf::MessageLite& request_proto) { |
36 // Create the base URL to call. | 30 // Create the base URL to call. |
37 GURL url(server_host + "/" + rpc_name); | 31 GURL url(server_host + "/" + rpc_name); |
38 | 32 |
39 // Add the Chrome API key. | |
40 // TODO(ckehoe): Replace this with an app-specific key once the | |
41 // server API supports it. Also fix the Chromium case. | |
42 #ifdef GOOGLE_CHROME_BUILD | |
43 DCHECK(google_apis::HasKeysConfigured()); | |
44 url = net::AppendQueryParameter(url, kApiKeyField, google_apis::GetAPIKey()); | |
45 #endif | |
46 | |
47 // Add the tracing token, if specified. | 33 // Add the tracing token, if specified. |
48 if (!tracing_token.empty()) { | 34 if (!tracing_token.empty()) { |
49 url = net::AppendQueryParameter( | 35 url = net::AppendQueryParameter( |
50 url, kTracingTokenField, "token:" + tracing_token); | 36 url, kTracingTokenField, "token:" + tracing_token); |
51 } | 37 } |
52 | 38 |
| 39 // If no API key is specified, use the Chrome API key. |
| 40 if (api_key.empty()) { |
| 41 #ifdef GOOGLE_CHROME_BUILD |
| 42 DCHECK(google_apis::HasKeysConfigured()); |
| 43 api_key = google_apis::GetAPIKey(); |
| 44 #else |
| 45 LOG(ERROR) << "No Copresence API key provided"; |
| 46 #endif |
| 47 } |
| 48 url = net::AppendQueryParameter(url, kApiKeyField, api_key); |
| 49 |
53 // Serialize the proto for transmission. | 50 // Serialize the proto for transmission. |
54 std::string request_data; | 51 std::string request_data; |
55 bool serialize_success = request_proto.SerializeToString(&request_data); | 52 bool serialize_success = request_proto.SerializeToString(&request_data); |
56 DCHECK(serialize_success); | 53 DCHECK(serialize_success); |
57 | 54 |
58 // Configure and send the request. | 55 // Configure and send the request. |
59 url_fetcher_.reset(net::URLFetcher::Create( | 56 url_fetcher_.reset(net::URLFetcher::Create( |
60 kUrlFetcherId, url, net::URLFetcher::POST, this)); | 57 kUrlFetcherId, url, net::URLFetcher::POST, this)); |
61 url_fetcher_->SetRequestContext(url_context_getter); | 58 url_fetcher_->SetRequestContext(url_context_getter); |
62 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 59 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
(...skipping 26 matching lines...) Expand all Loading... |
89 } else if (response_code != net::HTTP_OK) { | 86 } else if (response_code != net::HTTP_OK) { |
90 LOG(WARNING) << "Copresence request got HTTP response code " | 87 LOG(WARNING) << "Copresence request got HTTP response code " |
91 << response_code << ". Response:\n" << response; | 88 << response_code << ". Response:\n" << response; |
92 } | 89 } |
93 | 90 |
94 // Return the response. | 91 // Return the response. |
95 response_callback_.Run(response_code, response); | 92 response_callback_.Run(response_code, response); |
96 } | 93 } |
97 | 94 |
98 } // namespace copresence | 95 } // namespace copresence |
OLD | NEW |