Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: remoting/client/ios/facade/host_list_fetcher.cc

Issue 2871993003: Moving the iOS directory to be remoting top level. (Closed)
Patch Set: //remoting/ios was the old landing target for the internal iOS application. Fix. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // TODO(nicholss): There is a bug here if two host list fetches are happening
33 // at the same time there will be a dcheck thrown. Fix this for release.
34 DCHECK(!access_token.empty());
35 DCHECK(callback);
36 DCHECK(!hostlist_callback_);
37
38 hostlist_callback_ = callback;
39
40 request_ = net::URLFetcher::Create(GURL(kHostListProdRequestUrl),
41 net::URLFetcher::GET, this);
42 request_->SetRequestContext(url_request_context_getter_.get());
43 request_->AddExtraRequestHeader("Authorization: OAuth " + access_token);
44 request_->SetMaxRetriesOn5xx(0);
45 request_->SetAutomaticallyRetryOnNetworkChanges(3);
46 request_->Start();
47 }
48
49 bool HostListFetcher::ProcessResponse(
50 std::vector<remoting::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 std::unique_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 const base::DictionaryValue* host_dict;
91 for (const auto& host_info : *hosts) {
92 remoting::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(const net::URLFetcher* source) {
102 DCHECK(source);
103
104 std::vector<HostInfo> hostlist;
105 if (!ProcessResponse(&hostlist)) {
106 hostlist.clear();
107 }
108 base::ResetAndReturn(&hostlist_callback_).Run(hostlist);
109 }
110
111 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/ios/facade/host_list_fetcher.h ('k') | remoting/client/ios/facade/ios_client_runtime_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698