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 base::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->SetUseJsonJSFormatV2();
     48   source->AddLocalizedString("loadingMessage", IDS_CONFLICTS_LOADING_MESSAGE);
     49   source->AddLocalizedString("modulesLongTitle",
     50                              IDS_CONFLICTS_CHECK_PAGE_TITLE_LONG);
     51   source->AddLocalizedString("modulesBlurb", IDS_CONFLICTS_EXPLANATION_TEXT);
     52   source->AddLocalizedString("moduleSuspectedBad",
     53                              IDS_CONFLICTS_CHECK_WARNING_SUSPECTED);
     54   source->AddLocalizedString("moduleConfirmedBad",
     55                      IDS_CONFLICTS_CHECK_WARNING_CONFIRMED);
     56   source->AddLocalizedString("helpCenterLink", IDS_LEARN_MORE);
     57   source->AddLocalizedString("investigatingText",
     58                              IDS_CONFLICTS_CHECK_INVESTIGATING);
     59   source->AddLocalizedString("modulesNoneLoaded",
     60                              IDS_CONFLICTS_NO_MODULES_LOADED);
     61   source->AddLocalizedString("headerSoftware", IDS_CONFLICTS_HEADER_SOFTWARE);
     62   source->AddLocalizedString("headerSignedBy", IDS_CONFLICTS_HEADER_SIGNED_BY);
     63   source->AddLocalizedString("headerLocation", IDS_CONFLICTS_HEADER_LOCATION);
     64   source->AddLocalizedString("headerVersion", IDS_CONFLICTS_HEADER_VERSION);
     65   source->AddLocalizedString("headerHelpTip", IDS_CONFLICTS_HEADER_HELP_TIP);
     66   source->SetJsonPath("strings.js");
     67   source->AddResourcePath("conflicts.js", IDR_ABOUT_CONFLICTS_JS);
     68   source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML);
     69   return source;
     70 }
     71 
     72 ////////////////////////////////////////////////////////////////////////////////
     73 //
     74 // ConflictsDOMHandler
     75 //
     76 ////////////////////////////////////////////////////////////////////////////////
     77 
     78 // The handler for JavaScript messages for the about:flags page.
     79 class ConflictsDOMHandler : public WebUIMessageHandler,
     80                             public content::NotificationObserver {
     81  public:
     82   ConflictsDOMHandler() {}
     83   virtual ~ConflictsDOMHandler() {}
     84 
     85   // WebUIMessageHandler implementation.
     86   virtual void RegisterMessages();
     87 
     88   // Callback for the "requestModuleList" message.
     89   void HandleRequestModuleList(const base::ListValue* args);
     90 
     91  private:
     92   void SendModuleList();
     93 
     94   void Observe(int type,
     95                const content::NotificationSource& source,
     96                const content::NotificationDetails& details);
     97 
     98   content::NotificationRegistrar registrar_;
     99 
    100   DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler);
    101 };
    102 
    103 void ConflictsDOMHandler::RegisterMessages() {
    104   web_ui()->RegisterMessageCallback("requestModuleList",
    105       base::Bind(&ConflictsDOMHandler::HandleRequestModuleList,
    106                  base::Unretained(this)));
    107 }
    108 
    109 void ConflictsDOMHandler::HandleRequestModuleList(const base::ListValue* args) {
    110   // This request is handled asynchronously. See Observe for when we reply back.
    111   registrar_.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED,
    112                  content::NotificationService::AllSources());
    113   EnumerateModulesModel::GetInstance()->ScanNow();
    114 }
    115 
    116 void ConflictsDOMHandler::SendModuleList() {
    117   EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance();
    118   base::ListValue* list = loaded_modules->GetModuleList();
    119   base::DictionaryValue results;
    120   results.Set("moduleList", list);
    121 
    122   // Add the section title and the total count for bad modules found.
    123   int confirmed_bad = loaded_modules->confirmed_bad_modules_detected();
    124   int suspected_bad = loaded_modules->suspected_bad_modules_detected();
    125   base::string16 table_title;
    126   if (!confirmed_bad && !suspected_bad) {
    127     table_title += l10n_util::GetStringFUTF16(
    128         IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE,
    129             base::IntToString16(list->GetSize()));
    130   } else {
    131     table_title += l10n_util::GetStringFUTF16(
    132         IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO,
    133             base::IntToString16(list->GetSize()),
    134             base::IntToString16(confirmed_bad),
    135             base::IntToString16(suspected_bad));
    136   }
    137   results.SetString("modulesTableTitle", table_title);
    138 
    139   web_ui()->CallJavascriptFunction("returnModuleList", results);
    140 }
    141 
    142 void ConflictsDOMHandler::Observe(int type,
    143                                   const content::NotificationSource& source,
    144                                   const content::NotificationDetails& details) {
    145   switch (type) {
    146     case chrome::NOTIFICATION_MODULE_LIST_ENUMERATED:
    147       SendModuleList();
    148       registrar_.RemoveAll();
    149       break;
    150     default:
    151       NOTREACHED();
    152       break;
    153   }
    154 }
    155 
    156 }  // namespace
    157 
    158 ///////////////////////////////////////////////////////////////////////////////
    159 //
    160 // ConflictsUI
    161 //
    162 ///////////////////////////////////////////////////////////////////////////////
    163 
    164 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
    165   content::RecordAction(UserMetricsAction("ViewAboutConflicts"));
    166   web_ui->AddMessageHandler(new ConflictsDOMHandler());
    167 
    168   // Set up the about:conflicts source.
    169   Profile* profile = Profile::FromWebUI(web_ui);
    170   content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource());
    171 }
    172 
    173 // static
    174 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes(
    175       ui::ScaleFactor scale_factor) {
    176   return static_cast<base::RefCountedMemory*>(
    177       ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
    178           IDR_CONFLICT_FAVICON, scale_factor));
    179 }
    180 
    181 #endif
    182