Index: remoting/test/hostlist_fetcher.cc |
diff --git a/remoting/test/hostlist_fetcher.cc b/remoting/test/hostlist_fetcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f1c904e8c07e9c508e70212ccb24fd78c360634e |
--- /dev/null |
+++ b/remoting/test/hostlist_fetcher.cc |
@@ -0,0 +1,119 @@ |
+// 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/hostlist_fetcher.h" |
+ |
+#include <iostream> |
joedow
2015/07/01 03:39:35
You should not be using iostream, use printf or DV
tonychun
2015/07/06 20:11:51
I put it in because I wanted to output the hostlis
|
+ |
+#include "base/bind.h" |
+#include "base/callback_helpers.h" |
+#include "base/json/json_reader.h" |
+#include "base/logging.h" |
+#include "base/message_loop/message_loop.h" |
joedow
2015/07/01 03:39:34
Where is message_loop used? Remove it if not used
tonychun
2015/07/06 20:11:50
Done.
|
+#include "base/strings/stringprintf.h" |
joedow
2015/07/01 03:39:35
remove header and StringPrintf call since it isn't
tonychun
2015/07/06 20:11:51
Done.
|
+#include "base/thread_task_runner_handle.h" |
+#include "base/values.h" |
+#include "net/http/http_response_headers.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 { |
+ |
+HostlistFetcher::HostlistFetcher() { |
+} |
+ |
+HostlistFetcher::~HostlistFetcher() { |
+} |
+ |
+bool HostlistFetcher::RetrieveHostlist(const std::string& access_token, |
+ ServiceEnvironment service_environment, |
+ const HostlistCallback& callback) { |
+ // Message the Directory service for the hostlist |
joedow
2015/07/01 03:39:35
I don't think this comment is useful, please remov
tonychun
2015/07/06 20:11:51
Done.
|
+ DVLOG(2) << "HostlistFetcher::RetrieveHostlist() called"; |
+ |
joedow
2015/07/01 03:39:35
Please add some additional DCHECKS here. You shou
tonychun
2015/07/06 20:11:51
I removed service_environment because it is only t
|
+ |
joedow
2015/07/01 03:39:35
Why so many empty newlines? Please reduce to one.
tonychun
2015/07/06 20:11:51
Done.
|
+ |
+ std::string service_url; |
+ switch (service_environment) { |
+ case kProductionEnvironment: |
+ DVLOG(1) << "Configuring service request for prod environment"; |
+ service_url = base::StringPrintf(kProdServiceEnvironmentUrlFormat); |
joedow
2015/07/01 03:39:35
Why are you using StringPrintf here? You aren't r
tonychun
2015/07/06 20:11:50
Done.
|
+ break; |
+ |
+ default: |
+ LOG(ERROR) << "Unrecognized service type: " << service_environment; |
+ return false; |
+ } |
+ |
+ hostlist_callback_ = callback; |
+ |
+ request_context_getter_ = new remoting::URLRequestContextGetter( |
+ base::ThreadTaskRunnerHandle::Get(), // network_runner |
+ base::ThreadTaskRunnerHandle::Get()); // file_runner |
+ |
+ request_ = |
+ net::URLFetcher::Create(GURL(service_url), net::URLFetcher::GET, this); |
+ request_->SetRequestContext(request_context_getter_.get()); |
+ request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); |
+ request_->SetUploadData("application/json; charset=UTF-8", "{}"); |
joedow
2015/07/01 03:39:35
Is this line needed for this call? I'm not sure t
tonychun
2015/07/06 20:11:51
Removed.
|
+ request_->Start(); |
+ |
+ return true; |
+} |
+ |
+void HostlistFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
+ DCHECK(source); |
+ DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); |
+ |
+ 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; |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist); |
joedow
2015/07/01 03:39:35
I think a ScopedClosureRunner() would make the mul
tonychun
2015/07/06 20:11:51
Done.
|
+ return; |
+ } |
+ |
+ std::string response_string; |
+ if (!request_->GetResponseAsString(&response_string)) { |
+ LOG(ERROR) << "Failed to retrieve Hostlist response data"; |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist); |
+ 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"; |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist); |
+ return; |
+ } |
+ |
+ const base::DictionaryValue* response; |
+ if (!response_value->GetAsDictionary(&response)) { |
+ LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist); |
+ return; |
+ } |
+ |
+ // add hosts to hostlist |
joedow
2015/07/01 03:39:35
Is this comment useful? Please remove it or updat
tonychun
2015/07/06 20:11:51
Done.
|
+ const base::ListValue* items = nullptr; |
+ response->GetList("data.items", &items); |
+ |
+ int size = items->GetSize(); |
joedow
2015/07/01 03:39:35
What if items is a nullptr, you should make sure i
tonychun
2015/07/06 20:11:51
Done.
|
+ DVLOG(1) << "There are " << size << " hosts in the hostlist"; |
+ const base::DictionaryValue* item; |
joedow
2015/07/01 03:39:35
it's a good idea to initialize pointers to nullptr
tonychun
2015/07/06 20:11:51
Done.
|
+ for (int i = 0; i < size; ++i) { |
+ items->GetDictionary(i,&item); |
joedow
2015/07/01 03:39:35
add space between comma and ampersand (and run cpp
tonychun
2015/07/06 20:11:51
Done.
|
+ hostlist.push_back(ChromotingHostInfo(*item)); |
+ } |
+ |
+ base::ResetAndReturn(&hostlist_callback_).Run(hostlist); |
+} |
+ |
+} // namespace test |
+} // namespace remoting |