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.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"
18 ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() {
22 void ExtensionInfoBarDelegate::Create(content::WebContents
* web_contents
,
24 const extensions::Extension
* extension
,
27 InfoBarService::FromWebContents(web_contents
)->AddInfoBar(
28 ExtensionInfoBarDelegate::CreateInfoBar(
29 scoped_ptr
<ExtensionInfoBarDelegate
>(new ExtensionInfoBarDelegate(
30 browser
, extension
, url
, web_contents
, height
))));
33 ExtensionInfoBarDelegate::ExtensionInfoBarDelegate(
35 const extensions::Extension
* extension
,
37 content::WebContents
* web_contents
,
40 #if defined(TOOLKIT_VIEWS)
43 extension_(extension
),
45 extension_view_host_
.reset(
46 extensions::ExtensionViewHostFactory::CreateInfobarHost(url
, browser
));
47 extension_view_host_
->SetAssociatedWebContents(web_contents
);
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()));
54 height_
= std::max(0, height
);
55 height_
= std::min(2 * InfoBar::kDefaultBarTargetHeight
, height_
);
57 height_
= InfoBar::kDefaultBarTargetHeight
;
60 // ExtensionInfoBarDelegate::CreateInfoBar() is implemented in platform-specific
63 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
71 if (!extension_delegate
)
74 // Only allow one InfoBar at a time per extension.
75 return extension_delegate
->extension_view_host()->extension() ==
76 extension_view_host_
->extension();
79 void ExtensionInfoBarDelegate::InfoBarDismissed() {
83 InfoBarDelegate::Type
ExtensionInfoBarDelegate::GetInfoBarType() const {
84 return PAGE_ACTION_TYPE
;
87 ExtensionInfoBarDelegate
*
88 ExtensionInfoBarDelegate::AsExtensionInfoBarDelegate() {
92 void ExtensionInfoBarDelegate::Observe(
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();
101 DCHECK(type
== chrome::NOTIFICATION_EXTENSION_UNLOADED
);
102 if (extension_
== content::Details
<extensions::UnloadedExtensionInfo
>(
104 infobar()->RemoveSelf();