1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/tabs/pinned_tab_codec.h"
#include "base/values.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/page_transition_types.h"
#include "chrome/common/pref_names.h"
#include "ipc/ipc_message.h"
typedef BrowserInit::LaunchWithProfile::Tab Tab;
// Key used in dictionaries for the app id.
static const char kAppID[] = "app_id";
// Key used in dictionaries for the url.
static const char kURL[] = "url";
// Returns true if |browser| has any pinned tabs.
static bool HasPinnedTabs(Browser* browser) {
TabStripModel* tab_model = browser->tabstrip_model();
for (int i = 0; i < tab_model->count(); ++i) {
if (tab_model->IsTabPinned(i))
return true;
}
return false;
}
// Adds a DictionaryValue to |values| representing the pinned tab at the
// specified index.
static void EncodePinnedTab(TabStripModel* model,
int index,
ListValue* values) {
scoped_ptr<DictionaryValue> value(new DictionaryValue());
TabContents* tab_contents = model->GetTabContentsAt(index);
if (model->IsAppTab(index)) {
const Extension* extension = tab_contents->extension_app();
DCHECK(extension);
value->SetString(kAppID, extension->id());
// For apps we use the launch url. We do this for the following reason:
// . the user is effectively restarting the app, so that returning them to
// the app's launch page seems closest to what they expect.
value->SetString(kURL, extension->GetFullLaunchURL().spec());
values->Append(value.release());
} else {
NavigationEntry* entry = tab_contents->controller().GetActiveEntry();
if (!entry && tab_contents->controller().entry_count())
entry = tab_contents->controller().GetEntryAtIndex(0);
if (entry) {
value->SetString(kURL, entry->url().spec());
values->Append(value.release());
}
}
}
// Invokes EncodePinnedTab for each pinned tab in browser.
static void EncodePinnedTabs(Browser* browser, ListValue* values) {
TabStripModel* tab_model = browser->tabstrip_model();
for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i)
EncodePinnedTab(tab_model, i, values);
}
// Decodes the previously written values in |value| to |tab|, returning true
// on success.
static bool DecodeTab(const DictionaryValue& value, Tab* tab) {
tab->is_app = false;
std::string url_string;
if (!value.GetString(kURL, &url_string))
return false;
tab->url = GURL(url_string);
if (value.GetString(kAppID, &(tab->app_id)))
tab->is_app = true;
return true;
}
// static
void PinnedTabCodec::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterListPref(prefs::kPinnedTabs);
}
// static
void PinnedTabCodec::WritePinnedTabs(Profile* profile) {
PrefService* prefs = profile->GetPrefs();
if (!prefs)
return;
ListValue values;
for (BrowserList::const_iterator i = BrowserList::begin();
i != BrowserList::end(); ++i) {
Browser* browser = *i;
if (browser->type() == Browser::TYPE_NORMAL &&
browser->profile() == profile && HasPinnedTabs(browser)) {
EncodePinnedTabs(browser, &values);
}
}
prefs->Set(prefs::kPinnedTabs, values);
prefs->ScheduleSavePersistentPrefs();
}
// static
std::vector<Tab> PinnedTabCodec::ReadPinnedTabs(Profile* profile) {
std::vector<Tab> results;
PrefService* prefs = profile->GetPrefs();
if (!prefs)
return results;
const ListValue* pref_value = prefs->GetList(prefs::kPinnedTabs);
if (!pref_value)
return results;
for (size_t i = 0, max = pref_value->GetSize(); i < max; ++i) {
DictionaryValue* values = NULL;
if (pref_value->GetDictionary(i, &values)) {
Tab tab;
if (DecodeTab(*values, &tab))
results.push_back(tab);
}
}
return results;
}
|