1// Copyright (c) 2012 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/signin_internals_ui.h"
6
7#include "base/hash.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/signin/about_signin_internals_factory.h"
10#include "chrome/common/url_constants.h"
11#include "components/signin/core/browser/about_signin_internals.h"
12#include "content/public/browser/web_ui.h"
13#include "content/public/browser/web_ui_data_source.h"
14#include "grit/signin_internals_resources.h"
15
16namespace {
17
18content::WebUIDataSource* CreateSignInInternalsHTMLSource() {
19  content::WebUIDataSource* source =
20      content::WebUIDataSource::Create(chrome::kChromeUISignInInternalsHost);
21
22  source->SetUseJsonJSFormatV2();
23  source->SetJsonPath("strings.js");
24  source->AddResourcePath("signin_internals.js", IDR_SIGNIN_INTERNALS_INDEX_JS);
25  source->SetDefaultResource(IDR_SIGNIN_INTERNALS_INDEX_HTML);
26  return source;
27}
28
29} //  namespace
30
31SignInInternalsUI::SignInInternalsUI(content::WebUI* web_ui)
32    : WebUIController(web_ui) {
33  Profile* profile = Profile::FromWebUI(web_ui);
34  content::WebUIDataSource::Add(profile, CreateSignInInternalsHTMLSource());
35  if (profile) {
36    AboutSigninInternals* about_signin_internals =
37        AboutSigninInternalsFactory::GetForProfile(profile);
38    if (about_signin_internals)
39      about_signin_internals->AddSigninObserver(this);
40  }
41}
42
43SignInInternalsUI::~SignInInternalsUI() {
44  Profile* profile = Profile::FromWebUI(web_ui());
45  if (profile) {
46    AboutSigninInternals* about_signin_internals =
47        AboutSigninInternalsFactory::GetForProfile(profile);
48    if (about_signin_internals) {
49      about_signin_internals->RemoveSigninObserver(this);
50    }
51  }
52}
53
54bool SignInInternalsUI::OverrideHandleWebUIMessage(
55    const GURL& source_url,
56    const std::string& name,
57    const base::ListValue& content) {
58  if (name == "getSigninInfo") {
59    Profile* profile = Profile::FromWebUI(web_ui());
60    if (!profile)
61      return false;
62
63    AboutSigninInternals* about_signin_internals =
64        AboutSigninInternalsFactory::GetForProfile(profile);
65    // TODO(vishwath): The UI would look better if we passed in a dict with some
66    // reasonable defaults, so the about:signin-internals page doesn't look
67    // empty in incognito mode. Alternatively, we could force about:signin to
68    // open in non-incognito mode always (like about:settings for ex.).
69    if (about_signin_internals) {
70      web_ui()->CallJavascriptFunction(
71          "chrome.signin.getSigninInfo.handleReply",
72          *about_signin_internals->GetSigninStatus());
73      about_signin_internals->GetCookieAccountsAsync();
74
75      return true;
76    }
77  }
78  return false;
79}
80
81void SignInInternalsUI::OnSigninStateChanged(
82    const base::DictionaryValue* info) {
83  web_ui()->CallJavascriptFunction(
84      "chrome.signin.onSigninInfoChanged.fire", *info);
85}
86
87void SignInInternalsUI::OnCookieAccountsFetched(
88    const base::DictionaryValue* info) {
89  web_ui()->CallJavascriptFunction(
90      "chrome.signin.onCookieAccountsFetched.fire", *info);
91}
92