Home | History | Annotate | Download | only in extensions
      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/extensions/extension_error_ui.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string16.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "chrome/browser/extensions/extension_service.h"
     11 #include "chrome/browser/ui/global_error/global_error.h"
     12 #include "grit/chromium_strings.h"
     13 #include "grit/generated_resources.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 
     16 using extensions::ExtensionIdSet;
     17 
     18 ExtensionErrorUI::ExtensionErrorUI(ExtensionService* extension_service)
     19     : extension_service_(extension_service),
     20       external_extension_ids_(new ExtensionIdSet),
     21       blacklisted_extension_ids_(new ExtensionIdSet) {
     22   DCHECK(extension_service_);
     23 }
     24 
     25 ExtensionErrorUI::~ExtensionErrorUI() {
     26 }
     27 
     28 void ExtensionErrorUI::AddExternalExtension(const std::string& id) {
     29   external_extension_ids_->insert(id);
     30 }
     31 
     32 void ExtensionErrorUI::AddBlacklistedExtension(const std::string& id) {
     33   blacklisted_extension_ids_->insert(id);
     34 }
     35 
     36 base::string16 ExtensionErrorUI::GenerateMessageSection(
     37     const ExtensionIdSet* extensions,
     38     int extension_template_message_id,
     39     int app_template_message_id) {
     40   CHECK(extensions);
     41   CHECK(extension_template_message_id);
     42   CHECK(app_template_message_id);
     43   base::string16 message;
     44 
     45   for (ExtensionIdSet::const_iterator iter = extensions->begin();
     46        iter != extensions->end(); ++iter) {
     47     const extensions::Extension* e =
     48         extension_service_->GetInstalledExtension(*iter);
     49     message += l10n_util::GetStringFUTF16(
     50         e->is_app() ? app_template_message_id : extension_template_message_id,
     51         UTF8ToUTF16(e->name())) + char16('\n');
     52   }
     53   return message;
     54 }
     55 
     56 base::string16 ExtensionErrorUI::GenerateMessage() {
     57   return GenerateMessageSection(external_extension_ids_.get(),
     58                                 IDS_EXTENSION_ALERT_ITEM_EXTERNAL,
     59                                 IDS_APP_ALERT_ITEM_EXTERNAL) +
     60          GenerateMessageSection(blacklisted_extension_ids_.get(),
     61                                 IDS_EXTENSION_ALERT_ITEM_BLACKLISTED,
     62                                 IDS_APP_ALERT_ITEM_BLACKLISTED);
     63 }
     64 
     65 std::vector<base::string16> ExtensionErrorUI::GetBubbleViewMessages() {
     66   if (message_.empty()) {
     67     message_ = GenerateMessage();
     68     if (message_[message_.size()-1] == '\n')
     69       message_.resize(message_.size()-1);
     70   }
     71   return std::vector<base::string16>(1, message_);
     72 }
     73 
     74 base::string16 ExtensionErrorUI::GetBubbleViewTitle() {
     75   return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_TITLE);
     76 }
     77 
     78 base::string16 ExtensionErrorUI::GetBubbleViewAcceptButtonLabel() {
     79   return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_ITEM_OK);
     80 }
     81 
     82 base::string16 ExtensionErrorUI::GetBubbleViewCancelButtonLabel() {
     83   return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_ITEM_DETAILS);
     84 }
     85 
     86 void ExtensionErrorUI::BubbleViewDidClose() {
     87   // This call deletes ExtensionErrorUI object referenced by this.
     88   extension_service_->HandleExtensionAlertClosed();
     89 }
     90 
     91 void ExtensionErrorUI::BubbleViewAcceptButtonPressed() {
     92   extension_service_->HandleExtensionAlertAccept();
     93 }
     94 
     95 void ExtensionErrorUI::BubbleViewCancelButtonPressed() {
     96   extension_service_->HandleExtensionAlertDetails();
     97 }
     98