|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/sessions_ui.h" | |
6 | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/browser/sync/engine/syncapi.h" | |
9 #include "chrome/browser/sync/glue/session_model_associator.h" | |
10 #include "chrome/browser/sync/profile_sync_service.h" | |
11 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
12 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | |
13 #include "chrome/common/chrome_version_info.h" | |
14 #include "chrome/common/jstemplate_builder.h" | |
15 #include "chrome/common/url_constants.h" | |
16 #include "content/browser/webui/web_ui.h" | |
17 #include "grit/browser_resources.h" | |
18 #include "grit/chromium_strings.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "grit/theme_resources.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/base/resource/resource_bundle.h" | |
23 | |
24 | |
25 namespace { | |
26 | |
27 /////////////////////////////////////////////////////////////////////////////// | |
28 // | |
29 // SessionsUIHTMLSource | |
30 // | |
31 /////////////////////////////////////////////////////////////////////////////// | |
32 | |
33 class SessionsUIHTMLSource : public ChromeURLDataManager::DataSource { | |
34 public: | |
35 SessionsUIHTMLSource() | |
36 : DataSource(chrome::kChromeUISessionsHost, MessageLoop::current()) {} | |
37 | |
38 // Called when the network layer has requested a resource underneath | |
39 // the path we registered. | |
40 virtual void StartDataRequest(const std::string& path, | |
41 bool is_incognito, | |
42 int request_id); | |
43 virtual std::string GetMimeType(const std::string&) const { | |
44 return "text/html"; | |
45 } | |
46 | |
47 private: | |
48 ~SessionsUIHTMLSource() {} | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(SessionsUIHTMLSource); | |
51 }; | |
52 | |
53 void SessionsUIHTMLSource::StartDataRequest(const std::string& path, | |
54 bool is_incognito, | |
55 int request_id) { | |
56 DictionaryValue localized_strings; | |
57 localized_strings.SetString("sessionsTitle", | |
58 l10n_util::GetStringUTF16(IDS_SESSIONS_TITLE)); | |
59 localized_strings.SetString("sessionsCountFormat", | |
60 l10n_util::GetStringUTF16(IDS_SESSIONS_SESSION_COUNT_BANNER_FORMAT)); | |
61 localized_strings.SetString("noSessionsMessage", | |
62 l10n_util::GetStringUTF16(IDS_SESSIONS_NO_SESSIONS_MESSAGE)); | |
63 | |
64 localized_strings.SetString("magicCountFormat", | |
65 l10n_util::GetStringUTF16(IDS_SESSIONS_MAGIC_LIST_BANNER_FORMAT)); | |
66 localized_strings.SetString("noMagicMessage", | |
67 l10n_util::GetStringUTF16(IDS_SESSIONS_NO_MAGIC_MESSAGE)); | |
68 | |
69 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings); | |
70 | |
71 static const base::StringPiece sessions_html( | |
72 ResourceBundle::GetSharedInstance(). | |
73 GetRawDataResource(IDR_SESSIONS_HTML)); | |
74 std::string full_html = | |
75 jstemplate_builder::GetI18nTemplateHtml(sessions_html, | |
76 &localized_strings); | |
77 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); | |
78 | |
79 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
80 html_bytes->data.resize(full_html.size()); | |
81 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
82 | |
83 SendResponse(request_id, html_bytes); | |
84 } | |
85 | |
86 //////////////////////////////////////////////////////////////////////////////// | |
87 // | |
88 // SessionsDOMHandler | |
89 // | |
90 //////////////////////////////////////////////////////////////////////////////// | |
91 | |
92 // The handler for Javascript messages for the chrome://sessions/ page. | |
93 class SessionsDOMHandler : public WebUIMessageHandler { | |
94 public: | |
95 SessionsDOMHandler(); | |
96 virtual ~SessionsDOMHandler(); | |
97 | |
98 // WebUIMessageHandler implementation. | |
99 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | |
100 virtual void RegisterMessages(); | |
101 | |
102 private: | |
103 // Asynchronously fetches the session list. Called from JS. | |
104 void HandleRequestSessions(const ListValue* args); | |
105 | |
106 // Sends the recent session list to JS. | |
107 void UpdateUI(); | |
108 | |
109 // Returns the model associated for getting the list of sessions from sync. | |
110 browser_sync::SessionModelAssociator* GetModelAssociator(); | |
111 | |
112 // Appends each entry in |tabs| to |tab_list| as a DictonaryValue. | |
113 void GetTabList(const std::vector<SessionTab*>& tabs, ListValue* tab_list); | |
114 | |
115 // Appends each entry in |windows| to |window_list| as a DictonaryValue. | |
116 void GetWindowList(const std::vector<SessionWindow*>& windows, | |
117 ListValue* window_list); | |
118 | |
119 // Appends each entry in |sessions| to |session_list| as a DictonaryValue. | |
120 void GetSessionList(const std::vector<const ForeignSession*>& sessions, | |
121 ListValue* session_list); | |
122 | |
123 // Traverses all tabs in |sessions| and adds them to |all_tabs|. | |
124 void GetAllTabs(const std::vector<const ForeignSession*>& sessions, | |
125 std::vector<SessionTab*>* all_tabs); | |
126 | |
127 // Creates a "magic" list of tabs from all the sessions. | |
128 void CreateMagicTabList(const std::vector<const ForeignSession*>& sessions, | |
129 ListValue* tab_list); | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(SessionsDOMHandler); | |
132 }; | |
133 | |
134 SessionsDOMHandler::SessionsDOMHandler() { | |
135 } | |
136 | |
137 SessionsDOMHandler::~SessionsDOMHandler() { | |
138 } | |
139 | |
140 WebUIMessageHandler* SessionsDOMHandler::Attach(WebUI* web_ui) { | |
141 return WebUIMessageHandler::Attach(web_ui); | |
142 } | |
143 | |
144 void SessionsDOMHandler::RegisterMessages() { | |
145 web_ui_->RegisterMessageCallback("requestSessionList", | |
146 NewCallback(this, &SessionsDOMHandler::HandleRequestSessions)); | |
147 } | |
148 | |
149 void SessionsDOMHandler::HandleRequestSessions(const ListValue* args) { | |
150 UpdateUI(); | |
151 } | |
152 | |
153 browser_sync::SessionModelAssociator* SessionsDOMHandler::GetModelAssociator() { | |
154 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); | |
Nicolas Zea
2011/06/07 00:37:49
GetProfileSyncService performs lazy-creation of th
Alexei Svitkine (slow)
2011/06/07 14:52:46
Done.
| |
155 if (service == NULL) | |
156 return NULL; | |
157 // We only want to set the model associator if there is one, and it is done | |
158 // syncing sessions. | |
159 browser_sync::SessionModelAssociator* model_associator = service-> | |
160 GetSessionModelAssociator(); | |
161 if (model_associator == NULL || !service->ShouldPushChanges()) | |
162 return NULL; | |
163 return web_ui_->GetProfile()->GetProfileSyncService()-> | |
Nicolas Zea
2011/06/07 00:37:49
return "model_associator" instead.
Alexei Svitkine (slow)
2011/06/07 14:52:46
Refactored so GetSessionModelAssociator() is only
| |
164 GetSessionModelAssociator(); | |
165 } | |
166 | |
167 void SessionsDOMHandler::GetTabList( | |
168 const std::vector<SessionTab*>& tabs, ListValue* tab_list) { | |
169 for (std::vector<SessionTab*>::const_iterator it = tabs.begin(); | |
170 it != tabs.end(); ++it) { | |
171 SessionTab* tab = *it; | |
Nicolas Zea
2011/06/07 00:37:49
const SessionTab* tab?
Alexei Svitkine (slow)
2011/06/07 14:52:46
Done.
| |
172 const TabNavigation& nav = | |
173 tab->navigations[tab->current_navigation_index]; | |
174 scoped_ptr<DictionaryValue> tab_data(new DictionaryValue()); | |
175 tab_data->SetInteger("id", tab->tab_id.id()); | |
176 tab_data->SetDouble("timestamp", | |
177 static_cast<double> (tab->timestamp.ToInternalValue())); | |
178 if (nav.title().empty()) | |
179 tab_data->SetString("title", nav.virtual_url().spec()); | |
180 else | |
181 tab_data->SetString("title", nav.title()); | |
182 tab_data->SetString("url", nav.virtual_url().spec()); | |
183 tab_list->Append(tab_data.release()); | |
184 } | |
185 } | |
186 | |
187 void SessionsDOMHandler::GetWindowList( | |
188 const std::vector<SessionWindow*>& windows, ListValue* window_list) { | |
189 for (std::vector<SessionWindow*>::const_iterator it = | |
190 windows.begin(); it != windows.end(); ++it) { | |
191 SessionWindow* window = *it; | |
Nicolas Zea
2011/06/07 00:37:49
const here too
Alexei Svitkine (slow)
2011/06/07 14:52:46
Done.
| |
192 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | |
193 window_data->SetInteger("id", window->window_id.id()); | |
194 window_data->SetDouble("timestamp", | |
195 static_cast<double> (window->timestamp.ToInternalValue())); | |
196 scoped_ptr<ListValue> tab_list(new ListValue()); | |
197 GetTabList(window->tabs, tab_list.get()); | |
198 window_data->Set("tabs", tab_list.release()); | |
199 window_list->Append(window_data.release()); | |
200 } | |
201 } | |
202 | |
203 void SessionsDOMHandler::GetSessionList( | |
204 const std::vector<const ForeignSession*>& sessions, | |
205 ListValue* session_list) { | |
206 for (std::vector<const ForeignSession*>::const_iterator it = | |
207 sessions.begin(); it != sessions.end(); ++it) { | |
208 const ForeignSession* session = *it; | |
209 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | |
210 session_data->SetString("tag", session->foreign_session_tag); | |
211 scoped_ptr<ListValue> window_list(new ListValue()); | |
212 GetWindowList(session->windows, window_list.get()); | |
213 session_data->Set("windows", window_list.release()); | |
214 session_list->Append(session_data.release()); | |
215 } | |
216 } | |
217 | |
218 void SessionsDOMHandler::GetAllTabs( | |
219 const std::vector<const ForeignSession*>& sessions, | |
220 std::vector<SessionTab*>* all_tabs) { | |
221 for (size_t i = 0; i < sessions.size(); i++) { | |
222 const std::vector<SessionWindow*>& windows = sessions[i]->windows; | |
223 for (size_t j = 0; j < windows.size(); j++) { | |
224 const std::vector<SessionTab*>& tabs = windows[j]->tabs; | |
225 all_tabs->insert(all_tabs->end(), tabs.begin(), tabs.end()); | |
226 } | |
227 } | |
228 } | |
229 | |
230 // Comparator function for sort() in CreateMagicTabList() below. | |
231 bool CompareTabsByTimestamp(SessionTab* lhs, SessionTab* rhs) { | |
232 return lhs->timestamp.ToInternalValue() > rhs->timestamp.ToInternalValue(); | |
233 } | |
234 | |
235 void SessionsDOMHandler::CreateMagicTabList( | |
236 const std::vector<const ForeignSession*>& sessions, | |
237 ListValue* tab_list) { | |
238 std::vector<SessionTab*> all_tabs; | |
239 GetAllTabs(sessions, &all_tabs); | |
240 | |
241 // Sort the list by timestamp - newest first. | |
242 std::sort(all_tabs.begin(), all_tabs.end(), CompareTabsByTimestamp); | |
243 | |
244 // Truncate the list if necessary. | |
245 const size_t kMagicTabListMaxSize = 10; | |
246 if (all_tabs.size() > kMagicTabListMaxSize) | |
247 all_tabs.resize(kMagicTabListMaxSize); | |
248 | |
249 GetTabList(all_tabs, tab_list); | |
250 } | |
251 | |
252 void SessionsDOMHandler::UpdateUI() { | |
253 ListValue session_list; | |
254 ListValue magic_list; | |
255 | |
256 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); | |
257 // Make sure the associator has been created. | |
258 if (associator) { | |
259 std::vector<const ForeignSession*> sessions; | |
260 if (associator->GetAllForeignSessions(&sessions)) { | |
261 GetSessionList(sessions, &session_list); | |
262 CreateMagicTabList(sessions, &magic_list); | |
263 } | |
264 } | |
265 | |
266 // Send the results to JavaScript, even if the lists are empty, so that the | |
267 // UI can show a message that there is nothing. | |
268 web_ui_->CallJavascriptFunction("updateSessionList", | |
269 session_list, | |
270 magic_list); | |
271 } | |
272 | |
273 } // namespace | |
274 | |
275 /////////////////////////////////////////////////////////////////////////////// | |
276 // | |
277 // SessionsUI | |
278 // | |
279 /////////////////////////////////////////////////////////////////////////////// | |
280 | |
281 SessionsUI::SessionsUI(TabContents* contents) : WebUI(contents) { | |
282 AddMessageHandler((new SessionsDOMHandler())->Attach(this)); | |
283 | |
284 SessionsUIHTMLSource* html_source = new SessionsUIHTMLSource(); | |
285 | |
286 // Set up the chrome://sessions/ source. | |
287 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); | |
288 } | |
289 | |
290 // static | |
291 RefCountedMemory* SessionsUI::GetFaviconResourceBytes() { | |
292 return ResourceBundle::GetSharedInstance(). | |
293 LoadDataResourceBytes(IDR_HISTORY_FAVICON); | |
294 } | |
OLD | NEW |