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