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.
5 #include "chrome/browser/extensions/extension_infobar_delegate.h"
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_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "components/infobars/core/infobar.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_source.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/common/extension.h"
19 ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() {
23 void ExtensionInfoBarDelegate::Create(content::WebContents
* web_contents
,
25 const extensions::Extension
* extension
,
28 InfoBarService::FromWebContents(web_contents
)->AddInfoBar(
29 ExtensionInfoBarDelegate::CreateInfoBar(
30 scoped_ptr
<ExtensionInfoBarDelegate
>(new ExtensionInfoBarDelegate(
31 browser
, extension
, url
, web_contents
, height
))));
34 ExtensionInfoBarDelegate::ExtensionInfoBarDelegate(
36 const extensions::Extension
* extension
,
38 content::WebContents
* web_contents
,
40 : infobars::InfoBarDelegate(),
41 #if defined(TOOLKIT_VIEWS)
44 extension_(extension
),
45 extension_registry_observer_(this),
47 extension_view_host_
.reset(
48 extensions::ExtensionViewHostFactory::CreateInfobarHost(url
, browser
));
49 extension_view_host_
->SetAssociatedWebContents(web_contents
);
51 extension_registry_observer_
.Add(
52 extensions::ExtensionRegistry::Get(browser
->profile()));
53 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
,
54 content::Source
<Profile
>(browser
->profile()));
56 height_
= std::max(0, height
);
57 height_
= std::min(2 * infobars::InfoBar::kDefaultBarTargetHeight
, height_
);
59 height_
= infobars::InfoBar::kDefaultBarTargetHeight
;
62 content::WebContents
* ExtensionInfoBarDelegate::GetWebContents() {
63 return InfoBarService::WebContentsFromInfoBar(infobar());
66 // ExtensionInfoBarDelegate::CreateInfoBar() is implemented in platform-specific
69 bool ExtensionInfoBarDelegate::EqualsDelegate(
70 infobars::InfoBarDelegate
* delegate
) const {
71 ExtensionInfoBarDelegate
* extension_delegate
=
72 delegate
->AsExtensionInfoBarDelegate();
73 // When an extension crashes, an InfoBar is shown (for the crashed extension).
74 // That will result in a call to this function (to see if this InfoBarDelegate
75 // is already showing the 'extension crashed InfoBar', which it never is), but
76 // if it is our extension that crashes, the extension delegate is NULL so
78 if (!extension_delegate
)
81 // Only allow one InfoBar at a time per extension.
82 return extension_delegate
->extension_view_host()->extension() ==
83 extension_view_host_
->extension();
86 void ExtensionInfoBarDelegate::InfoBarDismissed() {
90 infobars::InfoBarDelegate::Type
ExtensionInfoBarDelegate::GetInfoBarType()
92 return PAGE_ACTION_TYPE
;
95 ExtensionInfoBarDelegate
*
96 ExtensionInfoBarDelegate::AsExtensionInfoBarDelegate() {
100 void ExtensionInfoBarDelegate::OnExtensionUnloaded(
101 content::BrowserContext
* browser_context
,
102 const extensions::Extension
* extension
,
103 extensions::UnloadedExtensionInfo::Reason reason
) {
104 if (extension_
== extension
)
105 infobar()->RemoveSelf();
108 void ExtensionInfoBarDelegate::Observe(
110 const content::NotificationSource
& source
,
111 const content::NotificationDetails
& details
) {
112 DCHECK_EQ(type
, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
);
113 if (extension_view_host_
.get() ==
114 content::Details
<extensions::ExtensionHost
>(details
).ptr())
115 infobar()->RemoveSelf();