OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "content/public/browser/web_ui.h" | 26 #include "content/public/browser/web_ui.h" |
27 | 27 |
28 namespace browser_sync { | 28 namespace browser_sync { |
29 | 29 |
30 // Maximum number of session we're going to display on the NTP | 30 // Maximum number of session we're going to display on the NTP |
31 static const size_t kMaxSessionsToShow = 10; | 31 static const size_t kMaxSessionsToShow = 10; |
32 | 32 |
33 // Invalid value, used to note that we don't have a tab or window number. | 33 // Invalid value, used to note that we don't have a tab or window number. |
34 static const int kInvalidId = -1; | 34 static const int kInvalidId = -1; |
35 | 35 |
| 36 namespace { |
| 37 |
| 38 // Comparator function for use with std::sort that will sort sessions by |
| 39 // descending modified_time (i.e., most recent first). |
| 40 bool SortSessionsByRecency(const SyncedSession* s1, const SyncedSession* s2) { |
| 41 return s1->modified_time > s2->modified_time; |
| 42 } |
| 43 |
| 44 } // namepace |
| 45 |
36 ForeignSessionHandler::ForeignSessionHandler() { | 46 ForeignSessionHandler::ForeignSessionHandler() { |
37 } | 47 } |
38 | 48 |
39 void ForeignSessionHandler::RegisterMessages() { | 49 void ForeignSessionHandler::RegisterMessages() { |
40 Init(); | 50 Init(); |
41 web_ui()->RegisterMessageCallback("getForeignSessions", | 51 web_ui()->RegisterMessageCallback("getForeignSessions", |
42 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, | 52 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, |
43 base::Unretained(this))); | 53 base::Unretained(this))); |
44 web_ui()->RegisterMessageCallback("openForeignSession", | 54 web_ui()->RegisterMessageCallback("openForeignSession", |
45 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, | 55 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 103 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
94 return service && service->GetSessionModelAssociator(); | 104 return service && service->GetSessionModelAssociator(); |
95 } | 105 } |
96 | 106 |
97 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { | 107 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
98 SessionModelAssociator* associator = GetModelAssociator(); | 108 SessionModelAssociator* associator = GetModelAssociator(); |
99 std::vector<const SyncedSession*> sessions; | 109 std::vector<const SyncedSession*> sessions; |
100 | 110 |
101 ListValue session_list; | 111 ListValue session_list; |
102 if (associator && associator->GetAllForeignSessions(&sessions)) { | 112 if (associator && associator->GetAllForeignSessions(&sessions)) { |
| 113 // Sort sessions from most recent to least recent. |
| 114 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
| 115 |
103 // Note: we don't own the SyncedSessions themselves. | 116 // Note: we don't own the SyncedSessions themselves. |
104 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { | 117 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { |
105 const SyncedSession* session = sessions[i]; | 118 const SyncedSession* session = sessions[i]; |
106 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | 119 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
107 session_data->SetString("tag", session->session_tag); | 120 session_data->SetString("tag", session->session_tag); |
108 session_data->SetString("name", session->session_name); | 121 session_data->SetString("name", session->session_name); |
109 scoped_ptr<ListValue> window_list(new ListValue()); | 122 scoped_ptr<ListValue> window_list(new ListValue()); |
110 for (SyncedSession::SyncedWindowMap::const_iterator it = | 123 for (SyncedSession::SyncedWindowMap::const_iterator it = |
111 session->windows.begin(); it != session->windows.end(); ++it) { | 124 session->windows.begin(); it != session->windows.end(); ++it) { |
112 SessionWindow* window = it->second; | 125 SessionWindow* window = it->second; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 return false; | 249 return false; |
237 dictionary->SetString("type", "window"); | 250 dictionary->SetString("type", "window"); |
238 dictionary->SetDouble("timestamp", | 251 dictionary->SetDouble("timestamp", |
239 static_cast<double>(window.timestamp.ToInternalValue())); | 252 static_cast<double>(window.timestamp.ToInternalValue())); |
240 dictionary->SetInteger("sessionId", window.window_id.id()); | 253 dictionary->SetInteger("sessionId", window.window_id.id()); |
241 dictionary->Set("tabs", tab_values.release()); | 254 dictionary->Set("tabs", tab_values.release()); |
242 return true; | 255 return true; |
243 } | 256 } |
244 | 257 |
245 } // namespace browser_sync | 258 } // namespace browser_sync |
OLD | NEW |