15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/ntp/foreign_session_handler.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/time_formatting.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_vector.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_restore.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service_factory.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/url_data_source.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_ui.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
37bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/base/l10n/time_format.h"
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/base/webui/web_ui_util.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace browser_sync {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Maximum number of sessions we're going to display on the NTP
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const size_t kMaxSessionsToShow = 10;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Comparator function for use with std::sort that will sort sessions by
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// descending modified_time (i.e., most recent first).
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SortSessionsByRecency(const SyncedSession* s1, const SyncedSession* s2) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return s1->modified_time > s2->modified_time;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namepace
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ForeignSessionHandler::ForeignSessionHandler() {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid ForeignSessionHandler::RegisterProfilePrefs(
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterDictionaryPref(
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kNtpCollapsedForeignSessions,
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::OpenForeignSessionTab(
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WebUI* web_ui,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& session_string_value,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SessionID::id_type window_num,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SessionID::id_type tab_id,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const WindowOpenDisposition& disposition) {
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui);
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!open_tabs)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't actually care about |window_num|, this is just a sanity check.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_LT(kInvalidId, window_num);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SessionTab* tab;
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!open_tabs->GetForeignTab(session_string_value, tab_id, &tab)) {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to load foreign tab.";
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab->navigations.empty()) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Foreign tab no longer has valid navigations.";
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionRestore::RestoreForeignSessionTab(
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      web_ui->GetWebContents(), *tab, disposition);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::OpenForeignSessionWindows(
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WebUI* web_ui,
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& session_string_value,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SessionID::id_type window_num) {
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui);
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!open_tabs)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<const SessionWindow*> windows;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: we don't own the ForeignSessions themselves.
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!open_tabs->GetForeignSession(session_string_value, &windows)) {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "ForeignSessionHandler failed to get session data from"
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "OpenTabsUIDelegate.";
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<const SessionWindow*>::const_iterator iter_begin =
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      windows.begin() + (window_num == kInvalidId ? 0 : window_num);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<const SessionWindow*>::const_iterator iter_end =
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      window_num == kInvalidId ?
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<const SessionWindow*>::const_iterator(windows.end()) :
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      iter_begin + 1;
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::HostDesktopType host_desktop_type =
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::GetHostDesktopTypeForNativeView(
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          web_ui->GetWebContents()->GetNativeView());
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionRestore::RestoreForeignSessionWindows(
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Profile::FromWebUI(web_ui), host_desktop_type, iter_begin, iter_end);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ForeignSessionHandler::SessionTabToValue(
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SessionTab& tab,
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* dictionary) {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab.navigations.empty())
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int selected_index = std::min(tab.current_navigation_index,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                static_cast<int>(tab.navigations.size() - 1));
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const ::sessions::SerializedNavigationEntry& current_navigation =
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tab.navigations.at(selected_index);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL tab_url = current_navigation.virtual_url();
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab_url == GURL(chrome::kChromeUINewTabURL))
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NewTabUI::SetUrlTitleAndDirection(dictionary, current_navigation.title(),
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    tab_url);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetString("type", "tab");
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetDouble("timestamp",
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        static_cast<double>(tab.timestamp.ToInternalValue()));
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(jeremycho): This should probably be renamed to tabId to avoid
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // confusion with the ID corresponding to a session.  Investigate all the
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // places (C++ and JS) where this is being used.  (http://crbug.com/154865).
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetInteger("sessionId", tab.tab_id.id());
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)OpenTabsUIDelegate* ForeignSessionHandler::GetOpenTabsUIDelegate(
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WebUI* web_ui) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = Profile::FromWebUI(web_ui);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProfileSyncService* service =
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Only return the delegate if it exists and it is done syncing sessions.
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (service && service->ShouldPushChanges())
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return service->GetOpenTabsUIDelegate();
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::RegisterMessages() {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Init();
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("deleteForeignSession",
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("getForeignSessions",
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&ForeignSessionHandler::HandleGetForeignSessions,
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("openForeignSession",
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&ForeignSessionHandler::HandleOpenForeignSession,
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("setForeignSessionCollapsed",
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&ForeignSessionHandler::HandleSetForeignSessionCollapsed,
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::Init() {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = Profile::FromWebUI(web_ui());
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProfileSyncService* service =
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::Source<ProfileSyncService>(service));
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::Source<Profile>(profile));
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::Source<Profile>(profile));
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::Observe(
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int type,
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationSource& source,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationDetails& details) {
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue list_value;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (type) {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED:
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Tab sync is disabled, so clean up data about collapsed sessions.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Profile::FromWebUI(web_ui())->GetPrefs()->ClearPref(
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          prefs::kNtpCollapsedForeignSessions);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Fall through.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE:
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED:
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HandleGetForeignSessions(&list_value);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ForeignSessionHandler::IsTabSyncEnabled() {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = Profile::FromWebUI(web_ui());
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProfileSyncService* service =
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 ForeignSessionHandler::FormatSessionTime(
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Time& time) {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a time like "1 hour ago", "2 days ago", etc.
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Time now = base::Time::Now();
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TimeFormat does not support negative TimeDelta values, so then we use 0.
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return ui::TimeFormat::Simple(
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ui::TimeFormat::FORMAT_ELAPSED, ui::TimeFormat::LENGTH_SHORT,
226bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch      now < time ? base::TimeDelta() : now - time);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ForeignSessionHandler::HandleGetForeignSessions(
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::ListValue* args) {
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui());
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<const SyncedSession*> sessions;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue session_list;
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) {
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Sort sessions from most recent to least recent.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Use a pref to keep track of sessions that were collapsed by the user.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // To prevent the pref from accumulating stale sessions, clear it each time
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // and only add back sessions that are still current.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DictionaryPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(),
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     prefs::kNtpCollapsedForeignSessions);
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* current_collapsed_sessions = pref_update.Get();
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> collapsed_sessions(
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        current_collapsed_sessions->DeepCopy());
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    current_collapsed_sessions->Clear();
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Note: we don't own the SyncedSessions themselves.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SyncedSession* session = sessions[i];
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& session_tag = session->session_tag;
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<base::DictionaryValue> session_data(
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          new base::DictionaryValue());
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->SetString("tag", session_tag);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->SetString("name", session->session_name);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->SetString("deviceType", session->DeviceTypeAsString());
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->SetString("modifiedTime",
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              FormatSessionTime(session->modified_time));
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool is_collapsed = collapsed_sessions->HasKey(session_tag);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->SetBoolean("collapsed", is_collapsed);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (is_collapsed)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        current_collapsed_sessions->SetBoolean(session_tag, true);
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<base::ListValue> window_list(new base::ListValue());
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (SyncedSession::SyncedWindowMap::const_iterator it =
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           session->windows.begin(); it != session->windows.end(); ++it) {
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SessionWindow* window = it->second;
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        scoped_ptr<base::DictionaryValue> window_data(
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new base::DictionaryValue());
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (SessionWindowToValue(*window, window_data.get()))
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          window_list->Append(window_data.release());
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_data->Set("windows", window_list.release());
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_list.Append(session_data.release());
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled());
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction("ntp.setForeignSessions",
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   session_list,
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   tab_sync_enabled);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ForeignSessionHandler::HandleOpenForeignSession(
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::ListValue* args) {
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t num_args = args->GetSize();
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Expect either 1 or 8 args. For restoring an entire session, only
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one argument is required -- the session tag. To restore a tab,
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the additional args required are the window id, the tab id,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and 4 properties of the event object (button, altKey, ctrlKey,
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // metaKey, shiftKey) for determining how to open the tab.
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_args != 8U && num_args != 1U) {
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "openForeignSession called with " << args->GetSize()
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << " arguments.";
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extract the session tag (always provided).
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string session_string_value;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetString(0, &session_string_value)) {
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to extract session tag.";
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extract window number.
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string window_num_str;
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int window_num = kInvalidId;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_args >= 2 && (!args->GetString(1, &window_num_str) ||
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !base::StringToInt(window_num_str, &window_num))) {
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to extract window number.";
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extract tab id.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string tab_id_str;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionID::id_type tab_id = kInvalidId;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_args >= 3 && (!args->GetString(2, &tab_id_str) ||
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !base::StringToInt(tab_id_str, &tab_id))) {
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to extract tab SessionID.";
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab_id != kInvalidId) {
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowOpenDisposition disposition = webui::GetDispositionFromClick(args, 3);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OpenForeignSessionTab(
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        web_ui(), session_string_value, window_num, tab_id, disposition);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OpenForeignSessionWindows(web_ui(), session_string_value, window_num);
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ForeignSessionHandler::HandleDeleteForeignSession(
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::ListValue* args) {
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (args->GetSize() != 1U) {
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Wrong number of args to deleteForeignSession";
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the session tag argument (required).
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string session_tag;
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetString(0, &session_tag)) {
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Unable to extract session tag";
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
348f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui());
349f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (open_tabs)
350f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    open_tabs->DeleteForeignSession(session_tag);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ForeignSessionHandler::HandleSetForeignSessionCollapsed(
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::ListValue* args) {
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (args->GetSize() != 2U) {
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed";
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the session tag argument (required).
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string session_tag;
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetString(0, &session_tag)) {
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Unable to extract session tag";
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_collapsed;
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetBoolean(1, &is_collapsed)) {
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Unable to extract boolean argument";
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Store session tags for collapsed sessions in a preference so that the
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // collapsed state persists.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions);
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (is_collapsed)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    update.Get()->SetBoolean(session_tag, true);
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    update.Get()->Remove(session_tag, NULL);
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ForeignSessionHandler::SessionWindowToValue(
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SessionWindow& window,
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* dictionary) {
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (window.tabs.empty()) {
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> tab_values(new base::ListValue());
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the last |modification_time| for all entries within a window.
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time modification_time = window.timestamp;
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < window.tabs.size(); ++i) {
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> tab_value(new base::DictionaryValue());
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (SessionTabToValue(*window.tabs[i], tab_value.get())) {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      modification_time = std::max(modification_time,
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   window.tabs[i]->timestamp);
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tab_values->Append(tab_value.release());
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab_values->GetSize() == 0)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetString("type", "window");
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetDouble("timestamp", modification_time.ToInternalValue());
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const base::TimeDelta last_synced = base::Time::Now() - modification_time;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If clock skew leads to a future time, or we last synced less than a minute
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ago, output "Just now".
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetString("userVisibleTimestamp",
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      last_synced < base::TimeDelta::FromMinutes(1) ?
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) :
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED,
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 ui::TimeFormat::LENGTH_SHORT, last_synced));
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->SetInteger("sessionId", window.window_id.id());
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dictionary->Set("tabs", tab_values.release());
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace browser_sync
419