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