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