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

Side by Side Diff: chrome/browser/ui/webui/sessions_ui.cc

Issue 6969016: Adding chrome://sessions webui for prototyping NTP stuff. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 // We only want to get the model associator if there is one, and it is done
155 // syncing sessions.
156 Profile* profile = web_ui_->GetProfile();
157 if (!profile->HasProfileSyncService())
158 return NULL;
159 ProfileSyncService* service = profile->GetProfileSyncService();
160 if (!service->ShouldPushChanges())
161 return NULL;
162 return service->GetSessionModelAssociator();
163 }
164
165 void SessionsDOMHandler::GetTabList(
166 const std::vector<SessionTab*>& tabs, ListValue* tab_list) {
167 for (std::vector<SessionTab*>::const_iterator it = tabs.begin();
168 it != tabs.end(); ++it) {
169 const SessionTab* tab = *it;
170 const TabNavigation& nav =
171 tab->navigations[tab->current_navigation_index];
172 scoped_ptr<DictionaryValue> tab_data(new DictionaryValue());
173 tab_data->SetInteger("id", tab->tab_id.id());
174 tab_data->SetDouble("timestamp",
175 static_cast<double> (tab->timestamp.ToInternalValue()));
176 if (nav.title().empty())
177 tab_data->SetString("title", nav.virtual_url().spec());
178 else
179 tab_data->SetString("title", nav.title());
180 tab_data->SetString("url", nav.virtual_url().spec());
181 tab_list->Append(tab_data.release());
182 }
183 }
184
185 void SessionsDOMHandler::GetWindowList(
186 const std::vector<SessionWindow*>& windows, ListValue* window_list) {
187 for (std::vector<SessionWindow*>::const_iterator it =
188 windows.begin(); it != windows.end(); ++it) {
189 const SessionWindow* window = *it;
190 scoped_ptr<DictionaryValue> window_data(new DictionaryValue());
191 window_data->SetInteger("id", window->window_id.id());
192 window_data->SetDouble("timestamp",
193 static_cast<double> (window->timestamp.ToInternalValue()));
194 scoped_ptr<ListValue> tab_list(new ListValue());
195 GetTabList(window->tabs, tab_list.get());
196 window_data->Set("tabs", tab_list.release());
197 window_list->Append(window_data.release());
198 }
199 }
200
201 void SessionsDOMHandler::GetSessionList(
202 const std::vector<const ForeignSession*>& sessions,
203 ListValue* session_list) {
204 for (std::vector<const ForeignSession*>::const_iterator it =
205 sessions.begin(); it != sessions.end(); ++it) {
206 const ForeignSession* session = *it;
207 scoped_ptr<DictionaryValue> session_data(new DictionaryValue());
208 session_data->SetString("tag", session->foreign_session_tag);
209 scoped_ptr<ListValue> window_list(new ListValue());
210 GetWindowList(session->windows, window_list.get());
211 session_data->Set("windows", window_list.release());
212 session_list->Append(session_data.release());
213 }
214 }
215
216 void SessionsDOMHandler::GetAllTabs(
217 const std::vector<const ForeignSession*>& sessions,
218 std::vector<SessionTab*>* all_tabs) {
219 for (size_t i = 0; i < sessions.size(); i++) {
220 const std::vector<SessionWindow*>& windows = sessions[i]->windows;
221 for (size_t j = 0; j < windows.size(); j++) {
222 const std::vector<SessionTab*>& tabs = windows[j]->tabs;
223 all_tabs->insert(all_tabs->end(), tabs.begin(), tabs.end());
224 }
225 }
226 }
227
228 // Comparator function for sort() in CreateMagicTabList() below.
229 bool CompareTabsByTimestamp(SessionTab* lhs, SessionTab* rhs) {
230 return lhs->timestamp.ToInternalValue() > rhs->timestamp.ToInternalValue();
231 }
232
233 void SessionsDOMHandler::CreateMagicTabList(
234 const std::vector<const ForeignSession*>& sessions,
235 ListValue* tab_list) {
236 std::vector<SessionTab*> all_tabs;
237 GetAllTabs(sessions, &all_tabs);
238
239 // Sort the list by timestamp - newest first.
240 std::sort(all_tabs.begin(), all_tabs.end(), CompareTabsByTimestamp);
241
242 // Truncate the list if necessary.
243 const size_t kMagicTabListMaxSize = 10;
244 if (all_tabs.size() > kMagicTabListMaxSize)
245 all_tabs.resize(kMagicTabListMaxSize);
246
247 GetTabList(all_tabs, tab_list);
248 }
249
250 void SessionsDOMHandler::UpdateUI() {
251 ListValue session_list;
252 ListValue magic_list;
253
254 browser_sync::SessionModelAssociator* associator = GetModelAssociator();
255 // Make sure the associator has been created.
256 if (associator) {
257 std::vector<const ForeignSession*> sessions;
258 if (associator->GetAllForeignSessions(&sessions)) {
259 GetSessionList(sessions, &session_list);
260 CreateMagicTabList(sessions, &magic_list);
261 }
262 }
263
264 // Send the results to JavaScript, even if the lists are empty, so that the
265 // UI can show a message that there is nothing.
266 web_ui_->CallJavascriptFunction("updateSessionList",
267 session_list,
268 magic_list);
269 }
270
271 } // namespace
272
273 ///////////////////////////////////////////////////////////////////////////////
274 //
275 // SessionsUI
276 //
277 ///////////////////////////////////////////////////////////////////////////////
278
279 SessionsUI::SessionsUI(TabContents* contents) : WebUI(contents) {
280 AddMessageHandler((new SessionsDOMHandler())->Attach(this));
281
282 SessionsUIHTMLSource* html_source = new SessionsUIHTMLSource();
283
284 // Set up the chrome://sessions/ source.
285 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
286 }
287
288 // static
289 RefCountedMemory* SessionsUI::GetFaviconResourceBytes() {
290 return ResourceBundle::GetSharedInstance().
291 LoadDataResourceBytes(IDR_HISTORY_FAVICON);
292 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698