| 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/ntp_login_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/prefs/pref_notifier.h" | 10 #include "chrome/browser/prefs/pref_notifier.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sync/profile_sync_service.h" | 13 #include "chrome/browser/sync/profile_sync_service.h" |
| 14 #include "chrome/browser/sync/sync_setup_flow.h" | 14 #include "chrome/browser/sync/sync_setup_flow.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" |
| 17 #include "content/common/notification_details.h" | 18 #include "content/common/notification_details.h" |
| 18 | 19 |
| 19 NTPLoginHandler::NTPLoginHandler() { | 20 NTPLoginHandler::NTPLoginHandler() { |
| 20 } | 21 } |
| 21 | 22 |
| 22 NTPLoginHandler::~NTPLoginHandler() { | 23 NTPLoginHandler::~NTPLoginHandler() { |
| 23 } | 24 } |
| 24 | 25 |
| 25 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { | 26 WebUIMessageHandler* NTPLoginHandler::Attach(WebUI* web_ui) { |
| 26 PrefService* pref_service = web_ui->GetProfile()->GetPrefs(); | 27 Profile* profile = |
| 28 Profile::FromBrowserContext(web_ui->tab_contents()->browser_context()); |
| 29 PrefService* pref_service = profile->GetPrefs(); |
| 27 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); | 30 username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this); |
| 28 | 31 |
| 29 return WebUIMessageHandler::Attach(web_ui); | 32 return WebUIMessageHandler::Attach(web_ui); |
| 30 } | 33 } |
| 31 | 34 |
| 32 void NTPLoginHandler::RegisterMessages() { | 35 void NTPLoginHandler::RegisterMessages() { |
| 33 web_ui_->RegisterMessageCallback("initializeLogin", | 36 web_ui_->RegisterMessageCallback("initializeLogin", |
| 34 NewCallback(this, &NTPLoginHandler::HandleInitializeLogin)); | 37 NewCallback(this, &NTPLoginHandler::HandleInitializeLogin)); |
| 35 } | 38 } |
| 36 | 39 |
| 37 void NTPLoginHandler::Observe(int type, | 40 void NTPLoginHandler::Observe(int type, |
| 38 const NotificationSource& source, | 41 const NotificationSource& source, |
| 39 const NotificationDetails& details) { | 42 const NotificationDetails& details) { |
| 40 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | 43 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); |
| 41 std::string* name = Details<std::string>(details).ptr(); | 44 std::string* name = Details<std::string>(details).ptr(); |
| 42 if (prefs::kGoogleServicesUsername == *name) | 45 if (prefs::kGoogleServicesUsername == *name) |
| 43 UpdateLogin(); | 46 UpdateLogin(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 void NTPLoginHandler::HandleInitializeLogin(const ListValue* args) { | 49 void NTPLoginHandler::HandleInitializeLogin(const ListValue* args) { |
| 47 UpdateLogin(); | 50 UpdateLogin(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 void NTPLoginHandler::UpdateLogin() { | 53 void NTPLoginHandler::UpdateLogin() { |
| 51 std::string username = web_ui_->GetProfile()->GetPrefs()->GetString( | 54 Profile* profile = |
| 55 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 56 std::string username = profile->GetPrefs()->GetString( |
| 52 prefs::kGoogleServicesUsername); | 57 prefs::kGoogleServicesUsername); |
| 53 StringValue string_value(username); | 58 StringValue string_value(username); |
| 54 web_ui_->CallJavascriptFunction("updateLogin", string_value); | 59 web_ui_->CallJavascriptFunction("updateLogin", string_value); |
| 55 } | 60 } |
| OLD | NEW |