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/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 HostListFetcher::HostListFetcher() { | |
21 } | |
22 | |
23 HostListFetcher::~HostListFetcher() { | |
24 } | |
25 | |
26 void HostListFetcher::RetrieveHostlist( | |
27 const std::string& access_token, | |
28 const HostlistCallback& callback) { | |
29 | |
30 DVLOG(2) << "HostListFetcher::RetrieveHostlist() called"; | |
joedow
2015/07/09 03:09:56
VLOG?
tonychun
2015/07/09 17:36:22
Done.
| |
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(kHostListProdRequestUrl), 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 HostListFetcher::ProcessResponse( | |
50 std::vector<HostInfo>* 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 // Any host_info with malformed data will not be added to the hostlist. | |
90 base::DictionaryValue* host_dict; | |
91 for (base::Value* host_info : *hosts) { | |
92 HostInfo host; | |
93 if (host_info->GetAsDictionary(&host_dict) && | |
94 host.ParseHostInfo(*host_dict)) { | |
95 hostlist->push_back(host); | |
96 } | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 void HostListFetcher::OnURLFetchComplete( | |
102 const net::URLFetcher* source) { | |
103 DCHECK(source); | |
104 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); | |
105 | |
106 std::vector<HostInfo> hostlist; | |
107 | |
108 if (!ProcessResponse(&hostlist)) { | |
109 hostlist.clear(); | |
110 } | |
111 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
112 } | |
113 | |
114 } // namespace test | |
115 } // namespace remoting | |
OLD | NEW |