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