Home | History | Annotate | Download | only in download
      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/download/download_request_infobar_delegate.h"
      6 
      7 #include "chrome/browser/infobars/infobar_service.h"
      8 #include "grit/generated_resources.h"
      9 #include "grit/theme_resources.h"
     10 #include "ui/base/l10n/l10n_util.h"
     11 
     12 DownloadRequestInfoBarDelegate::FakeCreateCallback*
     13   DownloadRequestInfoBarDelegate::callback_ = NULL;
     14 
     15 DownloadRequestInfoBarDelegate::~DownloadRequestInfoBarDelegate() {
     16   if (!responded_ && host_)
     17     host_->CancelOnce();
     18 }
     19 
     20 // static
     21 void DownloadRequestInfoBarDelegate::Create(
     22     InfoBarService* infobar_service,
     23     base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) {
     24   if (DownloadRequestInfoBarDelegate::callback_ &&
     25       !DownloadRequestInfoBarDelegate::callback_->is_null()) {
     26     DownloadRequestInfoBarDelegate::callback_->Run(infobar_service, host);
     27   } else if (!infobar_service) {
     28     // |web_contents| may not have a InfoBarService if it's actually a
     29     // WebContents like those used for extension popups/bubbles and hosted apps
     30     // etc.
     31     // TODO(benjhayden): If this is an automatic download from an extension,
     32     // it would be convenient for the extension author if we send a message to
     33     // the extension's DevTools console (as we do for CSP) about how
     34     // extensions should use chrome.downloads.download() (requires the
     35     // "downloads" permission) to automatically download >1 files.
     36     host->Cancel();
     37   } else {
     38     infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>(
     39         new DownloadRequestInfoBarDelegate(infobar_service, host)));
     40   }
     41 }
     42 
     43 // static
     44 void DownloadRequestInfoBarDelegate::SetCallbackForTesting(
     45     FakeCreateCallback* callback) {
     46   DownloadRequestInfoBarDelegate::callback_ = callback;
     47 }
     48 #
     49 DownloadRequestInfoBarDelegate::DownloadRequestInfoBarDelegate(
     50     InfoBarService* infobar_service,
     51     base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host)
     52     : ConfirmInfoBarDelegate(infobar_service),
     53       responded_(false),
     54       host_(host) {
     55 }
     56 
     57 int DownloadRequestInfoBarDelegate::GetIconID() const {
     58   return IDR_INFOBAR_MULTIPLE_DOWNLOADS;
     59 }
     60 
     61 string16 DownloadRequestInfoBarDelegate::GetMessageText() const {
     62   return l10n_util::GetStringUTF16(IDS_MULTI_DOWNLOAD_WARNING);
     63 }
     64 
     65 string16 DownloadRequestInfoBarDelegate::GetButtonLabel(
     66     InfoBarButton button) const {
     67   return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
     68       IDS_MULTI_DOWNLOAD_WARNING_ALLOW : IDS_MULTI_DOWNLOAD_WARNING_DENY);
     69 }
     70 
     71 bool DownloadRequestInfoBarDelegate::Accept() {
     72   DCHECK(!responded_);
     73   responded_ = true;
     74   if (host_) {
     75     // This may invalidate |host_|.
     76     host_->Accept();
     77   }
     78   return !host_;
     79 }
     80 
     81 bool DownloadRequestInfoBarDelegate::Cancel() {
     82   DCHECK(!responded_);
     83   responded_ = true;
     84   if (host_) {
     85     // This may invalidate |host_|.
     86     host_->Cancel();
     87   }
     88   return !host_;
     89 }
     90