OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "remoting/test/remote_host_info_fetcher.h" | 5 #include "remoting/test/remote_host_info_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
8 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
17 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 82 |
82 void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 83 void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
83 DCHECK(source); | 84 DCHECK(source); |
84 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); | 85 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); |
85 | 86 |
86 RemoteHostInfo remote_host_info; | 87 RemoteHostInfo remote_host_info; |
87 int response_code = request_->GetResponseCode(); | 88 int response_code = request_->GetResponseCode(); |
88 if (response_code != net::HTTP_OK) { | 89 if (response_code != net::HTTP_OK) { |
89 LOG(ERROR) << "RemoteHostInfo request failed with error code: " | 90 LOG(ERROR) << "RemoteHostInfo request failed with error code: " |
90 << response_code; | 91 << response_code; |
91 remote_host_info_callback_.Run(remote_host_info); | 92 base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info); |
92 remote_host_info_callback_.Reset(); | |
93 return; | 93 return; |
94 } | 94 } |
95 | 95 |
96 std::string response_string; | 96 std::string response_string; |
97 if (!request_->GetResponseAsString(&response_string)) { | 97 if (!request_->GetResponseAsString(&response_string)) { |
98 LOG(ERROR) << "Failed to retrieve RemoteHostInfo response data"; | 98 LOG(ERROR) << "Failed to retrieve RemoteHostInfo response data"; |
99 remote_host_info_callback_.Run(remote_host_info); | 99 base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info); |
100 remote_host_info_callback_.Reset(); | |
101 return; | 100 return; |
102 } | 101 } |
103 | 102 |
104 scoped_ptr<base::Value> response_value( | 103 scoped_ptr<base::Value> response_value( |
105 base::JSONReader::Read(response_string)); | 104 base::JSONReader::Read(response_string)); |
106 if (!response_value || | 105 if (!response_value || |
107 !response_value->IsType(base::Value::TYPE_DICTIONARY)) { | 106 !response_value->IsType(base::Value::TYPE_DICTIONARY)) { |
108 LOG(ERROR) << "Failed to parse response string to JSON"; | 107 LOG(ERROR) << "Failed to parse response string to JSON"; |
109 remote_host_info_callback_.Run(remote_host_info); | 108 base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info); |
110 remote_host_info_callback_.Reset(); | |
111 return; | 109 return; |
112 } | 110 } |
113 | 111 |
114 std::string remote_host_status; | 112 std::string remote_host_status; |
115 const base::DictionaryValue* response; | 113 const base::DictionaryValue* response; |
116 if (response_value->GetAsDictionary(&response)) { | 114 if (response_value->GetAsDictionary(&response)) { |
117 response->GetString("status", &remote_host_status); | 115 response->GetString("status", &remote_host_status); |
118 } else { | 116 } else { |
119 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; | 117 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; |
120 remote_host_info_callback_.Run(remote_host_info); | 118 base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info); |
121 remote_host_info_callback_.Reset(); | |
122 return; | 119 return; |
123 } | 120 } |
124 | 121 |
125 remote_host_info.SetRemoteHostStatusFromString(remote_host_status); | 122 remote_host_info.SetRemoteHostStatusFromString(remote_host_status); |
126 | 123 |
127 if (remote_host_info.IsReadyForConnection()) { | 124 if (remote_host_info.IsReadyForConnection()) { |
128 response->GetString("host.applicationId", &remote_host_info.application_id); | 125 response->GetString("host.applicationId", &remote_host_info.application_id); |
129 response->GetString("host.hostId", &remote_host_info.host_id); | 126 response->GetString("host.hostId", &remote_host_info.host_id); |
130 response->GetString("hostJid", &remote_host_info.host_jid); | 127 response->GetString("hostJid", &remote_host_info.host_jid); |
131 response->GetString("authorizationCode", | 128 response->GetString("authorizationCode", |
132 &remote_host_info.authorization_code); | 129 &remote_host_info.authorization_code); |
133 response->GetString("sharedSecret", &remote_host_info.shared_secret); | 130 response->GetString("sharedSecret", &remote_host_info.shared_secret); |
134 } | 131 } |
135 | 132 |
136 remote_host_info_callback_.Run(remote_host_info); | 133 base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info); |
137 remote_host_info_callback_.Reset(); | |
138 } | 134 } |
139 | 135 |
140 } // namespace test | 136 } // namespace test |
141 } // namespace remoting | 137 } // namespace remoting |
OLD | NEW |