Index: remoting/test/chromoting_host_list_fetcher.cc |
diff --git a/remoting/test/chromoting_host_list_fetcher.cc b/remoting/test/chromoting_host_list_fetcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08664ae44d3b9dec6b83104970dcc40465043aca |
--- /dev/null |
+++ b/remoting/test/chromoting_host_list_fetcher.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/test/chromoting_host_list_fetcher.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback_helpers.h" |
+#include "base/json/json_reader.h" |
+#include "base/logging.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "base/values.h" |
+#include "net/http/http_status_code.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "remoting/base/url_request_context_getter.h" |
+ |
+namespace remoting { |
+namespace test { |
+ |
+ChromotingHostListFetcher::ChromotingHostListFetcher() { |
+} |
+ |
+ChromotingHostListFetcher::~ChromotingHostListFetcher() { |
+} |
+ |
+void ChromotingHostListFetcher::RetrieveHostlist( |
+ const std::string& access_token, |
+ const HostlistCallback& callback) { |
+ |
+ DVLOG(2) << "ChromotingHostListFetcher::RetrieveHostlist() called"; |
+ |
+ DCHECK(!access_token.empty()); |
+ DCHECK(!callback.is_null()); |
+ DCHECK(hostlist_callback_.is_null()); |
+ |
+ hostlist_callback_ = callback; |
+ |
+ request_context_getter_ = new remoting::URLRequestContextGetter( |
+ base::ThreadTaskRunnerHandle::Get(), // network_runner |
+ base::ThreadTaskRunnerHandle::Get()); // file_runner |
+ |
+ request_ = net::URLFetcher::Create( |
+ GURL(kChromotingHostListProdRequestUrl), net::URLFetcher::GET, this); |
+ request_->SetRequestContext(request_context_getter_.get()); |
+ request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); |
+ request_->Start(); |
+} |
+ |
+bool ChromotingHostListFetcher::ProcessResponse( |
+ std::vector<ChromotingHostInfo>* hostlist) { |
+ int response_code = request_->GetResponseCode(); |
+ if (response_code != net::HTTP_OK) { |
+ LOG(ERROR) << "Hostlist request failed with error code: " << response_code; |
+ return false; |
+ } |
+ |
+ std::string response_string; |
+ if (!request_->GetResponseAsString(&response_string)) { |
+ LOG(ERROR) << "Failed to retrieve Hostlist response data"; |
+ return false; |
+ } |
+ |
+ scoped_ptr<base::Value> response_value( |
+ base::JSONReader::Read(response_string)); |
+ if (!response_value || |
+ !response_value->IsType(base::Value::TYPE_DICTIONARY)) { |
+ LOG(ERROR) << "Failed to parse response string to JSON"; |
+ return false; |
+ } |
+ |
+ const base::DictionaryValue* response; |
+ if (!response_value->GetAsDictionary(&response)) { |
+ LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; |
+ return false; |
+ } |
+ |
+ const base::DictionaryValue* data = nullptr; |
+ if (!response->GetDictionary("data", &data)) { |
+ LOG(ERROR) << "Hostlist response data is empty"; |
+ return false; |
+ } |
+ |
+ const base::ListValue* hosts = nullptr; |
+ if (!data->GetList("items", &hosts)) { |
+ LOG(ERROR) << "Failed to find hosts in Hostlist response data"; |
+ return false; |
+ } |
+ |
+ // TODO(TonyChun): host_info that does not have all information (host_name, |
+ // host_id, jabber_id, host_offline_reason, host_status, public key) will not |
+ // be added to the hostlist. |
joedow
2015/07/08 17:19:07
Why is this a TODO? It seems like documentation v
tonychun
2015/07/08 22:38:15
That was a mistake. Changed it.
|
+ for (base::Value* host_info : *hosts) { |
+ ChromotingHostInfo chromoting_host_info; |
+ if (chromoting_host_info.ParseHostInfo( |
+ *reinterpret_cast<base::DictionaryValue*>(host_info))) { |
joedow
2015/07/08 17:19:07
Instead of using a reinterpret cast here, can you
tonychun
2015/07/08 22:38:15
Done.
|
+ hostlist->push_back(chromoting_host_info); |
+ } |
+ } |
+ return true; |
+} |
+ |
+void ChromotingHostListFetcher::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ DCHECK(source); |
+ DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); |
+ |
+ std::vector<ChromotingHostInfo> hostlist; |
+ |
+ // Any malformed data from the Directory will notify the callback that |
+ // processing the response failed. |
+ if (!ProcessResponse(&hostlist)) { |
+ hostlist.clear(); |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist, false); |
+ return; |
joedow
2015/07/08 17:19:07
nit: I think it is better to have a single return
tonychun
2015/07/08 22:38:15
Good thinking.
|
+ } |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist, true); |
+} |
+ |
+} // namespace test |
+} // namespace remoting |