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 |
23 // Key used in dictionaries for the app id. | 25 // Key used in dictionaries for the app id. |
24 static const char kAppID[] = "app_id"; | 26 static const char kAppID[] = "app_id"; |
25 | 27 |
26 // Key used in dictionaries for the url. | 28 // Key used in dictionaries for the url. |
27 static const char kURL[] = "url"; | 29 static const char kURL[] = "url"; |
28 | 30 |
29 // Returns true if |browser| has any pinned tabs. | 31 // Returns true if |browser| has any pinned tabs. |
30 static bool HasPinnedTabs(Browser* browser) { | 32 static bool HasPinnedTabs(Browser* browser) { |
31 TabStripModel* tab_model = browser->tabstrip_model(); | 33 TabStripModel* tab_model = browser->tabstrip_model(); |
32 for (int i = 0; i < tab_model->count(); ++i) { | 34 for (int i = 0; i < tab_model->count(); ++i) { |
33 if (tab_model->IsTabPinned(i)) | 35 if (tab_model->IsTabPinned(i)) |
34 return true; | 36 return true; |
35 } | 37 } |
36 return false; | 38 return false; |
37 } | 39 } |
38 | 40 |
39 // Adds a DictionaryValue to |values| representing |tab|. | 41 // Adds a DictionaryValue to |values| representing |tab|. |
40 static void EncodeTab(const StartupTab& tab, ListValue* values) { | 42 static void EncodeTab(const Tab& tab, ListValue* values) { |
41 scoped_ptr<DictionaryValue> value(new DictionaryValue); | 43 scoped_ptr<DictionaryValue> value(new DictionaryValue); |
42 value->SetString(kURL, tab.url.spec()); | 44 value->SetString(kURL, tab.url.spec()); |
43 if (tab.is_app) | 45 if (tab.is_app) |
44 value->SetString(kAppID, tab.app_id); | 46 value->SetString(kAppID, tab.app_id); |
45 values->Append(value.release()); | 47 values->Append(value.release()); |
46 } | 48 } |
47 | 49 |
48 // Adds a DictionaryValue to |values| representing the pinned tab at the | 50 // Adds a DictionaryValue to |values| representing the pinned tab at the |
49 // specified index. | 51 // specified index. |
50 static void EncodePinnedTab(TabStripModel* model, | 52 static void EncodePinnedTab(TabStripModel* model, |
(...skipping 26 matching lines...) Expand all Loading... |
77 | 79 |
78 // Invokes EncodePinnedTab for each pinned tab in browser. | 80 // Invokes EncodePinnedTab for each pinned tab in browser. |
79 static void EncodePinnedTabs(Browser* browser, ListValue* values) { | 81 static void EncodePinnedTabs(Browser* browser, ListValue* values) { |
80 TabStripModel* tab_model = browser->tabstrip_model(); | 82 TabStripModel* tab_model = browser->tabstrip_model(); |
81 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i) | 83 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i) |
82 EncodePinnedTab(tab_model, i, values); | 84 EncodePinnedTab(tab_model, i, values); |
83 } | 85 } |
84 | 86 |
85 // Decodes the previously written values in |value| to |tab|, returning true | 87 // Decodes the previously written values in |value| to |tab|, returning true |
86 // on success. | 88 // on success. |
87 static bool DecodeTab(const DictionaryValue& value, StartupTab* tab) { | 89 static bool DecodeTab(const DictionaryValue& value, Tab* tab) { |
88 tab->is_app = false; | 90 tab->is_app = false; |
89 | 91 |
90 std::string url_string; | 92 std::string url_string; |
91 if (!value.GetString(kURL, &url_string)) | 93 if (!value.GetString(kURL, &url_string)) |
92 return false; | 94 return false; |
93 tab->url = GURL(url_string); | 95 tab->url = GURL(url_string); |
94 | 96 |
95 if (value.GetString(kAppID, &(tab->app_id))) | 97 if (value.GetString(kAppID, &(tab->app_id))) |
96 tab->is_app = true; | 98 tab->is_app = true; |
97 | 99 |
(...skipping 17 matching lines...) Expand all Loading... |
115 Browser* browser = *i; | 117 Browser* browser = *i; |
116 if (browser->is_type_tabbed() && | 118 if (browser->is_type_tabbed() && |
117 browser->profile() == profile && HasPinnedTabs(browser)) { | 119 browser->profile() == profile && HasPinnedTabs(browser)) { |
118 EncodePinnedTabs(browser, &values); | 120 EncodePinnedTabs(browser, &values); |
119 } | 121 } |
120 } | 122 } |
121 prefs->Set(prefs::kPinnedTabs, values); | 123 prefs->Set(prefs::kPinnedTabs, values); |
122 } | 124 } |
123 | 125 |
124 // static | 126 // static |
125 void PinnedTabCodec::WritePinnedTabs(Profile* profile, | 127 void PinnedTabCodec::WritePinnedTabs(Profile* profile, const Tabs& tabs) { |
126 const StartupTabs& tabs) { | |
127 PrefService* prefs = profile->GetPrefs(); | 128 PrefService* prefs = profile->GetPrefs(); |
128 if (!prefs) | 129 if (!prefs) |
129 return; | 130 return; |
130 | 131 |
131 ListPrefUpdate update(prefs, prefs::kPinnedTabs); | 132 ListPrefUpdate update(prefs, prefs::kPinnedTabs); |
132 ListValue* values = update.Get(); | 133 ListValue* values = update.Get(); |
133 values->Clear(); | 134 values->Clear(); |
134 for (StartupTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) | 135 for (Tabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) |
135 EncodeTab(*i, values); | 136 EncodeTab(*i, values); |
136 } | 137 } |
137 | 138 |
138 // static | 139 // static |
139 StartupTabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) { | 140 PinnedTabCodec::Tabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) { |
140 PrefService* prefs = profile->GetPrefs(); | 141 PrefService* prefs = profile->GetPrefs(); |
141 if (!prefs) | 142 if (!prefs) |
142 return StartupTabs(); | 143 return Tabs(); |
143 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs)); | 144 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs)); |
144 } | 145 } |
145 | 146 |
146 // static | 147 // static |
147 StartupTabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) { | 148 PinnedTabCodec::Tabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) { |
148 StartupTabs results; | 149 Tabs results; |
149 | 150 |
150 const base::ListValue* tabs_list = NULL; | 151 const base::ListValue* tabs_list = NULL; |
151 if (!value->GetAsList(&tabs_list)) | 152 if (!value->GetAsList(&tabs_list)) |
152 return results; | 153 return results; |
153 | 154 |
154 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) { | 155 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) { |
155 base::DictionaryValue* tab_values = NULL; | 156 base::DictionaryValue* tab_values = NULL; |
156 if (tabs_list->GetDictionary(i, &tab_values)) { | 157 if (tabs_list->GetDictionary(i, &tab_values)) { |
157 StartupTab tab; | 158 Tab tab; |
158 if (DecodeTab(*tab_values, &tab)) | 159 if (DecodeTab(*tab_values, &tab)) |
159 results.push_back(tab); | 160 results.push_back(tab); |
160 } | 161 } |
161 } | 162 } |
162 return results; | 163 return results; |
163 } | 164 } |
OLD | NEW |