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