summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/sync_promo_ui.cc
blob: 59c05d4205a82b7dfe6ffab590bada045667a383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/sync_promo_ui.h"

#include "base/command_line.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
#include "chrome/browser/ui/webui/sync_promo_handler.h"
#include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

const char kStringsJsFile[] = "strings.js";
const char kSyncPromoJsFile[]  = "sync_promo.js";

// The maximum number of times we want to show the sync promo at startup.
const int kSyncPromoShowAtStartupMaxiumum = 10;

void RegisterSyncPromoPrefs(Profile* profile) {
  if (!profile->GetPrefs()->FindPreference(prefs::kSyncPromoStartupCount)) {
    profile->GetPrefs()->RegisterIntegerPref(
        prefs::kSyncPromoStartupCount, 0, PrefService::UNSYNCABLE_PREF);
  }
}

// The Web UI data source for the sync promo page.
class SyncPromoUIHTMLSource : public ChromeWebUIDataSource {
 public:
  SyncPromoUIHTMLSource();

 private:
  ~SyncPromoUIHTMLSource() {}
  DISALLOW_COPY_AND_ASSIGN(SyncPromoUIHTMLSource);
};

SyncPromoUIHTMLSource::SyncPromoUIHTMLSource()
    : ChromeWebUIDataSource(chrome::kChromeUISyncPromoHost) {
  DictionaryValue localized_strings;
  SyncSetupHandler::GetStaticLocalizedValues(&localized_strings);
  AddLocalizedStrings(localized_strings);
}

}  // namespace

SyncPromoUI::SyncPromoUI(TabContents* contents) : ChromeWebUI(contents) {
  should_hide_url_ = true;

  SyncPromoHandler* handler = new SyncPromoHandler(
      g_browser_process->profile_manager());
  AddMessageHandler(handler);
  handler->Attach(this);

  // Set up the chrome://theme/ source.
  Profile* profile = Profile::FromBrowserContext(contents->browser_context());
  ThemeSource* theme = new ThemeSource(profile);
  profile->GetChromeURLDataManager()->AddDataSource(theme);

  // Set up the sync promo source.
  SyncPromoUIHTMLSource* html_source =
      new SyncPromoUIHTMLSource();
  html_source->set_json_path(kStringsJsFile);
  html_source->add_resource_path(kSyncPromoJsFile, IDR_SYNC_PROMO_JS);
  html_source->set_default_resource(IDR_SYNC_PROMO_HTML);
  profile->GetChromeURLDataManager()->AddDataSource(html_source);
}

bool SyncPromoUI::ShouldShowSyncPromo(Profile* profile) {
#if defined(OS_CHROMEOS)
  // There's no need to show the sync promo on cros since cros users are logged
  // into sync already.
  return false;
#endif

  // Temporarily hide this feature behind a command line flag.
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (!command_line->HasSwitch(switches::kSyncShowPromo))
    return false;

  // Honor the sync policies.
  if (!profile->GetOriginalProfile()->IsSyncAccessible())
    return false;

  // If the user is already signed into sync then don't show the promo.
  ProfileSyncService* service =
      profile->GetOriginalProfile()->GetProfileSyncService();
  if (!service || service->HasSyncSetupCompleted())
    return false;

  return true;
}

bool IsFirstRun(Profile* profile) {
  return FirstRun::IsChromeFirstRun();
}

bool SyncPromoUI::ShouldShowSyncPromoAtStartup(Profile* profile,
                                               bool is_new_profile) {
  if (!ShouldShowSyncPromo(profile))
    return false;

  RegisterSyncPromoPrefs(profile);
  if (!is_new_profile) {
    if (!profile->GetPrefs()->HasPrefPath(prefs::kSyncPromoStartupCount))
      return false;
  }

  int show_count = profile->GetPrefs()->GetInteger(
      prefs::kSyncPromoStartupCount);
  return show_count < kSyncPromoShowAtStartupMaxiumum;
}

void SyncPromoUI::DidShowSyncPromoAtStartup(Profile* profile) {
  RegisterSyncPromoPrefs(profile);
  int show_count = profile->GetPrefs()->GetInteger(
      prefs::kSyncPromoStartupCount);
  show_count++;
  profile->GetPrefs()->SetInteger(prefs::kSyncPromoStartupCount, show_count);
}