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/conflicts_ui.h"
      6 
      7 #if defined(OS_WIN)
      8 
      9 #include <string>
     10 
     11 #include "base/bind.h"
     12 #include "base/bind_helpers.h"
     13 #include "base/memory/ref_counted_memory.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "base/strings/utf_string_conversions.h"
     16 #include "base/values.h"
     17 #include "chrome/browser/chrome_notification_types.h"
     18 #include "chrome/browser/enumerate_modules_model_win.h"
     19 #include "chrome/browser/profiles/profile.h"
     20 #include "chrome/common/url_constants.h"
     21 #include "content/public/browser/notification_observer.h"
     22 #include "content/public/browser/notification_registrar.h"
     23 #include "content/public/browser/notification_service.h"
     24 #include "content/public/browser/user_metrics.h"
     25 #include "content/public/browser/web_contents.h"
     26 #include "content/public/browser/web_ui.h"
     27 #include "content/public/browser/web_ui_data_source.h"
     28 #include "content/public/browser/web_ui_message_handler.h"
     29 #include "grit/browser_resources.h"
     30 #include "grit/chromium_strings.h"
     31 #include "grit/generated_resources.h"
     32 #include "grit/theme_resources.h"
     33 #include "ui/base/l10n/l10n_util.h"
     34 #include "ui/base/layout.h"
     35 #include "ui/base/resource/resource_bundle.h"
     36 
     37 using content::UserMetricsAction;
     38 using content::WebContents;
     39 using content::WebUIMessageHandler;
     40 
     41 namespace {
     42 
     43 content::WebUIDataSource* CreateConflictsUIHTMLSource() {
     44   content::WebUIDataSource* source =
     45       content::WebUIDataSource::Create(chrome::kChromeUIConflictsHost);
     46 
     47   source->AddLocalizedString("loadingMessage", IDS_CONFLICTS_LOADING_MESSAGE);
     48   source->AddLocalizedString("modulesLongTitle",
     49                              IDS_CONFLICTS_CHECK_PAGE_TITLE_LONG);
     50   source->AddLocalizedString("modulesBlurb", IDS_CONFLICTS_EXPLANATION_TEXT);
     51   source->AddLocalizedString("moduleSuspectedBad",
     52                              IDS_CONFLICTS_CHECK_WARNING_SUSPECTED);
     53   source->AddLocalizedString("moduleConfirmedBad",
     54                      IDS_CONFLICTS_CHECK_WARNING_CONFIRMED);
     55   source->AddLocalizedString("helpCenterLink", IDS_LEARN_MORE);
     56   source->AddLocalizedString("investigatingText",
     57                              IDS_CONFLICTS_CHECK_INVESTIGATING);
     58   source->AddLocalizedString("modulesNoneLoaded",
     59                              IDS_CONFLICTS_NO_MODULES_LOADED);
     60   source->AddLocalizedString("headerSoftware", IDS_CONFLICTS_HEADER_SOFTWARE);
     61   source->AddLocalizedString("headerSignedBy", IDS_CONFLICTS_HEADER_SIGNED_BY);
     62   source->AddLocalizedString("headerLocation", IDS_CONFLICTS_HEADER_LOCATION);
     63   source->AddLocalizedString("headerVersion", IDS_CONFLICTS_HEADER_VERSION);
     64   source->AddLocalizedString("headerHelpTip", IDS_CONFLICTS_HEADER_HELP_TIP);
     65   source->SetJsonPath("strings.js");
     66   source->AddResourcePath("conflicts.js", IDR_ABOUT_CONFLICTS_JS);
     67   source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML);
     68   return source;
     69 }
     70 
     71 ////////////////////////////////////////////////////////////////////////////////
     72 //
     73 // ConflictsDOMHandler
     74 //
     75 ////////////////////////////////////////////////////////////////////////////////
     76 
     77 // The handler for JavaScript messages for the about:flags page.
     78 class ConflictsDOMHandler : public WebUIMessageHandler,
     79                             public content::NotificationObserver {
     80  public:
     81   ConflictsDOMHandler() {}
     82   virtual ~ConflictsDOMHandler() {}
     83 
     84   // WebUIMessageHandler implementation.
     85   virtual void RegisterMessages();
     86 
     87   // Callback for the "requestModuleList" message.
     88   void HandleRequestModuleList(const ListValue* args);
     89 
     90  private:
     91   void SendModuleList();
     92 
     93   void Observe(int type,
     94                const content::NotificationSource& source,
     95                const content::NotificationDetails& details);
     96 
     97   content::NotificationRegistrar registrar_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler);
    100 };
    101 
    102 void ConflictsDOMHandler::RegisterMessages() {
    103   web_ui()->RegisterMessageCallback("requestModuleList",
    104       base::Bind(&ConflictsDOMHandler::HandleRequestModuleList,
    105                  base::Unretained(this)));
    106 }
    107 
    108 void ConflictsDOMHandler::HandleRequestModuleList(const ListValue* args) {
    109   // This request is handled asynchronously. See Observe for when we reply back.
    110   registrar_.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED,
    111                  content::NotificationService::AllSources());
    112   EnumerateModulesModel::GetInstance()->ScanNow();
    113 }
    114 
    115 void ConflictsDOMHandler::SendModuleList() {
    116   EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance();
    117   ListValue* list = loaded_modules->GetModuleList();
    118   DictionaryValue results;
    119   results.Set("moduleList", list);
    120 
    121   // Add the section title and the total count for bad modules found.
    122   int confirmed_bad = loaded_modules->confirmed_bad_modules_detected();
    123   int suspected_bad = loaded_modules->suspected_bad_modules_detected();
    124   base::string16 table_title;
    125   if (!confirmed_bad && !suspected_bad) {
    126     table_title += l10n_util::GetStringFUTF16(
    127         IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE,
    128             base::IntToString16(list->GetSize()));
    129   } else {
    130     table_title += l10n_util::GetStringFUTF16(
    131         IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO,
    132             base::IntToString16(list->GetSize()),
    133             base::IntToString16(confirmed_bad),
    134             base::IntToString16(suspected_bad));
    135   }
    136   results.SetString("modulesTableTitle", table_title);
    137 
    138   web_ui()->CallJavascriptFunction("returnModuleList", results);
    139 }
    140 
    141 void ConflictsDOMHandler::Observe(int type,
    142                                   const content::NotificationSource& source,
    143                                   const content::NotificationDetails& details) {
    144   switch (type) {
    145     case chrome::NOTIFICATION_MODULE_LIST_ENUMERATED:
    146       SendModuleList();
    147       registrar_.RemoveAll();
    148       break;
    149     default:
    150       NOTREACHED();
    151       break;
    152   }
    153 }
    154 
    155 }  // namespace
    156 
    157 ///////////////////////////////////////////////////////////////////////////////
    158 //
    159 // ConflictsUI
    160 //
    161 ///////////////////////////////////////////////////////////////////////////////
    162 
    163 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
    164   content::RecordAction(UserMetricsAction("ViewAboutConflicts"));
    165   web_ui->AddMessageHandler(new ConflictsDOMHandler());
    166 
    167   // Set up the about:conflicts source.
    168   Profile* profile = Profile::FromWebUI(web_ui);
    169   content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource());
    170 }
    171 
    172 // static
    173 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes(
    174       ui::ScaleFactor scale_factor) {
    175   return static_cast<base::RefCountedMemory*>(
    176       ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
    177           IDR_CONFLICT_FAVICON, scale_factor));
    178 }
    179 
    180 #endif
    181