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

Side by Side Diff: chrome/browser/ui/webui/ntp/foreign_session_handler.cc

Issue 11009013: NTP5: Starting implementation of a native menu for showing other device sessions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing Vadim's comment. Created 8 years, 2 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/ntp/foreign_session_handler.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/ntp/foreign_session_handler.h" 5 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/i18n/time_formatting.h" 12 #include "base/i18n/time_formatting.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "base/string_number_conversions.h" 14 #include "base/string_number_conversions.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/prefs/pref_service.h" 17 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/prefs/scoped_user_pref_update.h" 18 #include "chrome/browser/prefs/scoped_user_pref_update.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/sessions/session_restore.h" 20 #include "chrome/browser/sessions/session_restore.h"
21 #include "chrome/browser/sync/profile_sync_service.h" 21 #include "chrome/browser/sync/profile_sync_service.h"
22 #include "chrome/browser/sync/profile_sync_service_factory.h" 22 #include "chrome/browser/sync/profile_sync_service_factory.h"
23 #include "chrome/browser/ui/search/other_device_menu.h"
23 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 24 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
24 #include "chrome/browser/ui/webui/session_favicon_source.h" 25 #include "chrome/browser/ui/webui/session_favicon_source.h"
25 #include "chrome/browser/ui/webui/web_ui_util.h" 26 #include "chrome/browser/ui/webui/web_ui_util.h"
26 #include "chrome/common/chrome_notification_types.h" 27 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
28 #include "chrome/common/time_format.h" 29 #include "chrome/common/time_format.h"
29 #include "chrome/common/url_constants.h" 30 #include "chrome/common/url_constants.h"
30 #include "content/public/browser/notification_service.h" 31 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_source.h" 32 #include "content/public/browser/notification_source.h"
32 #include "content/public/browser/web_ui.h"
33 #include "grit/generated_resources.h" 33 #include "grit/generated_resources.h"
34 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
35 35
36 namespace browser_sync { 36 namespace browser_sync {
37 37
38 // Maximum number of session we're going to display on the NTP 38 // Maximum number of sessions we're going to display on the NTP
39 static const size_t kMaxSessionsToShow = 10; 39 static const size_t kMaxSessionsToShow = 10;
40 40
41 // Invalid value, used to note that we don't have a tab or window number.
42 static const int kInvalidId = -1;
43
44 namespace { 41 namespace {
45 42
46 // Comparator function for use with std::sort that will sort sessions by 43 // Comparator function for use with std::sort that will sort sessions by
47 // descending modified_time (i.e., most recent first). 44 // descending modified_time (i.e., most recent first).
48 bool SortSessionsByRecency(const SyncedSession* s1, const SyncedSession* s2) { 45 bool SortSessionsByRecency(const SyncedSession* s1, const SyncedSession* s2) {
49 return s1->modified_time > s2->modified_time; 46 return s1->modified_time > s2->modified_time;
50 } 47 }
51 48
52 } // namepace 49 } // namepace
53 50
54 ForeignSessionHandler::ForeignSessionHandler() { 51 ForeignSessionHandler::ForeignSessionHandler() {
55 } 52 }
56 53
57 // static 54 // static
58 void ForeignSessionHandler::RegisterUserPrefs(PrefService* prefs) { 55 void ForeignSessionHandler::RegisterUserPrefs(PrefService* prefs) {
59 prefs->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions, 56 prefs->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions,
60 PrefService::UNSYNCABLE_PREF); 57 PrefService::UNSYNCABLE_PREF);
61 } 58 }
62 59
60 // static
61 void ForeignSessionHandler::OpenForeignSession(
62 content::WebUI* web_ui,
63 const std::string& session_string_value,
64 SessionID::id_type window_num,
65 SessionID::id_type tab_id,
66 const WindowOpenDisposition& disposition) {
67
68 SessionModelAssociator* associator = GetModelAssociator(web_ui);
69 if (!associator)
70 return;
71
72 if (tab_id != kInvalidId) {
73 // We don't actually care about |window_num|, this is just a sanity check.
74 DCHECK_LT(kInvalidId, window_num);
75 const SessionTab* tab;
76 if (!associator->GetForeignTab(session_string_value, tab_id, &tab)) {
77 LOG(ERROR) << "Failed to load foreign tab.";
78 return;
79 }
80 SessionRestore::RestoreForeignSessionTab(
81 web_ui->GetWebContents(), *tab, disposition);
82 } else {
83 std::vector<const SessionWindow*> windows;
84 // Note: we don't own the ForeignSessions themselves.
85 if (!associator->GetForeignSession(session_string_value, &windows)) {
86 LOG(ERROR) << "ForeignSessionHandler failed to get session data from"
87 "SessionModelAssociator.";
88 return;
89 }
90 std::vector<const SessionWindow*>::const_iterator iter_begin =
91 windows.begin() + ((window_num == kInvalidId) ? 0 : window_num);
92 std::vector<const SessionWindow*>::const_iterator iter_end =
93 ((window_num == kInvalidId) ?
94 std::vector<const SessionWindow*>::const_iterator(windows.end()) :
95 iter_begin + 1);
96 SessionRestore::RestoreForeignSessionWindows(
97 Profile::FromWebUI(web_ui), iter_begin, iter_end);
98 }
99 }
100
101 // static
102 bool ForeignSessionHandler::SessionTabToValue(
103 const SessionTab& tab,
104 DictionaryValue* dictionary) {
105 if (tab.navigations.empty())
106 return false;
107 int selected_index = tab.current_navigation_index;
108 selected_index = std::max(
109 0,
110 std::min(selected_index,
111 static_cast<int>(tab.navigations.size() - 1)));
112 const TabNavigation& current_navigation =
113 tab.navigations.at(selected_index);
114 GURL tab_url = current_navigation.virtual_url();
115 if (tab_url == GURL(chrome::kChromeUINewTabURL))
116 return false;
117 NewTabUI::SetUrlTitleAndDirection(dictionary, current_navigation.title(),
118 tab_url);
119 dictionary->SetString("type", "tab");
120 dictionary->SetDouble("timestamp",
121 static_cast<double>(tab.timestamp.ToInternalValue()));
122 // TODO(jeremycho): Rename to tabId?
123 dictionary->SetInteger("sessionId", tab.tab_id.id());
124 return true;
125 }
126
127 // static
128 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator(
129 content::WebUI* web_ui) {
130 Profile* profile = Profile::FromWebUI(web_ui);
131 ProfileSyncService* service =
132 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
133
134 // Only return the associator if it exists and it is done syncing sessions.
135 if (service && service->ShouldPushChanges())
136 return service->GetSessionModelAssociator();
137
138 return NULL;
139 }
140
63 void ForeignSessionHandler::RegisterMessages() { 141 void ForeignSessionHandler::RegisterMessages() {
64 Init(); 142 Init();
65 web_ui()->RegisterMessageCallback("getForeignSessions", 143 web_ui()->RegisterMessageCallback("getForeignSessions",
66 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, 144 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions,
67 base::Unretained(this))); 145 base::Unretained(this)));
68 web_ui()->RegisterMessageCallback("openForeignSession", 146 web_ui()->RegisterMessageCallback("openForeignSession",
69 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, 147 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession,
70 base::Unretained(this))); 148 base::Unretained(this)));
71 web_ui()->RegisterMessageCallback("setForeignSessionCollapsed", 149 web_ui()->RegisterMessageCallback("setForeignSessionCollapsed",
72 base::Bind(&ForeignSessionHandler::HandleSetForeignSessionCollapsed, 150 base::Bind(&ForeignSessionHandler::HandleSetForeignSessionCollapsed,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 // Fall through. 184 // Fall through.
107 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: 185 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE:
108 case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: 186 case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED:
109 HandleGetForeignSessions(&list_value); 187 HandleGetForeignSessions(&list_value);
110 break; 188 break;
111 default: 189 default:
112 NOTREACHED(); 190 NOTREACHED();
113 } 191 }
114 } 192 }
115 193
116 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() {
117 Profile* profile = Profile::FromWebUI(web_ui());
118 ProfileSyncService* service =
119 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
120
121 // Only return the associator if it exists and it is done syncing sessions.
122 if (service && service->ShouldPushChanges())
123 return service->GetSessionModelAssociator();
124
125 return NULL;
126 }
127 194
128 bool ForeignSessionHandler::IsTabSyncEnabled() { 195 bool ForeignSessionHandler::IsTabSyncEnabled() {
129 Profile* profile = Profile::FromWebUI(web_ui()); 196 Profile* profile = Profile::FromWebUI(web_ui());
130 ProfileSyncService* service = 197 ProfileSyncService* service =
131 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); 198 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
132 return service && service->GetPreferredDataTypes().Has(syncer::SESSIONS); 199 return service && service->GetPreferredDataTypes().Has(syncer::SESSIONS);
133 } 200 }
134 201
135 string16 ForeignSessionHandler::FormatSessionTime(const base::Time& time) { 202 string16 ForeignSessionHandler::FormatSessionTime(const base::Time& time) {
136 // Return a time like "1 hour ago", "2 days ago", etc. 203 // Return a time like "1 hour ago", "2 days ago", etc.
137 return TimeFormat::TimeElapsed(base::Time::Now() - time); 204 return TimeFormat::TimeElapsed(base::Time::Now() - time);
138 } 205 }
139 206
140 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { 207 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) {
141 SessionModelAssociator* associator = GetModelAssociator(); 208 SessionModelAssociator* associator = GetModelAssociator(web_ui());
142 std::vector<const SyncedSession*> sessions; 209 std::vector<const SyncedSession*> sessions;
143 210
144 ListValue session_list; 211 ListValue session_list;
145 if (associator && associator->GetAllForeignSessions(&sessions)) { 212 if (associator && associator->GetAllForeignSessions(&sessions)) {
146 // Sort sessions from most recent to least recent. 213 // Sort sessions from most recent to least recent.
147 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); 214 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
148 215
149 // Use a pref to keep track of sessions that were collapsed by the user. 216 // Use a pref to keep track of sessions that were collapsed by the user.
150 // To prevent the pref from accumulating stale sessions, clear it each time 217 // To prevent the pref from accumulating stale sessions, clear it each time
151 // and only add back sessions that are still current. 218 // and only add back sessions that are still current.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 289
223 // Extract tab id. 290 // Extract tab id.
224 std::string tab_id_str; 291 std::string tab_id_str;
225 SessionID::id_type tab_id = kInvalidId; 292 SessionID::id_type tab_id = kInvalidId;
226 if (num_args >= 3 && (!args->GetString(2, &tab_id_str) || 293 if (num_args >= 3 && (!args->GetString(2, &tab_id_str) ||
227 !base::StringToInt(tab_id_str, &tab_id))) { 294 !base::StringToInt(tab_id_str, &tab_id))) {
228 LOG(ERROR) << "Failed to extract tab SessionID."; 295 LOG(ERROR) << "Failed to extract tab SessionID.";
229 return; 296 return;
230 } 297 }
231 298
232 SessionModelAssociator* associator = GetModelAssociator(); 299 WindowOpenDisposition disposition =
233 if (!associator) 300 web_ui_util::GetDispositionFromClick(args, 3);
234 return;
235 301
236 if (tab_id != kInvalidId) { 302 OpenForeignSession(
237 // We don't actually care about |window_num|, this is just a sanity check. 303 web_ui(), session_string_value, window_num, tab_id, disposition);
238 DCHECK_LT(kInvalidId, window_num);
239 const SessionTab* tab;
240 if (!associator->GetForeignTab(session_string_value, tab_id, &tab)) {
241 LOG(ERROR) << "Failed to load foreign tab.";
242 return;
243 }
244 WindowOpenDisposition disposition =
245 web_ui_util::GetDispositionFromClick(args, 3);
246 SessionRestore::RestoreForeignSessionTab(
247 web_ui()->GetWebContents(), *tab, disposition);
248 } else {
249 std::vector<const SessionWindow*> windows;
250 // Note: we don't own the ForeignSessions themselves.
251 if (!associator->GetForeignSession(session_string_value, &windows)) {
252 LOG(ERROR) << "ForeignSessionHandler failed to get session data from"
253 "SessionModelAssociator.";
254 return;
255 }
256 std::vector<const SessionWindow*>::const_iterator iter_begin =
257 windows.begin() + ((window_num == kInvalidId) ? 0 : window_num);
258 std::vector<const SessionWindow*>::const_iterator iter_end =
259 ((window_num == kInvalidId) ?
260 std::vector<const SessionWindow*>::const_iterator(windows.end()) :
261 iter_begin + 1);
262 SessionRestore::RestoreForeignSessionWindows(
263 Profile::FromWebUI(web_ui()), iter_begin, iter_end);
264 }
265 } 304 }
266 305
267 void ForeignSessionHandler::HandleSetForeignSessionCollapsed( 306 void ForeignSessionHandler::HandleSetForeignSessionCollapsed(
268 const ListValue* args) { 307 const ListValue* args) {
269 if (args->GetSize() != 2U) { 308 if (args->GetSize() != 2U) {
270 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed"; 309 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed";
271 return; 310 return;
272 } 311 }
273 312
274 // Get the session tag argument (required). 313 // Get the session tag argument (required).
(...skipping 20 matching lines...) Expand all
295 } 334 }
296 335
297 void ForeignSessionHandler::HandleShowOtherDeviceSessionPopup( 336 void ForeignSessionHandler::HandleShowOtherDeviceSessionPopup(
298 const ListValue* args) { 337 const ListValue* args) {
299 CHECK(args->GetSize() == 3U); 338 CHECK(args->GetSize() == 3U);
300 339
301 // Extract the session tag. 340 // Extract the session tag.
302 std::string session_string_value; 341 std::string session_string_value;
303 CHECK(args->GetString(0, &session_string_value)); 342 CHECK(args->GetString(0, &session_string_value));
304 343
305 // Extract horizontal coordinate of the click within the application's client 344 // Extract horizontal coordinate of the click within the application's window
306 // area. 345 // area.
307 double client_x; 346 double window_x;
vadimt 2012/10/08 19:39:12 Hi Jeremy! Thanks for the correction! However, pe
jeremycho 2012/10/09 01:52:13 Sorry, my bad. I had screen in the JS too... On
308 CHECK(args->GetDouble(1, &client_x)); 347 CHECK(args->GetDouble(1, &window_x));
309 348
310 // Extract vertical coordinate of the click within the application's client 349 // Extract vertical coordinate of the click within the application's window
311 // area. 350 // area.
312 double client_y; 351 double window_y;
313 CHECK(args->GetDouble(2, &client_y)); 352 CHECK(args->GetDouble(2, &window_y));
314 353
315 // TODO(vadimt): implement this method. 354 OtherDeviceMenu* menu = new OtherDeviceMenu(
316 } 355 web_ui(),
317 356 session_string_value, gfx::Point(window_x, window_y));
318 bool ForeignSessionHandler::SessionTabToValue( 357 menu->ShowMenu();
319 const SessionTab& tab,
320 DictionaryValue* dictionary) {
321 if (tab.navigations.empty())
322 return false;
323 int selected_index = tab.current_navigation_index;
324 selected_index = std::max(
325 0,
326 std::min(selected_index,
327 static_cast<int>(tab.navigations.size() - 1)));
328 const TabNavigation& current_navigation =
329 tab.navigations.at(selected_index);
330 GURL tab_url = current_navigation.virtual_url();
331 if (tab_url == GURL(chrome::kChromeUINewTabURL))
332 return false;
333 NewTabUI::SetUrlTitleAndDirection(dictionary, current_navigation.title(),
334 tab_url);
335 dictionary->SetString("type", "tab");
336 dictionary->SetDouble("timestamp",
337 static_cast<double>(tab.timestamp.ToInternalValue()));
338 dictionary->SetInteger("sessionId", tab.tab_id.id());
339 return true;
340 } 358 }
341 359
342 bool ForeignSessionHandler::SessionWindowToValue( 360 bool ForeignSessionHandler::SessionWindowToValue(
343 const SessionWindow& window, 361 const SessionWindow& window,
344 DictionaryValue* dictionary) { 362 DictionaryValue* dictionary) {
345 if (window.tabs.empty()) { 363 if (window.tabs.empty()) {
346 NOTREACHED(); 364 NOTREACHED();
347 return false; 365 return false;
348 } 366 }
349 scoped_ptr<ListValue> tab_values(new ListValue()); 367 scoped_ptr<ListValue> tab_values(new ListValue());
(...skipping 17 matching lines...) Expand all
367 dictionary->SetString("userVisibleTimestamp", 385 dictionary->SetString("userVisibleTimestamp",
368 last_synced < base::TimeDelta::FromMinutes(1) ? 386 last_synced < base::TimeDelta::FromMinutes(1) ?
369 l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) : 387 l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) :
370 TimeFormat::TimeElapsed(last_synced)); 388 TimeFormat::TimeElapsed(last_synced));
371 dictionary->SetInteger("sessionId", window.window_id.id()); 389 dictionary->SetInteger("sessionId", window.window_id.id());
372 dictionary->Set("tabs", tab_values.release()); 390 dictionary->Set("tabs", tab_values.release());
373 return true; 391 return true;
374 } 392 }
375 393
376 } // namespace browser_sync 394 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/foreign_session_handler.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698