OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/tabs/pinned_tab_codec.h" | 5 #include "chrome/browser/tabs/pinned_tab_codec.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/extension_tab_helper.h" | 8 #include "chrome/browser/extensions/extension_tab_helper.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/tabs/tab_strip_model.h" | 12 #include "chrome/browser/tabs/tab_strip_model.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 | 20 |
21 using content::NavigationEntry; | 21 using content::NavigationEntry; |
22 | 22 |
23 typedef StartupBrowserCreator::LaunchWithProfile::Tab Tab; | |
24 | |
25 // Key used in dictionaries for the app id. | 23 // Key used in dictionaries for the app id. |
26 static const char kAppID[] = "app_id"; | 24 static const char kAppID[] = "app_id"; |
27 | 25 |
28 // Key used in dictionaries for the url. | 26 // Key used in dictionaries for the url. |
29 static const char kURL[] = "url"; | 27 static const char kURL[] = "url"; |
30 | 28 |
31 // Returns true if |browser| has any pinned tabs. | 29 // Returns true if |browser| has any pinned tabs. |
32 static bool HasPinnedTabs(Browser* browser) { | 30 static bool HasPinnedTabs(Browser* browser) { |
33 TabStripModel* tab_model = browser->tabstrip_model(); | 31 TabStripModel* tab_model = browser->tabstrip_model(); |
34 for (int i = 0; i < tab_model->count(); ++i) { | 32 for (int i = 0; i < tab_model->count(); ++i) { |
35 if (tab_model->IsTabPinned(i)) | 33 if (tab_model->IsTabPinned(i)) |
36 return true; | 34 return true; |
37 } | 35 } |
38 return false; | 36 return false; |
39 } | 37 } |
40 | 38 |
41 // Adds a DictionaryValue to |values| representing |tab|. | 39 // Adds a DictionaryValue to |values| representing |tab|. |
42 static void EncodeTab(const Tab& tab, ListValue* values) { | 40 static void EncodeTab(const StartupTab& tab, ListValue* values) { |
43 scoped_ptr<DictionaryValue> value(new DictionaryValue); | 41 scoped_ptr<DictionaryValue> value(new DictionaryValue); |
44 value->SetString(kURL, tab.url.spec()); | 42 value->SetString(kURL, tab.url.spec()); |
45 if (tab.is_app) | 43 if (tab.is_app) |
46 value->SetString(kAppID, tab.app_id); | 44 value->SetString(kAppID, tab.app_id); |
47 values->Append(value.release()); | 45 values->Append(value.release()); |
48 } | 46 } |
49 | 47 |
50 // Adds a DictionaryValue to |values| representing the pinned tab at the | 48 // Adds a DictionaryValue to |values| representing the pinned tab at the |
51 // specified index. | 49 // specified index. |
52 static void EncodePinnedTab(TabStripModel* model, | 50 static void EncodePinnedTab(TabStripModel* model, |
(...skipping 26 matching lines...) Expand all Loading... |
79 | 77 |
80 // Invokes EncodePinnedTab for each pinned tab in browser. | 78 // Invokes EncodePinnedTab for each pinned tab in browser. |
81 static void EncodePinnedTabs(Browser* browser, ListValue* values) { | 79 static void EncodePinnedTabs(Browser* browser, ListValue* values) { |
82 TabStripModel* tab_model = browser->tabstrip_model(); | 80 TabStripModel* tab_model = browser->tabstrip_model(); |
83 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i) | 81 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i) |
84 EncodePinnedTab(tab_model, i, values); | 82 EncodePinnedTab(tab_model, i, values); |
85 } | 83 } |
86 | 84 |
87 // Decodes the previously written values in |value| to |tab|, returning true | 85 // Decodes the previously written values in |value| to |tab|, returning true |
88 // on success. | 86 // on success. |
89 static bool DecodeTab(const DictionaryValue& value, Tab* tab) { | 87 static bool DecodeTab(const DictionaryValue& value, StartupTab* tab) { |
90 tab->is_app = false; | 88 tab->is_app = false; |
91 | 89 |
92 std::string url_string; | 90 std::string url_string; |
93 if (!value.GetString(kURL, &url_string)) | 91 if (!value.GetString(kURL, &url_string)) |
94 return false; | 92 return false; |
95 tab->url = GURL(url_string); | 93 tab->url = GURL(url_string); |
96 | 94 |
97 if (value.GetString(kAppID, &(tab->app_id))) | 95 if (value.GetString(kAppID, &(tab->app_id))) |
98 tab->is_app = true; | 96 tab->is_app = true; |
99 | 97 |
(...skipping 17 matching lines...) Expand all Loading... |
117 Browser* browser = *i; | 115 Browser* browser = *i; |
118 if (browser->is_type_tabbed() && | 116 if (browser->is_type_tabbed() && |
119 browser->profile() == profile && HasPinnedTabs(browser)) { | 117 browser->profile() == profile && HasPinnedTabs(browser)) { |
120 EncodePinnedTabs(browser, &values); | 118 EncodePinnedTabs(browser, &values); |
121 } | 119 } |
122 } | 120 } |
123 prefs->Set(prefs::kPinnedTabs, values); | 121 prefs->Set(prefs::kPinnedTabs, values); |
124 } | 122 } |
125 | 123 |
126 // static | 124 // static |
127 void PinnedTabCodec::WritePinnedTabs(Profile* profile, const Tabs& tabs) { | 125 void PinnedTabCodec::WritePinnedTabs(Profile* profile, |
| 126 const StartupTabs& tabs) { |
128 PrefService* prefs = profile->GetPrefs(); | 127 PrefService* prefs = profile->GetPrefs(); |
129 if (!prefs) | 128 if (!prefs) |
130 return; | 129 return; |
131 | 130 |
132 ListPrefUpdate update(prefs, prefs::kPinnedTabs); | 131 ListPrefUpdate update(prefs, prefs::kPinnedTabs); |
133 ListValue* values = update.Get(); | 132 ListValue* values = update.Get(); |
134 values->Clear(); | 133 values->Clear(); |
135 for (Tabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) | 134 for (StartupTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) |
136 EncodeTab(*i, values); | 135 EncodeTab(*i, values); |
137 } | 136 } |
138 | 137 |
139 // static | 138 // static |
140 PinnedTabCodec::Tabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) { | 139 StartupTabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) { |
141 PrefService* prefs = profile->GetPrefs(); | 140 PrefService* prefs = profile->GetPrefs(); |
142 if (!prefs) | 141 if (!prefs) |
143 return Tabs(); | 142 return StartupTabs(); |
144 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs)); | 143 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs)); |
145 } | 144 } |
146 | 145 |
147 // static | 146 // static |
148 PinnedTabCodec::Tabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) { | 147 StartupTabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) { |
149 Tabs results; | 148 StartupTabs results; |
150 | 149 |
151 const base::ListValue* tabs_list = NULL; | 150 const base::ListValue* tabs_list = NULL; |
152 if (!value->GetAsList(&tabs_list)) | 151 if (!value->GetAsList(&tabs_list)) |
153 return results; | 152 return results; |
154 | 153 |
155 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) { | 154 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) { |
156 base::DictionaryValue* tab_values = NULL; | 155 base::DictionaryValue* tab_values = NULL; |
157 if (tabs_list->GetDictionary(i, &tab_values)) { | 156 if (tabs_list->GetDictionary(i, &tab_values)) { |
158 Tab tab; | 157 StartupTab tab; |
159 if (DecodeTab(*tab_values, &tab)) | 158 if (DecodeTab(*tab_values, &tab)) |
160 results.push_back(tab); | 159 results.push_back(tab); |
161 } | 160 } |
162 } | 161 } |
163 return results; | 162 return results; |
164 } | 163 } |
OLD | NEW |