• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • 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/extensions/extension_infobar_delegate.h"
6 
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_view_host.h"
9 #include "chrome/browser/extensions/extension_view_host_factory.h"
10 #include "chrome/browser/infobars/infobar.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_source.h"
16 #include "extensions/common/extension.h"
17 
~ExtensionInfoBarDelegate()18 ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() {
19 }
20 
21 // static
Create(content::WebContents * web_contents,Browser * browser,const extensions::Extension * extension,const GURL & url,int height)22 void ExtensionInfoBarDelegate::Create(content::WebContents* web_contents,
23                                       Browser* browser,
24                                       const extensions::Extension* extension,
25                                       const GURL& url,
26                                       int height) {
27   InfoBarService::FromWebContents(web_contents)->AddInfoBar(
28       ExtensionInfoBarDelegate::CreateInfoBar(
29           scoped_ptr<ExtensionInfoBarDelegate>(new ExtensionInfoBarDelegate(
30               browser, extension, url, web_contents, height))));
31 }
32 
ExtensionInfoBarDelegate(Browser * browser,const extensions::Extension * extension,const GURL & url,content::WebContents * web_contents,int height)33 ExtensionInfoBarDelegate::ExtensionInfoBarDelegate(
34     Browser* browser,
35     const extensions::Extension* extension,
36     const GURL& url,
37     content::WebContents* web_contents,
38     int height)
39     : InfoBarDelegate(),
40 #if defined(TOOLKIT_VIEWS)
41       browser_(browser),
42 #endif
43       extension_(extension),
44       closing_(false) {
45   extension_view_host_.reset(
46       extensions::ExtensionViewHostFactory::CreateInfobarHost(url, browser));
47   extension_view_host_->SetAssociatedWebContents(web_contents);
48 
49   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
50                  content::Source<Profile>(browser->profile()));
51   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
52                  content::Source<Profile>(browser->profile()));
53 
54   height_ = std::max(0, height);
55   height_ = std::min(2 * InfoBar::kDefaultBarTargetHeight, height_);
56   if (height_ == 0)
57     height_ = InfoBar::kDefaultBarTargetHeight;
58 }
59 
60 // ExtensionInfoBarDelegate::CreateInfoBar() is implemented in platform-specific
61 // files.
62 
EqualsDelegate(InfoBarDelegate * delegate) const63 bool ExtensionInfoBarDelegate::EqualsDelegate(InfoBarDelegate* delegate) const {
64   ExtensionInfoBarDelegate* extension_delegate =
65       delegate->AsExtensionInfoBarDelegate();
66   // When an extension crashes, an InfoBar is shown (for the crashed extension).
67   // That will result in a call to this function (to see if this InfoBarDelegate
68   // is already showing the 'extension crashed InfoBar', which it never is), but
69   // if it is our extension that crashes, the extension delegate is NULL so
70   // we cannot check.
71   if (!extension_delegate)
72     return false;
73 
74   // Only allow one InfoBar at a time per extension.
75   return extension_delegate->extension_view_host()->extension() ==
76          extension_view_host_->extension();
77 }
78 
InfoBarDismissed()79 void ExtensionInfoBarDelegate::InfoBarDismissed() {
80   closing_ = true;
81 }
82 
GetInfoBarType() const83 InfoBarDelegate::Type ExtensionInfoBarDelegate::GetInfoBarType() const {
84   return PAGE_ACTION_TYPE;
85 }
86 
87 ExtensionInfoBarDelegate*
AsExtensionInfoBarDelegate()88     ExtensionInfoBarDelegate::AsExtensionInfoBarDelegate() {
89   return this;
90 }
91 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)92 void ExtensionInfoBarDelegate::Observe(
93     int type,
94     const content::NotificationSource& source,
95     const content::NotificationDetails& details) {
96   if (type == chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE) {
97     if (extension_view_host_.get() ==
98         content::Details<extensions::ExtensionHost>(details).ptr())
99       infobar()->RemoveSelf();
100   } else {
101     DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED);
102     if (extension_ == content::Details<extensions::UnloadedExtensionInfo>(
103         details)->extension)
104       infobar()->RemoveSelf();
105   }
106 }
107