| OLD | NEW |
| (Empty) |
| 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 "base/basictypes.h" | |
| 6 #include "base/compiler_specific.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 11 #include "chrome/browser/protector/base_prefs_change.h" | |
| 12 #include "chrome/browser/protector/histograms.h" | |
| 13 #include "chrome/browser/protector/protector_service.h" | |
| 14 #include "chrome/browser/protector/protector_service_factory.h" | |
| 15 #include "chrome/browser/ui/browser.h" | |
| 16 #include "chrome/browser/ui/browser_navigator.h" | |
| 17 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" | |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "grit/chromium_strings.h" | |
| 22 #include "grit/generated_resources.h" | |
| 23 #include "grit/theme_resources.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 | |
| 26 namespace protector { | |
| 27 | |
| 28 // Session startup settings change tracked by Protector. | |
| 29 class SessionStartupChange : public BasePrefsChange { | |
| 30 public: | |
| 31 SessionStartupChange(const SessionStartupPref& actual_startup_pref, | |
| 32 const StartupTabs& actual_pinned_tabs, | |
| 33 const SessionStartupPref& backup_startup_pref, | |
| 34 const StartupTabs& backup_pinned_tabs); | |
| 35 | |
| 36 // BaseSettingChange overrides: | |
| 37 virtual bool Init(Profile* profile) OVERRIDE; | |
| 38 virtual void Apply(Browser* browser) OVERRIDE; | |
| 39 virtual void Discard(Browser* browser) OVERRIDE; | |
| 40 virtual void Timeout() OVERRIDE; | |
| 41 virtual int GetBadgeIconID() const OVERRIDE; | |
| 42 virtual int GetMenuItemIconID() const OVERRIDE; | |
| 43 virtual int GetBubbleIconID() const OVERRIDE; | |
| 44 virtual string16 GetBubbleTitle() const OVERRIDE; | |
| 45 virtual string16 GetBubbleMessage() const OVERRIDE; | |
| 46 virtual string16 GetApplyButtonText() const OVERRIDE; | |
| 47 virtual string16 GetDiscardButtonText() const OVERRIDE; | |
| 48 virtual DisplayName GetApplyDisplayName() const OVERRIDE; | |
| 49 virtual GURL GetNewSettingURL() const OVERRIDE; | |
| 50 | |
| 51 private: | |
| 52 virtual ~SessionStartupChange(); | |
| 53 | |
| 54 // Returns the first URL that was added to the startup pages. Returns the | |
| 55 // first startup URL if they haven't changed. Returns an empty URL if there | |
| 56 // are no startup URLs. | |
| 57 GURL GetFirstNewURL() const; | |
| 58 | |
| 59 // Opens all tabs in |tabs| and makes them pinned. | |
| 60 void OpenPinnedTabs(Browser* browser, const StartupTabs& tabs); | |
| 61 | |
| 62 const SessionStartupPref new_startup_pref_; | |
| 63 const SessionStartupPref backup_startup_pref_; | |
| 64 const StartupTabs new_pinned_tabs_; | |
| 65 const StartupTabs backup_pinned_tabs_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(SessionStartupChange); | |
| 68 }; | |
| 69 | |
| 70 SessionStartupChange::SessionStartupChange( | |
| 71 const SessionStartupPref& actual_startup_pref, | |
| 72 const StartupTabs& actual_pinned_tabs, | |
| 73 const SessionStartupPref& backup_startup_pref, | |
| 74 const StartupTabs& backup_pinned_tabs) | |
| 75 : new_startup_pref_(actual_startup_pref), | |
| 76 backup_startup_pref_(backup_startup_pref), | |
| 77 new_pinned_tabs_(actual_pinned_tabs), | |
| 78 backup_pinned_tabs_(backup_pinned_tabs) { | |
| 79 UMA_HISTOGRAM_ENUMERATION( | |
| 80 kProtectorHistogramStartupSettingsChanged, | |
| 81 actual_startup_pref.type, | |
| 82 SessionStartupPref::TYPE_COUNT); | |
| 83 } | |
| 84 | |
| 85 SessionStartupChange::~SessionStartupChange() { | |
| 86 } | |
| 87 | |
| 88 bool SessionStartupChange::Init(Profile* profile) { | |
| 89 if (!BasePrefsChange::Init(profile)) | |
| 90 return false; | |
| 91 SessionStartupPref::SetStartupPref(profile, backup_startup_pref_); | |
| 92 PinnedTabCodec::WritePinnedTabs(profile, backup_pinned_tabs_); | |
| 93 DismissOnPrefChange(prefs::kRestoreOnStartup); | |
| 94 DismissOnPrefChange(prefs::kURLsToRestoreOnStartup); | |
| 95 DismissOnPrefChange(prefs::kPinnedTabs); | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 void SessionStartupChange::Apply(Browser* browser) { | |
| 100 UMA_HISTOGRAM_ENUMERATION( | |
| 101 kProtectorHistogramStartupSettingsApplied, | |
| 102 new_startup_pref_.type, | |
| 103 SessionStartupPref::TYPE_COUNT); | |
| 104 IgnorePrefChanges(); | |
| 105 SessionStartupPref::SetStartupPref(profile(), new_startup_pref_); | |
| 106 PinnedTabCodec::WritePinnedTabs(profile(), new_pinned_tabs_); | |
| 107 OpenPinnedTabs(browser, new_pinned_tabs_); | |
| 108 } | |
| 109 | |
| 110 void SessionStartupChange::Discard(Browser* browser) { | |
| 111 UMA_HISTOGRAM_ENUMERATION( | |
| 112 kProtectorHistogramStartupSettingsDiscarded, | |
| 113 new_startup_pref_.type, | |
| 114 SessionStartupPref::TYPE_COUNT); | |
| 115 IgnorePrefChanges(); | |
| 116 // Nothing to do here since backup has already been made active by Init(). | |
| 117 } | |
| 118 | |
| 119 void SessionStartupChange::Timeout() { | |
| 120 UMA_HISTOGRAM_ENUMERATION( | |
| 121 kProtectorHistogramStartupSettingsTimeout, | |
| 122 new_startup_pref_.type, | |
| 123 SessionStartupPref::TYPE_COUNT); | |
| 124 } | |
| 125 | |
| 126 int SessionStartupChange::GetBadgeIconID() const { | |
| 127 // Icons are the same for homepage and startup settings. | |
| 128 return IDR_HOMEPAGE_CHANGE_BADGE; | |
| 129 } | |
| 130 | |
| 131 int SessionStartupChange::GetMenuItemIconID() const { | |
| 132 return IDR_HOMEPAGE_CHANGE_MENU; | |
| 133 } | |
| 134 | |
| 135 int SessionStartupChange::GetBubbleIconID() const { | |
| 136 return IDR_HOMEPAGE_CHANGE_ALERT; | |
| 137 } | |
| 138 | |
| 139 string16 SessionStartupChange::GetBubbleTitle() const { | |
| 140 return l10n_util::GetStringUTF16(IDS_STARTUP_SETTINGS_CHANGE_TITLE); | |
| 141 } | |
| 142 | |
| 143 string16 SessionStartupChange::GetBubbleMessage() const { | |
| 144 return l10n_util::GetStringUTF16(IDS_STARTUP_SETTINGS_CHANGE_BUBBLE_MESSAGE); | |
| 145 } | |
| 146 | |
| 147 string16 SessionStartupChange::GetApplyButtonText() const { | |
| 148 if (new_startup_pref_.type == SessionStartupPref::LAST) | |
| 149 return l10n_util::GetStringUTF16(IDS_CHANGE_STARTUP_SETTINGS_RESTORE); | |
| 150 | |
| 151 // Display the domain name of the first startup/pinned URL. | |
| 152 string16 first_domain = UTF8ToUTF16(GetFirstNewURL().host()); | |
| 153 return first_domain.empty() ? | |
| 154 l10n_util::GetStringUTF16(IDS_CHANGE_STARTUP_SETTINGS_NTP) : | |
| 155 l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS, | |
| 156 first_domain); | |
| 157 } | |
| 158 | |
| 159 string16 SessionStartupChange::GetDiscardButtonText() const { | |
| 160 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); | |
| 161 } | |
| 162 | |
| 163 BaseSettingChange::DisplayName | |
| 164 SessionStartupChange::GetApplyDisplayName() const { | |
| 165 string16 first_domain = UTF8ToUTF16(GetFirstNewURL().host()); | |
| 166 return first_domain.empty() ? | |
| 167 BaseSettingChange::GetApplyDisplayName() : | |
| 168 DisplayName(kSessionStartupChangeNamePriority, first_domain); | |
| 169 } | |
| 170 | |
| 171 GURL SessionStartupChange::GetNewSettingURL() const { | |
| 172 VLOG(1) << "HP URL: " << GetFirstNewURL().spec(); | |
| 173 return GetFirstNewURL(); | |
| 174 } | |
| 175 | |
| 176 GURL SessionStartupChange::GetFirstNewURL() const { | |
| 177 if (new_startup_pref_.type == SessionStartupPref::LAST) | |
| 178 return GURL(); | |
| 179 | |
| 180 std::set<GURL> old_urls; | |
| 181 if (backup_startup_pref_.type == SessionStartupPref::URLS) { | |
| 182 old_urls.insert(backup_startup_pref_.urls.begin(), | |
| 183 backup_startup_pref_.urls.end()); | |
| 184 } | |
| 185 for (size_t i = 0; i < backup_pinned_tabs_.size(); ++i) | |
| 186 old_urls.insert(backup_pinned_tabs_[i].url); | |
| 187 | |
| 188 std::vector<GURL> new_urls; | |
| 189 if (new_startup_pref_.type == SessionStartupPref::URLS) { | |
| 190 new_urls.insert(new_urls.end(), new_startup_pref_.urls.begin(), | |
| 191 new_startup_pref_.urls.end()); | |
| 192 } | |
| 193 for (size_t i = 0; i < new_pinned_tabs_.size(); ++i) | |
| 194 new_urls.push_back(new_pinned_tabs_[i].url); | |
| 195 | |
| 196 if (new_urls.empty()) | |
| 197 return GURL(); | |
| 198 | |
| 199 // First try to find a URL in new settings that is not present in backup. | |
| 200 for (size_t i = 0; i < new_urls.size(); ++i) { | |
| 201 if (!old_urls.count(new_urls[i])) | |
| 202 return new_urls[i]; | |
| 203 } | |
| 204 // Then fallback to the first of the current startup URLs - this means that | |
| 205 // URLs themselves haven't changed. | |
| 206 return new_urls[0]; | |
| 207 } | |
| 208 | |
| 209 void SessionStartupChange::OpenPinnedTabs(Browser* browser, | |
| 210 const StartupTabs& tabs) { | |
| 211 for (size_t i = 0; i < tabs.size(); ++i) { | |
| 212 chrome::NavigateParams params(browser, tabs[i].url, | |
| 213 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
| 214 params.disposition = NEW_BACKGROUND_TAB; | |
| 215 params.tabstrip_index = -1; | |
| 216 params.tabstrip_add_types = TabStripModel::ADD_PINNED; | |
| 217 params.extension_app_id = tabs[i].app_id; | |
| 218 chrome::Navigate(¶ms); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 BaseSettingChange* CreateSessionStartupChange( | |
| 223 const SessionStartupPref& actual_startup_pref, | |
| 224 const StartupTabs& actual_pinned_tabs, | |
| 225 const SessionStartupPref& backup_startup_pref, | |
| 226 const StartupTabs& backup_pinned_tabs) { | |
| 227 return new SessionStartupChange(actual_startup_pref, actual_pinned_tabs, | |
| 228 backup_startup_pref, backup_pinned_tabs); | |
| 229 } | |
| 230 | |
| 231 } // namespace protector | |
| OLD | NEW |