| OLD | NEW |
| (Empty) |
| 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/ui/webui/sync_promo_ui.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/first_run/first_run.h" | |
| 11 #include "chrome/browser/google/google_util.h" | |
| 12 #include "chrome/browser/prefs/pref_service.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | |
| 15 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | |
| 17 #include "chrome/browser/ui/webui/options/core_options_handler.h" | |
| 18 #include "chrome/browser/ui/webui/sync_promo_handler.h" | |
| 19 #include "chrome/browser/ui/webui/theme_source.h" | |
| 20 #include "chrome/common/chrome_switches.h" | |
| 21 #include "chrome/common/pref_names.h" | |
| 22 #include "chrome/common/url_constants.h" | |
| 23 #include "content/browser/tab_contents/tab_contents.h" | |
| 24 #include "googleurl/src/url_util.h" | |
| 25 #include "grit/browser_resources.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "grit/theme_resources.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kStringsJsFile[] = "strings.js"; | |
| 33 const char kSyncPromoJsFile[] = "sync_promo.js"; | |
| 34 | |
| 35 const char kSyncPromoQueryKeyIsLaunchPage[] = "is_launch_page"; | |
| 36 const char kSyncPromoQueryKeyNextPage[] = "next_page"; | |
| 37 | |
| 38 // The maximum number of times we want to show the sync promo at startup. | |
| 39 const int kSyncPromoShowAtStartupMaximum = 10; | |
| 40 | |
| 41 // Checks we want to show the sync promo for the given brand. | |
| 42 bool AllowPromoAtStartupForCurrentBrand() { | |
| 43 std::string brand; | |
| 44 google_util::GetBrand(&brand); | |
| 45 | |
| 46 if (brand.empty()) | |
| 47 return true; | |
| 48 | |
| 49 if (google_util::IsInternetCafeBrandCode(brand)) | |
| 50 return false; | |
| 51 | |
| 52 if (google_util::IsOrganic(brand)) | |
| 53 return true; | |
| 54 | |
| 55 if (StartsWithASCII(brand, "CH", true)) | |
| 56 return true; | |
| 57 | |
| 58 // Default to disallow for all other brand codes. | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 // The Web UI data source for the sync promo page. | |
| 63 class SyncPromoUIHTMLSource : public ChromeWebUIDataSource { | |
| 64 public: | |
| 65 explicit SyncPromoUIHTMLSource(WebUI* web_ui); | |
| 66 | |
| 67 private: | |
| 68 ~SyncPromoUIHTMLSource() {} | |
| 69 DISALLOW_COPY_AND_ASSIGN(SyncPromoUIHTMLSource); | |
| 70 }; | |
| 71 | |
| 72 SyncPromoUIHTMLSource::SyncPromoUIHTMLSource(WebUI* web_ui) | |
| 73 : ChromeWebUIDataSource(chrome::kChromeUISyncPromoHost) { | |
| 74 DictionaryValue localized_strings; | |
| 75 CoreOptionsHandler::GetStaticLocalizedValues(&localized_strings); | |
| 76 SyncSetupHandler::GetStaticLocalizedValues(&localized_strings, web_ui); | |
| 77 AddLocalizedStrings(localized_strings); | |
| 78 } | |
| 79 | |
| 80 // Looks for |search_key| in the query portion of |url|. Returns true if the | |
| 81 // key is found and sets |out_value| to the value for the key. Returns false if | |
| 82 // the key is not found. | |
| 83 bool GetValueForKeyInQuery(const GURL& url, const std::string& search_key, | |
| 84 std::string* out_value) { | |
| 85 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
| 86 url_parse::Component key, value; | |
| 87 while (url_parse::ExtractQueryKeyValue( | |
| 88 url.spec().c_str(), &query, &key, &value)) { | |
| 89 if (key.is_nonempty()) { | |
| 90 std::string key_string = url.spec().substr(key.begin, key.len); | |
| 91 if (key_string == search_key) { | |
| 92 if (value.is_nonempty()) | |
| 93 *out_value = url.spec().substr(value.begin, value.len); | |
| 94 else | |
| 95 *out_value = ""; | |
| 96 return true; | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 SyncPromoUI::SyncPromoUI(TabContents* contents) : ChromeWebUI(contents) { | |
| 106 should_hide_url_ = true; | |
| 107 | |
| 108 SyncPromoHandler* handler = new SyncPromoHandler( | |
| 109 g_browser_process->profile_manager()); | |
| 110 AddMessageHandler(handler); | |
| 111 handler->Attach(this); | |
| 112 | |
| 113 // Set up the chrome://theme/ source. | |
| 114 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | |
| 115 ThemeSource* theme = new ThemeSource(profile); | |
| 116 profile->GetChromeURLDataManager()->AddDataSource(theme); | |
| 117 | |
| 118 // Set up the sync promo source. | |
| 119 SyncPromoUIHTMLSource* html_source = new SyncPromoUIHTMLSource(this); | |
| 120 html_source->set_json_path(kStringsJsFile); | |
| 121 html_source->add_resource_path(kSyncPromoJsFile, IDR_SYNC_PROMO_JS); | |
| 122 html_source->set_default_resource(IDR_SYNC_PROMO_HTML); | |
| 123 profile->GetChromeURLDataManager()->AddDataSource(html_source); | |
| 124 } | |
| 125 | |
| 126 // static | |
| 127 bool SyncPromoUI::ShouldShowSyncPromo(Profile* profile) { | |
| 128 #if defined(OS_CHROMEOS) | |
| 129 // There's no need to show the sync promo on cros since cros users are logged | |
| 130 // into sync already. | |
| 131 return false; | |
| 132 #endif | |
| 133 | |
| 134 // Honor the sync policies. | |
| 135 if (!profile->GetOriginalProfile()->IsSyncAccessible()) | |
| 136 return false; | |
| 137 | |
| 138 // If the user is already signed into sync then don't show the promo. | |
| 139 ProfileSyncService* service = | |
| 140 profile->GetOriginalProfile()->GetProfileSyncService(); | |
| 141 if (!service || service->HasSyncSetupCompleted()) | |
| 142 return false; | |
| 143 | |
| 144 // Default to allow the promo. | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 // static | |
| 149 void SyncPromoUI::RegisterUserPrefs(PrefService* prefs) { | |
| 150 prefs->RegisterIntegerPref( | |
| 151 prefs::kSyncPromoStartupCount, 0, PrefService::UNSYNCABLE_PREF); | |
| 152 prefs->RegisterBooleanPref( | |
| 153 prefs::kSyncPromoUserSkipped, false, PrefService::UNSYNCABLE_PREF); | |
| 154 prefs->RegisterBooleanPref(prefs::kSyncPromoShowOnFirstRunAllowed, true, | |
| 155 PrefService::UNSYNCABLE_PREF); | |
| 156 | |
| 157 SyncPromoHandler::RegisterUserPrefs(prefs); | |
| 158 } | |
| 159 | |
| 160 bool SyncPromoUI::ShouldShowSyncPromoAtStartup(Profile* profile, | |
| 161 bool is_new_profile) { | |
| 162 if (!ShouldShowSyncPromo(profile)) | |
| 163 return false; | |
| 164 | |
| 165 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 166 if (command_line.HasSwitch(switches::kNoFirstRun)) | |
| 167 is_new_profile = false; | |
| 168 | |
| 169 PrefService *prefs = profile->GetPrefs(); | |
| 170 if (!is_new_profile) { | |
| 171 if (!prefs->HasPrefPath(prefs::kSyncPromoStartupCount)) | |
| 172 return false; | |
| 173 } | |
| 174 | |
| 175 if (HasUserSkippedSyncPromo(profile)) | |
| 176 return false; | |
| 177 | |
| 178 // For Chinese users skip the sync promo. | |
| 179 if (g_browser_process->GetApplicationLocale() == "zh-CN") | |
| 180 return false; | |
| 181 | |
| 182 int show_count = prefs->GetInteger(prefs::kSyncPromoStartupCount); | |
| 183 if (show_count >= kSyncPromoShowAtStartupMaximum) | |
| 184 return false; | |
| 185 | |
| 186 // This pref can be set in the master preferences file to allow or disallow | |
| 187 // showing the sync promo at startup. | |
| 188 if (prefs->HasPrefPath(prefs::kSyncPromoShowOnFirstRunAllowed)) | |
| 189 return prefs->GetBoolean(prefs::kSyncPromoShowOnFirstRunAllowed); | |
| 190 | |
| 191 // For now don't show the promo for some brands. | |
| 192 if (!AllowPromoAtStartupForCurrentBrand()) | |
| 193 return false; | |
| 194 | |
| 195 // Default to show the promo. | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 199 void SyncPromoUI::DidShowSyncPromoAtStartup(Profile* profile) { | |
| 200 int show_count = profile->GetPrefs()->GetInteger( | |
| 201 prefs::kSyncPromoStartupCount); | |
| 202 show_count++; | |
| 203 profile->GetPrefs()->SetInteger(prefs::kSyncPromoStartupCount, show_count); | |
| 204 } | |
| 205 | |
| 206 bool SyncPromoUI::HasUserSkippedSyncPromo(Profile* profile) { | |
| 207 return profile->GetPrefs()->GetBoolean(prefs::kSyncPromoUserSkipped); | |
| 208 } | |
| 209 | |
| 210 void SyncPromoUI::SetUserSkippedSyncPromo(Profile* profile) { | |
| 211 profile->GetPrefs()->SetBoolean(prefs::kSyncPromoUserSkipped, true); | |
| 212 } | |
| 213 | |
| 214 // static | |
| 215 GURL SyncPromoUI::GetSyncPromoURL(const GURL& next_page, bool show_title) { | |
| 216 std::stringstream stream; | |
| 217 stream << chrome::kChromeUISyncPromoURL << "?" | |
| 218 << kSyncPromoQueryKeyIsLaunchPage << "=" | |
| 219 << (show_title ? "true" : "false"); | |
| 220 | |
| 221 if (!next_page.spec().empty()) { | |
| 222 url_canon::RawCanonOutputT<char> output; | |
| 223 url_util::EncodeURIComponent( | |
| 224 next_page.spec().c_str(), next_page.spec().length(), &output); | |
| 225 std::string escaped_spec(output.data(), output.length()); | |
| 226 stream << "&" << kSyncPromoQueryKeyNextPage << "=" << escaped_spec; | |
| 227 } | |
| 228 | |
| 229 return GURL(stream.str()); | |
| 230 } | |
| 231 | |
| 232 // static | |
| 233 bool SyncPromoUI::GetIsLaunchPageForSyncPromoURL(const GURL& url) { | |
| 234 std::string value; | |
| 235 // Show the title if the promo is currently the Chrome launch page (and not | |
| 236 // the page accessed through the NTP). | |
| 237 if (GetValueForKeyInQuery(url, kSyncPromoQueryKeyIsLaunchPage, &value)) | |
| 238 return value == "true"; | |
| 239 return false; | |
| 240 } | |
| 241 | |
| 242 // static | |
| 243 GURL SyncPromoUI::GetNextPageURLForSyncPromoURL(const GURL& url) { | |
| 244 std::string value; | |
| 245 if (GetValueForKeyInQuery(url, kSyncPromoQueryKeyNextPage, &value)) { | |
| 246 url_canon::RawCanonOutputT<char16> output; | |
| 247 url_util::DecodeURLEscapeSequences(value.c_str(), value.length(), &output); | |
| 248 std::string url; | |
| 249 UTF16ToUTF8(output.data(), output.length(), &url); | |
| 250 return GURL(url); | |
| 251 } | |
| 252 return GURL(); | |
| 253 } | |
| 254 | |
| 255 // static | |
| 256 bool SyncPromoUI::UserHasSeenSyncPromoAtStartup(Profile* profile) { | |
| 257 return profile->GetPrefs()->GetInteger(prefs::kSyncPromoStartupCount) > 0; | |
| 258 } | |
| OLD | NEW |