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..dcee406f5aa9ed8ad678b50960ac08d52515e4dd |
--- /dev/null |
+++ b/remoting/test/chromoting_host_list_fetcher.cc |
@@ -0,0 +1,117 @@ |
+// 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() { |
+} |
+ |
+bool 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); |
joedow
2015/07/06 22:19:13
Will this line fit on the line above?
tonychun
2015/07/08 03:12:13
Done.
|
+ request_->SetRequestContext(request_context_getter_.get()); |
+ request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); |
+ request_->Start(); |
+ |
+ return true; |
joedow
2015/07/06 22:19:13
Why return a value here if it is always true? I'd
tonychun
2015/07/08 03:12:14
Done.
|
+} |
+ |
+void ChromotingHostListFetcher::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ DCHECK(source); |
+ DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); |
+ |
+ std::vector<ChromotingHostInfo> hostlist_; |
joedow
2015/07/06 22:19:13
no trailing underscore is used for local vars, onl
tonychun
2015/07/08 03:12:13
Done.
|
+ |
+ // Allows executing a callback after the original callback is Reset(). |
joedow
2015/07/06 22:19:12
Can you update the comment a bit, basically state
tonychun
2015/07/08 03:12:14
The ScopedClosureRunner has been removed.
|
+ HostlistCallback reset_hostlist_callback(hostlist_callback_); |
joedow
2015/07/06 22:19:13
Can you rename reset_hostlist_callback? You could
tonychun
2015/07/08 03:12:13
Done.
|
+ hostlist_callback_.Reset(); |
+ |
joedow
2015/07/06 22:19:12
remove newline
tonychun
2015/07/08 03:12:14
Done.
|
+ base::Closure cb = base::Bind(reset_hostlist_callback, &hostlist_); |
joedow
2015/07/06 22:19:12
rename cb, something like bound_hostlist_callback
joedow
2015/07/06 22:19:13
You can use base::ConstRef(hostlist) here instead
tonychun
2015/07/08 03:12:14
Done.
tonychun
2015/07/08 03:12:14
Acknowledged.
|
+ base::ScopedClosureRunner runner(cb); |
Sergey Ulanov
2015/07/07 00:01:46
I don't think this is a good way to use ScopedClos
tonychun
2015/07/08 03:12:14
Done.
|
+ |
+ int response_code = request_->GetResponseCode(); |
+ if (response_code != net::HTTP_OK) { |
+ LOG(ERROR) << "Hostlist request failed with error code: " << response_code; |
+ return; |
+ } |
+ |
+ std::string response_string; |
+ if (!request_->GetResponseAsString(&response_string)) { |
+ LOG(ERROR) << "Failed to retrieve Hostlist response data"; |
+ return; |
+ } |
+ |
+ 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; |
+ } |
+ |
+ const base::DictionaryValue* response; |
+ if (!response_value->GetAsDictionary(&response)) { |
+ LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; |
+ return; |
+ } |
+ |
+ const base::DictionaryValue* data = nullptr; |
+ response->GetDictionary("data", &data); |
+ |
joedow
2015/07/06 22:19:13
remove newline
tonychun
2015/07/08 03:12:14
Done.
|
+ if (!data) { |
Sergey Ulanov
2015/07/07 00:01:46
use GetDictionary() result here
tonychun
2015/07/08 03:12:13
Done.
|
+ LOG(ERROR) << "Hostlist response data is empty"; |
+ return; |
+ } |
+ |
+ const base::ListValue* items = nullptr; |
joedow
2015/07/06 22:19:13
would hosts be a better name? items is pretty gen
tonychun
2015/07/08 03:12:14
Done.
|
+ data->GetList("items", &items); |
+ |
joedow
2015/07/06 22:19:13
remove newline
tonychun
2015/07/08 03:12:13
Done.
|
+ if (!items) { |
Sergey Ulanov
2015/07/07 00:01:46
See my comment in chromoting_host_info.cc: Diction
tonychun
2015/07/08 03:12:14
Done.
|
+ LOG(ERROR) << "Failed to find hosts in Hostlist response data"; |
+ return; |
+ } |
+ |
+ int size = items->GetSize(); |
+ const base::DictionaryValue* item = nullptr; |
joedow
2015/07/06 22:19:12
host_info instead of item?
tonychun
2015/07/08 03:12:14
Done.
|
+ for (int i = 0; i < size; ++i) { |
Sergey Ulanov
2015/07/07 00:01:46
List values support iterators, use them here, i.e.
tonychun
2015/07/08 03:12:14
Done.
|
+ items->GetDictionary(i, &item); |
+ hostlist_.push_back(ChromotingHostInfo(*item)); |
+ } |
+} |
+ |
+} // namespace test |
+} // namespace remoting |