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