pinned_tab_codec.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/tabs/pinned_tab_codec.h"
6
7#include "base/values.h"
8#include "chrome/browser/extensions/tab_helper.h"
9#include "chrome/browser/prefs/pref_service.h"
10#include "chrome/browser/prefs/scoped_user_pref_update.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_list.h"
14#include "chrome/browser/ui/tab_contents/tab_contents.h"
15#include "chrome/browser/ui/tabs/tab_strip_model.h"
16#include "chrome/common/extensions/extension.h"
17#include "chrome/common/pref_names.h"
18#include "content/public/browser/navigation_entry.h"
19#include "content/public/browser/web_contents.h"
20
21using content::NavigationEntry;
22
23// Key used in dictionaries for the app id.
24static const char kAppID[] = "app_id";
25
26// Key used in dictionaries for the url.
27static const char kURL[] = "url";
28
29// Returns true if |browser| has any pinned tabs.
30static bool HasPinnedTabs(Browser* browser) {
31  TabStripModel* tab_model = browser->tab_strip_model();
32  for (int i = 0; i < tab_model->count(); ++i) {
33    if (tab_model->IsTabPinned(i))
34      return true;
35  }
36  return false;
37}
38
39// Adds a DictionaryValue to |values| representing |tab|.
40static void EncodeTab(const StartupTab& tab, ListValue* values) {
41  scoped_ptr<DictionaryValue> value(new DictionaryValue);
42  value->SetString(kURL, tab.url.spec());
43  if (tab.is_app)
44    value->SetString(kAppID, tab.app_id);
45  values->Append(value.release());
46}
47
48// Adds a DictionaryValue to |values| representing the pinned tab at the
49// specified index.
50static void EncodePinnedTab(TabStripModel* model,
51                            int index,
52                            ListValue* values) {
53  scoped_ptr<DictionaryValue> value(new DictionaryValue());
54
55  content::WebContents* web_contents =
56      model->GetTabContentsAt(index)->web_contents();
57  if (model->IsAppTab(index)) {
58    const extensions::Extension* extension =
59        extensions::TabHelper::FromWebContents(web_contents)->extension_app();
60    DCHECK(extension);
61    value->SetString(kAppID, extension->id());
62    // For apps we use the launch url. We do this because the user is
63    // effectively restarting the app, so returning them to the app's launch
64    // page seems closest to what they expect.
65    value->SetString(kURL, extension->GetFullLaunchURL().spec());
66    values->Append(value.release());
67  } else {
68    NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
69    if (!entry && web_contents->GetController().GetEntryCount())
70      entry = web_contents->GetController().GetEntryAtIndex(0);
71    if (entry) {
72      value->SetString(kURL, entry->GetURL().spec());
73      values->Append(value.release());
74    }
75  }
76}
77
78// Invokes EncodePinnedTab for each pinned tab in browser.
79static void EncodePinnedTabs(Browser* browser, ListValue* values) {
80  TabStripModel* tab_model = browser->tab_strip_model();
81  for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i)
82    EncodePinnedTab(tab_model, i, values);
83}
84
85// Decodes the previously written values in |value| to |tab|, returning true
86// on success.
87static bool DecodeTab(const DictionaryValue& value, StartupTab* tab) {
88  tab->is_app = false;
89
90  std::string url_string;
91  if (!value.GetString(kURL, &url_string))
92    return false;
93  tab->url = GURL(url_string);
94
95  if (value.GetString(kAppID, &(tab->app_id)))
96    tab->is_app = true;
97
98  return true;
99}
100
101// static
102void PinnedTabCodec::RegisterUserPrefs(PrefService* prefs) {
103  prefs->RegisterListPref(prefs::kPinnedTabs, PrefService::UNSYNCABLE_PREF);
104}
105
106// static
107void PinnedTabCodec::WritePinnedTabs(Profile* profile) {
108  PrefService* prefs = profile->GetPrefs();
109  if (!prefs)
110    return;
111
112  ListValue values;
113  for (BrowserList::const_iterator i = BrowserList::begin();
114       i != BrowserList::end(); ++i) {
115    Browser* browser = *i;
116    if (browser->is_type_tabbed() &&
117        browser->profile() == profile && HasPinnedTabs(browser)) {
118      EncodePinnedTabs(browser, &values);
119    }
120  }
121  prefs->Set(prefs::kPinnedTabs, values);
122}
123
124// static
125void PinnedTabCodec::WritePinnedTabs(Profile* profile,
126                                     const StartupTabs& tabs) {
127  PrefService* prefs = profile->GetPrefs();
128  if (!prefs)
129    return;
130
131  ListPrefUpdate update(prefs, prefs::kPinnedTabs);
132  ListValue* values = update.Get();
133  values->Clear();
134  for (StartupTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i)
135    EncodeTab(*i, values);
136}
137
138// static
139StartupTabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) {
140  PrefService* prefs = profile->GetPrefs();
141  if (!prefs)
142    return StartupTabs();
143  return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs));
144}
145
146// static
147StartupTabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) {
148  StartupTabs results;
149
150  const base::ListValue* tabs_list = NULL;
151  if (!value->GetAsList(&tabs_list))
152    return results;
153
154  for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) {
155    const base::DictionaryValue* tab_values = NULL;
156    if (tabs_list->GetDictionary(i, &tab_values)) {
157      StartupTab tab;
158      if (DecodeTab(*tab_values, &tab))
159        results.push_back(tab);
160    }
161  }
162  return results;
163}
164