pinned_tab_codec.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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. 4 5#include "chrome/browser/ui/tabs/pinned_tab_codec.h" 6 7#include "base/prefs/pref_service.h" 8#include "base/values.h" 9#include "chrome/browser/extensions/tab_helper.h" 10#include "chrome/browser/prefs/scoped_user_pref_update.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/extension.h" 17#include "chrome/common/pref_names.h" 18#include "components/user_prefs/pref_registry_syncable.h" 19#include "content/public/browser/navigation_entry.h" 20#include "content/public/browser/web_contents.h" 21 22using content::NavigationEntry; 23 24// Key used in dictionaries for the app id. 25static const char kAppID[] = "app_id"; 26 27// Key used in dictionaries for the url. 28static const char kURL[] = "url"; 29 30// Returns true if |browser| has any pinned tabs. 31static bool HasPinnedTabs(Browser* browser) { 32 TabStripModel* tab_model = browser->tab_strip_model(); 33 for (int i = 0; i < tab_model->count(); ++i) { 34 if (tab_model->IsTabPinned(i)) 35 return true; 36 } 37 return false; 38} 39 40// Adds a DictionaryValue to |values| representing |tab|. 41static void EncodeTab(const StartupTab& tab, ListValue* values) { 42 scoped_ptr<DictionaryValue> value(new DictionaryValue); 43 value->SetString(kURL, tab.url.spec()); 44 if (tab.is_app) 45 value->SetString(kAppID, tab.app_id); 46 values->Append(value.release()); 47} 48 49// Adds a DictionaryValue to |values| representing the pinned tab at the 50// specified index. 51static void EncodePinnedTab(TabStripModel* model, 52 int index, 53 ListValue* values) { 54 scoped_ptr<DictionaryValue> value(new DictionaryValue()); 55 56 content::WebContents* web_contents = model->GetWebContentsAt(index); 57 if (model->IsAppTab(index)) { 58 const extensions::Extension* extension = 59 extensions::TabHelper::FromWebContents(web_contents)->extension_app(); 60 DCHECK(extension); 61 value->SetString(kAppID, extension->id()); 62 // For apps we use the launch url. We do this because the user is 63 // effectively restarting the app, so returning them to the app's launch 64 // page seems closest to what they expect. 65 value->SetString(kURL, extension->GetFullLaunchURL().spec()); 66 values->Append(value.release()); 67 } else { 68 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); 69 if (!entry && web_contents->GetController().GetEntryCount()) 70 entry = web_contents->GetController().GetEntryAtIndex(0); 71 if (entry) { 72 value->SetString(kURL, entry->GetURL().spec()); 73 values->Append(value.release()); 74 } 75 } 76} 77 78// Invokes EncodePinnedTab for each pinned tab in browser. 79static void EncodePinnedTabs(Browser* browser, ListValue* values) { 80 TabStripModel* tab_model = browser->tab_strip_model(); 81 for (int i = 0; i < tab_model->count() && tab_model->IsTabPinned(i); ++i) 82 EncodePinnedTab(tab_model, i, values); 83} 84 85// Decodes the previously written values in |value| to |tab|, returning true 86// on success. 87static bool DecodeTab(const DictionaryValue& value, StartupTab* tab) { 88 tab->is_app = false; 89 90 std::string url_string; 91 if (!value.GetString(kURL, &url_string)) 92 return false; 93 tab->url = GURL(url_string); 94 95 if (value.GetString(kAppID, &(tab->app_id))) 96 tab->is_app = true; 97 98 return true; 99} 100 101// static 102void PinnedTabCodec::RegisterUserPrefs( 103 user_prefs::PrefRegistrySyncable* registry) { 104 registry->RegisterListPref(prefs::kPinnedTabs, 105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 106} 107 108// static 109void PinnedTabCodec::WritePinnedTabs(Profile* profile) { 110 PrefService* prefs = profile->GetPrefs(); 111 if (!prefs) 112 return; 113 114 ListValue values; 115 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 116 Browser* browser = *it; 117 if (browser->is_type_tabbed() && 118 browser->profile() == profile && HasPinnedTabs(browser)) { 119 EncodePinnedTabs(browser, &values); 120 } 121 } 122 prefs->Set(prefs::kPinnedTabs, values); 123} 124 125// static 126void PinnedTabCodec::WritePinnedTabs(Profile* profile, 127 const StartupTabs& tabs) { 128 PrefService* prefs = profile->GetPrefs(); 129 if (!prefs) 130 return; 131 132 ListPrefUpdate update(prefs, prefs::kPinnedTabs); 133 ListValue* values = update.Get(); 134 values->Clear(); 135 for (StartupTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) 136 EncodeTab(*i, values); 137} 138 139// static 140StartupTabs PinnedTabCodec::ReadPinnedTabs(Profile* profile) { 141 PrefService* prefs = profile->GetPrefs(); 142 if (!prefs) 143 return StartupTabs(); 144 return ReadPinnedTabs(prefs->GetList(prefs::kPinnedTabs)); 145} 146 147// static 148StartupTabs PinnedTabCodec::ReadPinnedTabs(const base::Value* value) { 149 StartupTabs results; 150 151 const base::ListValue* tabs_list = NULL; 152 if (!value->GetAsList(&tabs_list)) 153 return results; 154 155 for (size_t i = 0, max = tabs_list->GetSize(); i < max; ++i) { 156 const base::DictionaryValue* tab_values = NULL; 157 if (tabs_list->GetDictionary(i, &tab_values)) { 158 StartupTab tab; 159 if (DecodeTab(*tab_values, &tab)) 160 results.push_back(tab); 161 } 162 } 163 return results; 164} 165