BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / tabs / pinned_tab_codec.cc
blob4141698384de4df161d2c034fb5b635a74f5d305
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.
5 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_iterator.h"
14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/common/extension.h"
23 using content::NavigationEntry;
25 // Key used in dictionaries for the url.
26 static const char kURL[] = "url";
28 // Returns true if |browser| has any pinned tabs.
29 static bool HasPinnedTabs(Browser* browser) {
30 TabStripModel* tab_model = browser->tab_strip_model();
31 for (int i = 0; i < tab_model->count(); ++i) {
32 if (tab_model->IsTabPinned(i))
33 return true;
35 return false;
38 // Adds a DictionaryValue to |values| representing |tab|.
39 static void EncodeTab(const StartupTab& tab, base::ListValue* values) {
40 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
41 value->SetString(kURL, tab.url.spec());
42 values->Append(value.release());
45 // Adds a base::DictionaryValue to |values| representing the pinned tab at the
46 // specified index.
47 static void EncodePinnedTab(TabStripModel* model,
48 int index,
49 base::ListValue* values) {
50 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
52 content::WebContents* web_contents = model->GetWebContentsAt(index);
53 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
54 if (!entry && web_contents->GetController().GetEntryCount())
55 entry = web_contents->GetController().GetEntryAtIndex(0);
56 if (entry) {
57 value->SetString(kURL, entry->GetURL().spec());
58 values->Append(value.release());
62 // Invokes EncodePinnedTab for each pinned tab in browser.
63 static void EncodePinnedTabs(Browser* browser, base::ListValue* values) {
64 TabStripModel* tab_model = browser->tab_strip_model();
65 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i)
66 EncodePinnedTab(tab_model, i, values);
69 // Decodes the previously written values in |value| to |tab|, returning true
70 // on success.
71 static bool DecodeTab(const base::DictionaryValue& value, StartupTab* tab) {
72 std::string url_string;
73 if (!value.GetString(kURL, &url_string))
74 return false;
75 tab->url = GURL(url_string);
77 return true;
80 // static
81 void PinnedTabCodec::RegisterProfilePrefs(
82 user_prefs::PrefRegistrySyncable* registry) {
83 registry->RegisterListPref(prefs::kPinnedTabs);
86 // static
87 void PinnedTabCodec::WritePinnedTabs(Profile* profile) {
88 PrefService* prefs = profile->GetPrefs();
89 if (!prefs)
90 return;
92 base::ListValue values;
93 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
94 Browser* browser = *it;
95 if (browser->is_type_tabbed() &&
96 browser->profile() == profile && HasPinnedTabs(browser)) {
97 EncodePinnedTabs(browser, &values);
100 prefs->Set(prefs::kPinnedTabs, values);
103 // static
104 void PinnedTabCodec::WritePinnedTabs(Profile* profile,
105 const StartupTabs& tabs) {
106 PrefService* prefs = profile->GetPrefs();
107 if (!prefs)
108 return;
110 ListPrefUpdate update(prefs, prefs::kPinnedTabs);
111 base::ListValue* values = update.Get();
112 values->Clear();
113 for (StartupTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i)
114 EncodeTab(*i, values);
117 // static
118 StartupTabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) {
119 PrefService* prefs = profile->GetPrefs();
120 if (!prefs)
121 return StartupTabs();
122 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs));
125 // static
126 StartupTabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) {
127 StartupTabs results;
129 const base::ListValue* tabs_list = NULL;
130 if (!value->GetAsList(&tabs_list))
131 return results;
133 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) {
134 const base::DictionaryValue* tab_values = NULL;
135 if (tabs_list->GetDictionary(i, &tab_values)) {
136 StartupTab tab;
137 if (DecodeTab(*tab_values, &tab))
138 results.push_back(tab);
141 return results;