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

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

Issue 2912293003: [CRD iOS] Consistent host list order and showing last online time (Closed)
Patch Set: Fix comment Created 3 years, 6 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/ios/facade/host_list_fetcher.h" 5 #include "remoting/ios/facade/host_list_fetcher.h"
6 6
7 #include <algorithm>
7 #include <thread> 8 #include <thread>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/callback_helpers.h" 11 #include "base/callback_helpers.h"
11 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "net/http/http_status_code.h" 16 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h" 17 #include "net/url_request/url_fetcher.h"
17 #include "remoting/base/url_request_context_getter.h" 18 #include "remoting/base/url_request_context_getter.h"
18 19
19 namespace remoting { 20 namespace remoting {
20 21
22 namespace {
23
24 // Returns true if |h1| should sort before |h2|.
25 bool compareHost(const HostInfo& h1, const HostInfo& h2) {
26 // Online hosts always sort before offline hosts.
27 if (h1.status != h2.status) {
28 return h1.status == HostStatus::kHostStatusOnline;
29 }
30
31 // Sort by host name.
32 int name_compare = h1.host_name.compare(h2.host_name);
33 if (name_compare != 0) {
34 return name_compare < 0;
35 }
36
37 // Sort by last update time if names are identical.
38 return h1.updated_time < h2.updated_time;
39 }
40
41 } // namespace
42
21 HostListFetcher::HostListFetcher( 43 HostListFetcher::HostListFetcher(
22 const scoped_refptr<net::URLRequestContextGetter>& 44 const scoped_refptr<net::URLRequestContextGetter>&
23 url_request_context_getter) 45 url_request_context_getter)
24 : url_request_context_getter_(url_request_context_getter) {} 46 : url_request_context_getter_(url_request_context_getter) {}
25 47
26 HostListFetcher::~HostListFetcher() {} 48 HostListFetcher::~HostListFetcher() {}
27 49
28 // TODO(nicholss): This was written assuming only one request at a time. Fix 50 // 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. 51 // that. For the moment it will work to make progress in the app.
30 void HostListFetcher::RetrieveHostlist(const std::string& access_token, 52 void HostListFetcher::RetrieveHostlist(const std::string& access_token,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 return true; 120 return true;
99 } 121 }
100 122
101 void HostListFetcher::OnURLFetchComplete(const net::URLFetcher* source) { 123 void HostListFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
102 DCHECK(source); 124 DCHECK(source);
103 125
104 std::vector<HostInfo> hostlist; 126 std::vector<HostInfo> hostlist;
105 if (!ProcessResponse(&hostlist)) { 127 if (!ProcessResponse(&hostlist)) {
106 hostlist.clear(); 128 hostlist.clear();
107 } 129 }
130 std::sort(hostlist.begin(), hostlist.end(), &compareHost);
108 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); 131 base::ResetAndReturn(&hostlist_callback_).Run(hostlist);
109 } 132 }
110 133
111 } // namespace remoting 134 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698