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

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
« no previous file with comments | « chrome/browser/ui/webui/sessions_ui.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 int nav_index = tab->current_navigation_index;
171 // Range-check |nav_index| as it has been observed to be invalid sometimes.
172 if (nav_index < 0 ||
173 static_cast<size_t>(nav_index) >= tab->navigations.size()) {
174 continue;
175 }
176 const TabNavigation& nav = tab->navigations[nav_index];
177 scoped_ptr<DictionaryValue> tab_data(new DictionaryValue());
178 tab_data->SetInteger("id", tab->tab_id.id());
179 tab_data->SetDouble("timestamp",
180 static_cast<double> (tab->timestamp.ToInternalValue()));
181 if (nav.title().empty())
182 tab_data->SetString("title", nav.virtual_url().spec());
183 else
184 tab_data->SetString("title", nav.title());
185 tab_data->SetString("url", nav.virtual_url().spec());
186 tab_list->Append(tab_data.release());
187 }
188 }
189
190 void SessionsDOMHandler::GetWindowList(
191 const std::vector<SessionWindow*>& windows, ListValue* window_list) {
192 for (std::vector<SessionWindow*>::const_iterator it =
193 windows.begin(); it != windows.end(); ++it) {
194 const SessionWindow* window = *it;
195 scoped_ptr<DictionaryValue> window_data(new DictionaryValue());
196 window_data->SetInteger("id", window->window_id.id());
197 window_data->SetDouble("timestamp",
198 static_cast<double> (window->timestamp.ToInternalValue()));
199 scoped_ptr<ListValue> tab_list(new ListValue());
200 GetTabList(window->tabs, tab_list.get());
201 window_data->Set("tabs", tab_list.release());
202 window_list->Append(window_data.release());
203 }
204 }
205
206 void SessionsDOMHandler::GetSessionList(
207 const std::vector<const ForeignSession*>& sessions,
208 ListValue* session_list) {
209 for (std::vector<const ForeignSession*>::const_iterator it =
210 sessions.begin(); it != sessions.end(); ++it) {
211 const ForeignSession* session = *it;
212 scoped_ptr<DictionaryValue> session_data(new DictionaryValue());
213 session_data->SetString("tag", session->foreign_session_tag);
214 scoped_ptr<ListValue> window_list(new ListValue());
215 GetWindowList(session->windows, window_list.get());
216 session_data->Set("windows", window_list.release());
217 session_list->Append(session_data.release());
218 }
219 }
220
221 void SessionsDOMHandler::GetAllTabs(
222 const std::vector<const ForeignSession*>& sessions,
223 std::vector<SessionTab*>* all_tabs) {
224 for (size_t i = 0; i < sessions.size(); i++) {
225 const std::vector<SessionWindow*>& windows = sessions[i]->windows;
226 for (size_t j = 0; j < windows.size(); j++) {
227 const std::vector<SessionTab*>& tabs = windows[j]->tabs;
228 all_tabs->insert(all_tabs->end(), tabs.begin(), tabs.end());
229 }
230 }
231 }
232
233 // Comparator function for sort() in CreateMagicTabList() below.
234 bool CompareTabsByTimestamp(SessionTab* lhs, SessionTab* rhs) {
235 return lhs->timestamp.ToInternalValue() > rhs->timestamp.ToInternalValue();
236 }
237
238 void SessionsDOMHandler::CreateMagicTabList(
239 const std::vector<const ForeignSession*>& sessions,
240 ListValue* tab_list) {
241 std::vector<SessionTab*> all_tabs;
242 GetAllTabs(sessions, &all_tabs);
243
244 // Sort the list by timestamp - newest first.
245 std::sort(all_tabs.begin(), all_tabs.end(), CompareTabsByTimestamp);
246
247 // Truncate the list if necessary.
248 const size_t kMagicTabListMaxSize = 10;
249 if (all_tabs.size() > kMagicTabListMaxSize)
250 all_tabs.resize(kMagicTabListMaxSize);
251
252 GetTabList(all_tabs, tab_list);
253 }
254
255 void SessionsDOMHandler::UpdateUI() {
256 ListValue session_list;
257 ListValue magic_list;
258
259 browser_sync::SessionModelAssociator* associator = GetModelAssociator();
260 // Make sure the associator has been created.
261 if (associator) {
262 std::vector<const ForeignSession*> sessions;
263 if (associator->GetAllForeignSessions(&sessions)) {
264 GetSessionList(sessions, &session_list);
265 CreateMagicTabList(sessions, &magic_list);
266 }
267 }
268
269 // Send the results to JavaScript, even if the lists are empty, so that the
270 // UI can show a message that there is nothing.
271 web_ui_->CallJavascriptFunction("updateSessionList",
272 session_list,
273 magic_list);
274 }
275
276 } // namespace
277
278 ///////////////////////////////////////////////////////////////////////////////
279 //
280 // SessionsUI
281 //
282 ///////////////////////////////////////////////////////////////////////////////
283
284 SessionsUI::SessionsUI(TabContents* contents) : ChromeWebUI(contents) {
285 AddMessageHandler((new SessionsDOMHandler())->Attach(this));
286
287 SessionsUIHTMLSource* html_source = new SessionsUIHTMLSource();
288
289 // Set up the chrome://sessions/ source.
290 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
291 }
292
293 // static
294 RefCountedMemory* SessionsUI::GetFaviconResourceBytes() {
295 return ResourceBundle::GetSharedInstance().
296 LoadDataResourceBytes(IDR_HISTORY_FAVICON);
297 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/sessions_ui.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698