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 bool 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), | |
44 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.
| |
45 request_->SetRequestContext(request_context_getter_.get()); | |
46 request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); | |
47 request_->Start(); | |
48 | |
49 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.
| |
50 } | |
51 | |
52 void ChromotingHostListFetcher::OnURLFetchComplete( | |
53 const net::URLFetcher* source) { | |
54 DCHECK(source); | |
55 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); | |
56 | |
57 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.
| |
58 | |
59 // 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.
| |
60 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.
| |
61 hostlist_callback_.Reset(); | |
62 | |
joedow
2015/07/06 22:19:12
remove newline
tonychun
2015/07/08 03:12:14
Done.
| |
63 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.
| |
64 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.
| |
65 | |
66 int response_code = request_->GetResponseCode(); | |
67 if (response_code != net::HTTP_OK) { | |
68 LOG(ERROR) << "Hostlist request failed with error code: " << response_code; | |
69 return; | |
70 } | |
71 | |
72 std::string response_string; | |
73 if (!request_->GetResponseAsString(&response_string)) { | |
74 LOG(ERROR) << "Failed to retrieve Hostlist response data"; | |
75 return; | |
76 } | |
77 | |
78 scoped_ptr<base::Value> response_value( | |
79 base::JSONReader::Read(response_string)); | |
80 if (!response_value || | |
81 !response_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
82 LOG(ERROR) << "Failed to parse response string to JSON"; | |
83 return; | |
84 } | |
85 | |
86 const base::DictionaryValue* response; | |
87 if (!response_value->GetAsDictionary(&response)) { | |
88 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; | |
89 return; | |
90 } | |
91 | |
92 const base::DictionaryValue* data = nullptr; | |
93 response->GetDictionary("data", &data); | |
94 | |
joedow
2015/07/06 22:19:13
remove newline
tonychun
2015/07/08 03:12:14
Done.
| |
95 if (!data) { | |
Sergey Ulanov
2015/07/07 00:01:46
use GetDictionary() result here
tonychun
2015/07/08 03:12:13
Done.
| |
96 LOG(ERROR) << "Hostlist response data is empty"; | |
97 return; | |
98 } | |
99 | |
100 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.
| |
101 data->GetList("items", &items); | |
102 | |
joedow
2015/07/06 22:19:13
remove newline
tonychun
2015/07/08 03:12:13
Done.
| |
103 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.
| |
104 LOG(ERROR) << "Failed to find hosts in Hostlist response data"; | |
105 return; | |
106 } | |
107 | |
108 int size = items->GetSize(); | |
109 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.
| |
110 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.
| |
111 items->GetDictionary(i, &item); | |
112 hostlist_.push_back(ChromotingHostInfo(*item)); | |
113 } | |
114 } | |
115 | |
116 } // namespace test | |
117 } // namespace remoting | |
OLD | NEW |