| 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" | 8 #include "chrome/browser/prefs/pref_notifier.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #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" | 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/common/notification_details.h" | 16 #include "content/common/notification_details.h" |
| 16 | 17 |
| 17 NewTabSyncSetupHandler::NewTabSyncSetupHandler() : SyncSetupHandler() { | 18 NewTabSyncSetupHandler::NewTabSyncSetupHandler() : SyncSetupHandler() { |
| 18 } | 19 } |
| 19 | 20 |
| 20 NewTabSyncSetupHandler::~NewTabSyncSetupHandler() { | 21 NewTabSyncSetupHandler::~NewTabSyncSetupHandler() { |
| 21 } | 22 } |
| 22 | 23 |
| 23 bool NewTabSyncSetupHandler::ShouldShowSyncPromo() { | 24 bool NewTabSyncSetupHandler::ShouldShowSyncPromo() { |
| 24 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 25 // There's no need to show the sync promo on cros since cros users are logged | 26 // There's no need to show the sync promo on cros since cros users are logged |
| 26 // into sync already. | 27 // into sync already. |
| 27 return false; | 28 return false; |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 // Temporarily hide this feature behind a command line flag. | 31 // Temporarily hide this feature behind a command line flag. |
| 31 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 32 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 32 return command_line->HasSwitch(switches::kSyncShowPromo); | 33 return command_line->HasSwitch(switches::kSyncShowPromo); |
| 33 } | 34 } |
| 34 | 35 |
| 35 WebUIMessageHandler* NewTabSyncSetupHandler::Attach(WebUI* web_ui) { | 36 WebUIMessageHandler* NewTabSyncSetupHandler::Attach(WebUI* web_ui) { |
| 36 PrefService* pref_service = web_ui->GetProfile()->GetPrefs(); | 37 Profile* profile = |
| 38 Profile::FromBrowserContext(web_ui->tab_contents()->browser_context()); |
| 39 PrefService* pref_service = profile->GetPrefs(); |
| 37 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); | 40 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); |
| 38 | 41 |
| 39 return SyncSetupHandler::Attach(web_ui); | 42 return SyncSetupHandler::Attach(web_ui); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void NewTabSyncSetupHandler::RegisterMessages() { | 45 void NewTabSyncSetupHandler::RegisterMessages() { |
| 43 web_ui_->RegisterMessageCallback("InitializeSyncPromo", | 46 web_ui_->RegisterMessageCallback("InitializeSyncPromo", |
| 44 NewCallback(this, &NewTabSyncSetupHandler::HandleInitializeSyncPromo)); | 47 NewCallback(this, &NewTabSyncSetupHandler::HandleInitializeSyncPromo)); |
| 45 web_ui_->RegisterMessageCallback("CollapseSyncPromo", | 48 web_ui_->RegisterMessageCallback("CollapseSyncPromo", |
| 46 NewCallback(this, &NewTabSyncSetupHandler::HandleCollapseSyncPromo)); | 49 NewCallback(this, &NewTabSyncSetupHandler::HandleCollapseSyncPromo)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 57 std::string* name = Details<std::string>(details).ptr(); | 60 std::string* name = Details<std::string>(details).ptr(); |
| 58 if (prefs::kGoogleServicesUsername == *name) { | 61 if (prefs::kGoogleServicesUsername == *name) { |
| 59 UpdateLogin(); | 62 UpdateLogin(); |
| 60 return; | 63 return; |
| 61 } | 64 } |
| 62 } | 65 } |
| 63 SyncSetupHandler::Observe(type, source, details); | 66 SyncSetupHandler::Observe(type, source, details); |
| 64 } | 67 } |
| 65 | 68 |
| 66 void NewTabSyncSetupHandler::ShowSetupUI() { | 69 void NewTabSyncSetupHandler::ShowSetupUI() { |
| 67 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); | 70 Profile* profile = |
| 71 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 72 ProfileSyncService* service = profile->GetProfileSyncService(); |
| 68 service->get_wizard().Step(SyncSetupWizard::GetLoginState()); | 73 service->get_wizard().Step(SyncSetupWizard::GetLoginState()); |
| 69 } | 74 } |
| 70 | 75 |
| 71 void NewTabSyncSetupHandler::HandleInitializeSyncPromo(const ListValue* args) { | 76 void NewTabSyncSetupHandler::HandleInitializeSyncPromo(const ListValue* args) { |
| 72 if (!ShouldShowSyncPromo()) | 77 if (!ShouldShowSyncPromo()) |
| 73 return; | 78 return; |
| 74 | 79 |
| 75 // Make sure the sync promo is visible. | 80 // Make sure the sync promo is visible. |
| 76 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.showSyncPromo"); | 81 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.showSyncPromo"); |
| 77 | 82 |
| 78 UpdateLogin(); | 83 UpdateLogin(); |
| 79 | 84 |
| 80 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); | 85 Profile* profile = |
| 86 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 87 ProfileSyncService* service = profile->GetProfileSyncService(); |
| 81 DCHECK(service); | 88 DCHECK(service); |
| 82 | 89 |
| 83 // If the user has not signed into sync then expand the sync promo. | 90 // If the user has not signed into sync then expand the sync promo. |
| 84 // TODO(sail): Need to throttle this behind a server side flag. | 91 // TODO(sail): Need to throttle this behind a server side flag. |
| 85 if (!service->HasSyncSetupCompleted() && | 92 if (!service->HasSyncSetupCompleted() && |
| 86 web_ui_->GetProfile()->GetPrefs()->GetBoolean( | 93 profile->GetPrefs()->GetBoolean(prefs::kSyncPromoExpanded)) { |
| 87 prefs::kSyncPromoExpanded)) { | |
| 88 OpenSyncSetup(); | 94 OpenSyncSetup(); |
| 89 SaveExpandedPreference(true); | 95 SaveExpandedPreference(true); |
| 90 } | 96 } |
| 91 } | 97 } |
| 92 | 98 |
| 93 void NewTabSyncSetupHandler::HandleCollapseSyncPromo(const ListValue* args) { | 99 void NewTabSyncSetupHandler::HandleCollapseSyncPromo(const ListValue* args) { |
| 94 CloseSyncSetup(); | 100 CloseSyncSetup(); |
| 95 SaveExpandedPreference(false); | 101 SaveExpandedPreference(false); |
| 96 } | 102 } |
| 97 | 103 |
| 98 | 104 |
| 99 void NewTabSyncSetupHandler::HandleExpandSyncPromo(const ListValue* args) { | 105 void NewTabSyncSetupHandler::HandleExpandSyncPromo(const ListValue* args) { |
| 100 OpenSyncSetup(); | 106 OpenSyncSetup(); |
| 101 SaveExpandedPreference(true); | 107 SaveExpandedPreference(true); |
| 102 } | 108 } |
| 103 | 109 |
| 104 void NewTabSyncSetupHandler::UpdateLogin() { | 110 void NewTabSyncSetupHandler::UpdateLogin() { |
| 105 std::string username = web_ui_->GetProfile()->GetPrefs()->GetString( | 111 Profile* profile = |
| 112 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 113 std::string username = profile->GetPrefs()->GetString( |
| 106 prefs::kGoogleServicesUsername); | 114 prefs::kGoogleServicesUsername); |
| 107 StringValue string_value(username); | 115 StringValue string_value(username); |
| 108 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.updateLogin", | 116 web_ui_->CallJavascriptFunction("new_tab.NewTabSyncPromo.updateLogin", |
| 109 string_value); | 117 string_value); |
| 110 } | 118 } |
| 111 | 119 |
| 112 void NewTabSyncSetupHandler::SaveExpandedPreference(bool is_expanded) { | 120 void NewTabSyncSetupHandler::SaveExpandedPreference(bool is_expanded) { |
| 113 web_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kSyncPromoExpanded, | 121 Profile* profile = |
| 114 is_expanded); | 122 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 115 web_ui_->GetProfile()->GetPrefs()->ScheduleSavePersistentPrefs(); | 123 profile->GetPrefs()->SetBoolean(prefs::kSyncPromoExpanded, is_expanded); |
| 124 profile->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 116 } | 125 } |
| OLD | NEW |