OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sessions_ui.h" | 5 #include "chrome/browser/ui/webui/sessions_ui.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/sync/glue/session_model_associator.h" | 10 #include "chrome/browser/sync/glue/session_model_associator.h" |
11 #include "chrome/browser/sync/glue/synced_session.h" | |
12 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/sync/profile_sync_service.h" |
13 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
14 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 13 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
15 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 14 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
16 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
17 #include "chrome/common/jstemplate_builder.h" | 16 #include "chrome/common/jstemplate_builder.h" |
18 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
19 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "content/browser/webui/web_ui.h" | 19 #include "content/browser/webui/web_ui.h" |
21 #include "grit/browser_resources.h" | 20 #include "grit/browser_resources.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 browser_sync::SessionModelAssociator* GetModelAssociator(); | 74 browser_sync::SessionModelAssociator* GetModelAssociator(); |
76 | 75 |
77 // Appends each entry in |tabs| to |tab_list| as a DictonaryValue. | 76 // Appends each entry in |tabs| to |tab_list| as a DictonaryValue. |
78 void GetTabList(const std::vector<SessionTab*>& tabs, ListValue* tab_list); | 77 void GetTabList(const std::vector<SessionTab*>& tabs, ListValue* tab_list); |
79 | 78 |
80 // Appends each entry in |windows| to |window_list| as a DictonaryValue. | 79 // Appends each entry in |windows| to |window_list| as a DictonaryValue. |
81 void GetWindowList(const std::vector<SessionWindow*>& windows, | 80 void GetWindowList(const std::vector<SessionWindow*>& windows, |
82 ListValue* window_list); | 81 ListValue* window_list); |
83 | 82 |
84 // Appends each entry in |sessions| to |session_list| as a DictonaryValue. | 83 // Appends each entry in |sessions| to |session_list| as a DictonaryValue. |
85 void GetSessionList( | 84 void GetSessionList(const std::vector<const SyncedSession*>& sessions, |
86 const std::vector<const browser_sync::SyncedSession*>& sessions, | 85 ListValue* session_list); |
87 ListValue* session_list); | |
88 | 86 |
89 // Traverses all tabs in |sessions| and adds them to |all_tabs|. | 87 // Traverses all tabs in |sessions| and adds them to |all_tabs|. |
90 void GetAllTabs( | 88 void GetAllTabs(const std::vector<const SyncedSession*>& sessions, |
91 const std::vector<const browser_sync::SyncedSession*>& sessions, | 89 std::vector<SessionTab*>* all_tabs); |
92 std::vector<SessionTab*>* all_tabs); | |
93 | 90 |
94 // Creates a "magic" list of tabs from all the sessions. | 91 // Creates a "magic" list of tabs from all the sessions. |
95 void CreateMagicTabList( | 92 void CreateMagicTabList(const std::vector<const SyncedSession*>& sessions, |
96 const std::vector<const browser_sync::SyncedSession*>& sessions, | 93 ListValue* tab_list); |
97 ListValue* tab_list); | |
98 | 94 |
99 DISALLOW_COPY_AND_ASSIGN(SessionsDOMHandler); | 95 DISALLOW_COPY_AND_ASSIGN(SessionsDOMHandler); |
100 }; | 96 }; |
101 | 97 |
102 SessionsDOMHandler::SessionsDOMHandler() { | 98 SessionsDOMHandler::SessionsDOMHandler() { |
103 } | 99 } |
104 | 100 |
105 SessionsDOMHandler::~SessionsDOMHandler() { | 101 SessionsDOMHandler::~SessionsDOMHandler() { |
106 } | 102 } |
107 | 103 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 window_data->SetDouble("timestamp", | 161 window_data->SetDouble("timestamp", |
166 static_cast<double> (window->timestamp.ToInternalValue())); | 162 static_cast<double> (window->timestamp.ToInternalValue())); |
167 scoped_ptr<ListValue> tab_list(new ListValue()); | 163 scoped_ptr<ListValue> tab_list(new ListValue()); |
168 GetTabList(window->tabs, tab_list.get()); | 164 GetTabList(window->tabs, tab_list.get()); |
169 window_data->Set("tabs", tab_list.release()); | 165 window_data->Set("tabs", tab_list.release()); |
170 window_list->Append(window_data.release()); | 166 window_list->Append(window_data.release()); |
171 } | 167 } |
172 } | 168 } |
173 | 169 |
174 void SessionsDOMHandler::GetSessionList( | 170 void SessionsDOMHandler::GetSessionList( |
175 const std::vector<const browser_sync::SyncedSession*>& sessions, | 171 const std::vector<const SyncedSession*>& sessions, |
176 ListValue* session_list) { | 172 ListValue* session_list) { |
177 for (std::vector<const browser_sync::SyncedSession*>::const_iterator it = | 173 for (std::vector<const SyncedSession*>::const_iterator it = |
178 sessions.begin(); it != sessions.end(); ++it) { | 174 sessions.begin(); it != sessions.end(); ++it) { |
179 const browser_sync::SyncedSession* session = *it; | 175 const SyncedSession* session = *it; |
180 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | 176 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
181 session_data->SetString("tag", session->session_tag); | 177 session_data->SetString("tag", session->session_tag); |
182 scoped_ptr<ListValue> window_list(new ListValue()); | 178 scoped_ptr<ListValue> window_list(new ListValue()); |
183 GetWindowList(session->windows, window_list.get()); | 179 GetWindowList(session->windows, window_list.get()); |
184 session_data->Set("windows", window_list.release()); | 180 session_data->Set("windows", window_list.release()); |
185 session_list->Append(session_data.release()); | 181 session_list->Append(session_data.release()); |
186 } | 182 } |
187 } | 183 } |
188 | 184 |
189 void SessionsDOMHandler::GetAllTabs( | 185 void SessionsDOMHandler::GetAllTabs( |
190 const std::vector<const browser_sync::SyncedSession*>& sessions, | 186 const std::vector<const SyncedSession*>& sessions, |
191 std::vector<SessionTab*>* all_tabs) { | 187 std::vector<SessionTab*>* all_tabs) { |
192 for (size_t i = 0; i < sessions.size(); i++) { | 188 for (size_t i = 0; i < sessions.size(); i++) { |
193 const std::vector<SessionWindow*>& windows = sessions[i]->windows; | 189 const std::vector<SessionWindow*>& windows = sessions[i]->windows; |
194 for (size_t j = 0; j < windows.size(); j++) { | 190 for (size_t j = 0; j < windows.size(); j++) { |
195 const std::vector<SessionTab*>& tabs = windows[j]->tabs; | 191 const std::vector<SessionTab*>& tabs = windows[j]->tabs; |
196 all_tabs->insert(all_tabs->end(), tabs.begin(), tabs.end()); | 192 all_tabs->insert(all_tabs->end(), tabs.begin(), tabs.end()); |
197 } | 193 } |
198 } | 194 } |
199 } | 195 } |
200 | 196 |
201 // Comparator function for sort() in CreateMagicTabList() below. | 197 // Comparator function for sort() in CreateMagicTabList() below. |
202 bool CompareTabsByTimestamp(SessionTab* lhs, SessionTab* rhs) { | 198 bool CompareTabsByTimestamp(SessionTab* lhs, SessionTab* rhs) { |
203 return lhs->timestamp.ToInternalValue() > rhs->timestamp.ToInternalValue(); | 199 return lhs->timestamp.ToInternalValue() > rhs->timestamp.ToInternalValue(); |
204 } | 200 } |
205 | 201 |
206 void SessionsDOMHandler::CreateMagicTabList( | 202 void SessionsDOMHandler::CreateMagicTabList( |
207 const std::vector<const browser_sync::SyncedSession*>& sessions, | 203 const std::vector<const SyncedSession*>& sessions, |
208 ListValue* tab_list) { | 204 ListValue* tab_list) { |
209 std::vector<SessionTab*> all_tabs; | 205 std::vector<SessionTab*> all_tabs; |
210 GetAllTabs(sessions, &all_tabs); | 206 GetAllTabs(sessions, &all_tabs); |
211 | 207 |
212 // Sort the list by timestamp - newest first. | 208 // Sort the list by timestamp - newest first. |
213 std::sort(all_tabs.begin(), all_tabs.end(), CompareTabsByTimestamp); | 209 std::sort(all_tabs.begin(), all_tabs.end(), CompareTabsByTimestamp); |
214 | 210 |
215 // Truncate the list if necessary. | 211 // Truncate the list if necessary. |
216 const size_t kMagicTabListMaxSize = 10; | 212 const size_t kMagicTabListMaxSize = 10; |
217 if (all_tabs.size() > kMagicTabListMaxSize) | 213 if (all_tabs.size() > kMagicTabListMaxSize) |
218 all_tabs.resize(kMagicTabListMaxSize); | 214 all_tabs.resize(kMagicTabListMaxSize); |
219 | 215 |
220 GetTabList(all_tabs, tab_list); | 216 GetTabList(all_tabs, tab_list); |
221 } | 217 } |
222 | 218 |
223 void SessionsDOMHandler::UpdateUI() { | 219 void SessionsDOMHandler::UpdateUI() { |
224 ListValue session_list; | 220 ListValue session_list; |
225 ListValue magic_list; | 221 ListValue magic_list; |
226 | 222 |
227 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); | 223 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); |
228 // Make sure the associator has been created. | 224 // Make sure the associator has been created. |
229 if (associator) { | 225 if (associator) { |
230 std::vector<const browser_sync::SyncedSession*> sessions; | 226 std::vector<const SyncedSession*> sessions; |
231 if (associator->GetAllForeignSessions(&sessions)) { | 227 if (associator->GetAllForeignSessions(&sessions)) { |
232 GetSessionList(sessions, &session_list); | 228 GetSessionList(sessions, &session_list); |
233 CreateMagicTabList(sessions, &magic_list); | 229 CreateMagicTabList(sessions, &magic_list); |
234 } | 230 } |
235 } | 231 } |
236 | 232 |
237 // Send the results to JavaScript, even if the lists are empty, so that the | 233 // Send the results to JavaScript, even if the lists are empty, so that the |
238 // UI can show a message that there is nothing. | 234 // UI can show a message that there is nothing. |
239 web_ui_->CallJavascriptFunction("updateSessionList", | 235 web_ui_->CallJavascriptFunction("updateSessionList", |
240 session_list, | 236 session_list, |
(...skipping 15 matching lines...) Expand all Loading... |
256 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 252 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
257 profile->GetChromeURLDataManager()->AddDataSource( | 253 profile->GetChromeURLDataManager()->AddDataSource( |
258 CreateSessionsUIHTMLSource()); | 254 CreateSessionsUIHTMLSource()); |
259 } | 255 } |
260 | 256 |
261 // static | 257 // static |
262 RefCountedMemory* SessionsUI::GetFaviconResourceBytes() { | 258 RefCountedMemory* SessionsUI::GetFaviconResourceBytes() { |
263 return ResourceBundle::GetSharedInstance(). | 259 return ResourceBundle::GetSharedInstance(). |
264 LoadDataResourceBytes(IDR_HISTORY_FAVICON); | 260 LoadDataResourceBytes(IDR_HISTORY_FAVICON); |
265 } | 261 } |
OLD | NEW |