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