Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/test/chromoting_host_list_fetcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "base/values.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 #include "remoting/base/url_request_context_getter.h" | |
| 16 | |
| 17 namespace remoting { | |
| 18 namespace test { | |
| 19 | |
| 20 ChromotingHostListFetcher::ChromotingHostListFetcher() { | |
| 21 } | |
| 22 | |
| 23 ChromotingHostListFetcher::~ChromotingHostListFetcher() { | |
| 24 } | |
| 25 | |
| 26 void ChromotingHostListFetcher::RetrieveHostlist( | |
| 27 const std::string& access_token, | |
| 28 const HostlistCallback& callback) { | |
| 29 | |
| 30 DVLOG(2) << "ChromotingHostListFetcher::RetrieveHostlist() called"; | |
| 31 | |
| 32 DCHECK(!access_token.empty()); | |
| 33 DCHECK(!callback.is_null()); | |
| 34 DCHECK(hostlist_callback_.is_null()); | |
| 35 | |
| 36 hostlist_callback_ = callback; | |
| 37 | |
| 38 request_context_getter_ = new remoting::URLRequestContextGetter( | |
| 39 base::ThreadTaskRunnerHandle::Get(), // network_runner | |
| 40 base::ThreadTaskRunnerHandle::Get()); // file_runner | |
| 41 | |
| 42 request_ = net::URLFetcher::Create( | |
| 43 GURL(kChromotingHostListProdRequestUrl), net::URLFetcher::GET, this); | |
| 44 request_->SetRequestContext(request_context_getter_.get()); | |
| 45 request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); | |
| 46 request_->Start(); | |
| 47 } | |
| 48 | |
| 49 bool ChromotingHostListFetcher::ProcessResponse( | |
| 50 std::vector<ChromotingHostInfo>* hostlist) { | |
| 51 int response_code = request_->GetResponseCode(); | |
| 52 if (response_code != net::HTTP_OK) { | |
| 53 LOG(ERROR) << "Hostlist request failed with error code: " << response_code; | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 std::string response_string; | |
| 58 if (!request_->GetResponseAsString(&response_string)) { | |
| 59 LOG(ERROR) << "Failed to retrieve Hostlist response data"; | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 scoped_ptr<base::Value> response_value( | |
| 64 base::JSONReader::Read(response_string)); | |
| 65 if (!response_value || | |
| 66 !response_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
| 67 LOG(ERROR) << "Failed to parse response string to JSON"; | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 const base::DictionaryValue* response; | |
| 72 if (!response_value->GetAsDictionary(&response)) { | |
| 73 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 const base::DictionaryValue* data = nullptr; | |
| 78 if (!response->GetDictionary("data", &data)) { | |
| 79 LOG(ERROR) << "Hostlist response data is empty"; | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 const base::ListValue* hosts = nullptr; | |
| 84 if (!data->GetList("items", &hosts)) { | |
| 85 LOG(ERROR) << "Failed to find hosts in Hostlist response data"; | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 // TODO(TonyChun): host_info that does not have all information (host_name, | |
| 90 // host_id, jabber_id, host_offline_reason, host_status, public key) will not | |
| 91 // 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.
| |
| 92 for (base::Value* host_info : *hosts) { | |
| 93 ChromotingHostInfo chromoting_host_info; | |
| 94 if (chromoting_host_info.ParseHostInfo( | |
| 95 *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.
| |
| 96 hostlist->push_back(chromoting_host_info); | |
| 97 } | |
| 98 } | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 void ChromotingHostListFetcher::OnURLFetchComplete( | |
| 103 const net::URLFetcher* source) { | |
| 104 DCHECK(source); | |
| 105 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); | |
| 106 | |
| 107 std::vector<ChromotingHostInfo> hostlist; | |
| 108 | |
| 109 // Any malformed data from the Directory will notify the callback that | |
| 110 // processing the response failed. | |
| 111 if (!ProcessResponse(&hostlist)) { | |
| 112 hostlist.clear(); | |
| 113 base::ResetAndReturn(&hostlist_callback_).Run(hostlist, false); | |
| 114 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.
| |
| 115 } | |
| 116 base::ResetAndReturn(&hostlist_callback_).Run(hostlist, true); | |
| 117 } | |
| 118 | |
| 119 } // namespace test | |
| 120 } // namespace remoting | |
| OLD | NEW |