1dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// Use of this source code is governed by a BSD-style license that can be
3bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// found in the LICENSE file.
4bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
5dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/ui/webui/value_helper.h"
6bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
7dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/ui/webui/new_tab_ui.h"
8bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/common/url_constants.h"
9bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
10bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenbool ValueHelper::TabToValue(
11bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    const TabRestoreService::Tab& tab,
12bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    DictionaryValue* dictionary) {
13bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  if (tab.navigations.empty())
14bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    return false;
15bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
16bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  const TabNavigation& current_navigation =
17bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen      tab.navigations.at(tab.current_navigation_index);
18bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL))
19bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    return false;
20bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  NewTabUI::SetURLTitleAndDirection(dictionary, current_navigation.title(),
21bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen                                    current_navigation.virtual_url());
22bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  dictionary->SetString("type", "tab");
2372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  dictionary->SetDouble("timestamp", tab.timestamp.ToDoubleT());
24bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  return true;
25bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen}
26bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
27bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenbool ValueHelper::WindowToValue(
28bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    const TabRestoreService::Window& window,
29bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    DictionaryValue* dictionary) {
30bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  if (window.tabs.empty()) {
31bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    NOTREACHED();
32bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    return false;
33bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  }
34bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  scoped_ptr<ListValue> tab_values(new ListValue());
35bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  for (size_t i = 0; i < window.tabs.size(); ++i) {
36bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    scoped_ptr<DictionaryValue> tab_value(new DictionaryValue());
37bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    if (TabToValue(window.tabs[i], tab_value.get()))
38bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen      tab_values->Append(tab_value.release());
39bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  }
40bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  if (tab_values->GetSize() == 0)
41bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen    return false;
42bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  dictionary->SetString("type", "window");
4372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  dictionary->SetDouble("timestamp", window.timestamp.ToDoubleT());
44bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  dictionary->Set("tabs", tab_values.release());
45bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen  return true;
46bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen}
47bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen
48