Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/client/ios/facade/host_list_fetcher.h" | |
| 6 | |
| 7 #include <thread> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "base/values.h" | |
| 15 #include "net/http/http_status_code.h" | |
| 16 #include "net/url_request/url_fetcher.h" | |
| 17 #include "remoting/base/url_request_context_getter.h" | |
| 18 | |
| 19 namespace remoting { | |
| 20 | |
| 21 HostListFetcher::HostListFetcher( | |
| 22 const scoped_refptr<net::URLRequestContextGetter>& | |
| 23 url_request_context_getter) | |
| 24 : url_request_context_getter_(url_request_context_getter) {} | |
| 25 | |
| 26 HostListFetcher::~HostListFetcher() {} | |
| 27 | |
| 28 // TODO(nicholss): This was written assuming only one request at a time. Fix | |
| 29 // that. For the moment it will work to make progress in the app. | |
| 30 void HostListFetcher::RetrieveHostlist(const std::string& access_token, | |
| 31 const HostlistCallback& callback) { | |
| 32 DCHECK(!access_token.empty()); | |
| 33 DCHECK(!callback.is_null()); | |
| 34 DCHECK(hostlist_callback_.is_null()); | |
|
joedow
2017/04/05 22:30:55
nit: this was written with the old Callback syntax
nicholss
2017/04/07 18:16:15
Done.
| |
| 35 | |
| 36 hostlist_callback_ = callback; | |
| 37 | |
| 38 request_ = net::URLFetcher::Create(GURL(kHostListProdRequestUrl), | |
| 39 net::URLFetcher::GET, this); | |
| 40 request_->SetRequestContext(url_request_context_getter_.get()); | |
| 41 request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); | |
| 42 request_->SetMaxRetriesOn5xx(0); | |
| 43 request_->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 44 request_->Start(); | |
| 45 } | |
| 46 | |
| 47 bool HostListFetcher::ProcessResponse( | |
| 48 std::vector<remoting::HostInfo>* hostlist) { | |
| 49 int response_code = request_->GetResponseCode(); | |
| 50 if (response_code != net::HTTP_OK) { | |
| 51 LOG(ERROR) << "Hostlist request failed with error code: " << response_code; | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 std::string response_string; | |
| 56 if (!request_->GetResponseAsString(&response_string)) { | |
| 57 LOG(ERROR) << "Failed to retrieve Hostlist response data"; | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 std::unique_ptr<base::Value> response_value( | |
| 62 base::JSONReader::Read(response_string)); | |
| 63 if (!response_value || | |
| 64 !response_value->IsType(base::Value::Type::DICTIONARY)) { | |
| 65 LOG(ERROR) << "Failed to parse response string to JSON"; | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 const base::DictionaryValue* response; | |
| 70 if (!response_value->GetAsDictionary(&response)) { | |
| 71 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 const base::DictionaryValue* data = nullptr; | |
| 76 if (!response->GetDictionary("data", &data)) { | |
| 77 LOG(ERROR) << "Hostlist response data is empty"; | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 const base::ListValue* hosts = nullptr; | |
| 82 if (!data->GetList("items", &hosts)) { | |
| 83 LOG(ERROR) << "Failed to find hosts in Hostlist response data"; | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 // Any host_info with malformed data will not be added to the hostlist. | |
| 88 base::DictionaryValue* host_dict; | |
| 89 for (const auto& host_info : *hosts) { | |
| 90 remoting::HostInfo host; | |
| 91 if (host_info->GetAsDictionary(&host_dict) && | |
| 92 host.ParseHostInfo(*host_dict)) { | |
| 93 hostlist->push_back(host); | |
| 94 } | |
| 95 } | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 void HostListFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 100 DCHECK(source); | |
| 101 | |
| 102 std::vector<HostInfo> hostlist; | |
| 103 if (!ProcessResponse(&hostlist)) { | |
| 104 hostlist.clear(); | |
| 105 } | |
| 106 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
| 107 } | |
| 108 | |
| 109 } // namespace remoting | |
| OLD | NEW |