OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/ntp/new_tab_sync_setup_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/new_tab_sync_setup_handler.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/prefs/pref_notifier.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" |
8 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/sync/profile_sync_service.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "content/common/notification_details.h" |
| 16 |
| 17 bool NewTabSyncSetupHandler::ShouldShowSyncPromo() { |
| 18 #if defined(OS_CHROMEOS) |
| 19 // There's no need to show the sync promo on cros since cros users are logged |
| 20 // into sync already. |
| 21 return false; |
| 22 #endif |
| 23 |
| 24 // Temporarily hide this feature behind a command line flag. |
| 25 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 26 return command_line->HasSwitch(switches::kSyncShowPromo); |
| 27 } |
| 28 |
| 29 WebUIMessageHandler* NewTabSyncSetupHandler::Attach(WebUI* web_ui) { |
| 30 PrefService* pref_service = web_ui->GetProfile()->GetPrefs(); |
| 31 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); |
| 32 |
| 33 return SyncSetupHandler::Attach(web_ui); |
| 34 } |
| 35 |
| 36 void NewTabSyncSetupHandler::RegisterMessages() { |
| 37 web_ui_->RegisterMessageCallback("InitializeSyncPromo", |
| 38 NewCallback(this, &NewTabSyncSetupHandler::HandleInitializeSyncPromo)); |
| 39 web_ui_->RegisterMessageCallback("CollapseSyncPromo", |
| 40 NewCallback(this, &NewTabSyncSetupHandler::HandleCollapseSyncPromo)); |
| 41 web_ui_->RegisterMessageCallback("ExpandSyncPromo", |
| 42 NewCallback(this, &NewTabSyncSetupHandler::HandleExpandSyncPromo)); |
| 43 |
| 44 SyncSetupHandler::RegisterMessages(); |
| 45 } |
| 46 |
| 47 void NewTabSyncSetupHandler::Observe(int type, |
| 48 const NotificationSource& source, |
| 49 const NotificationDetails& details) { |
| 50 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 51 std::string* name = Details<std::string>(details).ptr(); |
| 52 if (prefs::kGoogleServicesUsername == *name) { |
| 53 UpdateLogin(); |
| 54 return; |
| 55 } |
| 56 } |
| 57 SyncSetupHandler::Observe(type, source, details); |
| 58 } |
11 | 59 |
12 void NewTabSyncSetupHandler::ShowSetupUI() { | 60 void NewTabSyncSetupHandler::ShowSetupUI() { |
13 // Temporarily hide this feature behind a command line flag. | 61 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); |
14 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 62 service->get_wizard().Step(SyncSetupWizard::GAIA_LOGIN); |
15 if (!command_line->HasSwitch(switches::kSyncShowPromo)) | 63 } |
| 64 |
| 65 void NewTabSyncSetupHandler::HandleInitializeSyncPromo(const ListValue* args) { |
| 66 if (!ShouldShowSyncPromo()) |
16 return; | 67 return; |
17 | 68 |
| 69 // Make sure the sync promo is visible. |
| 70 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.showSyncPromo"); |
| 71 |
| 72 UpdateLogin(); |
| 73 |
18 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); | 74 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); |
19 DCHECK(service); | 75 DCHECK(service); |
20 | 76 |
21 // If they user has already synced then don't show anything. | 77 // If the user has not signed into sync then expand the sync promo. |
22 if (service->HasSyncSetupCompleted()) | 78 // TODO(sail): Need to throttle this behind a server side flag. |
23 return; | 79 if (!service->HasSyncSetupCompleted()) |
| 80 OpenSyncSetup(); |
| 81 } |
24 | 82 |
25 service->get_wizard().Step(SyncSetupWizard::GAIA_LOGIN); | 83 void NewTabSyncSetupHandler::HandleCollapseSyncPromo(const ListValue* args) { |
| 84 CloseSyncSetup(); |
26 } | 85 } |
| 86 |
| 87 |
| 88 void NewTabSyncSetupHandler::HandleExpandSyncPromo(const ListValue* args) { |
| 89 OpenSyncSetup(); |
| 90 } |
| 91 |
| 92 void NewTabSyncSetupHandler::UpdateLogin() { |
| 93 std::string username = web_ui_->GetProfile()->GetPrefs()->GetString( |
| 94 prefs::kGoogleServicesUsername); |
| 95 StringValue string_value(username); |
| 96 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.updateLogin", |
| 97 string_value); |
| 98 } |
OLD | NEW |