e7695199278f9aec1b1e81678bc7dee64f0fa910
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_infobar_delegate.cc
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_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"
18
19 ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() {
20 }
21
22 // static
23 void ExtensionInfoBarDelegate::Create(content::WebContents* web_contents,
24                                       Browser* browser,
25                                       const extensions::Extension* extension,
26                                       const GURL& url,
27                                       int height) {
28   InfoBarService::FromWebContents(web_contents)->AddInfoBar(
29       ExtensionInfoBarDelegate::CreateInfoBar(
30           scoped_ptr<ExtensionInfoBarDelegate>(new ExtensionInfoBarDelegate(
31               browser, extension, url, web_contents, height))));
32 }
33
34 ExtensionInfoBarDelegate::ExtensionInfoBarDelegate(
35     Browser* browser,
36     const extensions::Extension* extension,
37     const GURL& url,
38     content::WebContents* web_contents,
39     int height)
40     : infobars::InfoBarDelegate(),
41 #if defined(TOOLKIT_VIEWS)
42       browser_(browser),
43 #endif
44       extension_(extension),
45       extension_registry_observer_(this),
46       closing_(false) {
47   extension_view_host_.reset(
48       extensions::ExtensionViewHostFactory::CreateInfobarHost(url, browser));
49   extension_view_host_->SetAssociatedWebContents(web_contents);
50
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()));
55
56   height_ = std::max(0, height);
57   height_ = std::min(2 * infobars::InfoBar::kDefaultBarTargetHeight, height_);
58   if (height_ == 0)
59     height_ = infobars::InfoBar::kDefaultBarTargetHeight;
60 }
61
62 content::WebContents* ExtensionInfoBarDelegate::GetWebContents() {
63   return InfoBarService::WebContentsFromInfoBar(infobar());
64 }
65
66 // ExtensionInfoBarDelegate::CreateInfoBar() is implemented in platform-specific
67 // files.
68
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
77   // we cannot check.
78   if (!extension_delegate)
79     return false;
80
81   // Only allow one InfoBar at a time per extension.
82   return extension_delegate->extension_view_host()->extension() ==
83          extension_view_host_->extension();
84 }
85
86 void ExtensionInfoBarDelegate::InfoBarDismissed() {
87   closing_ = true;
88 }
89
90 infobars::InfoBarDelegate::Type ExtensionInfoBarDelegate::GetInfoBarType()
91     const {
92   return PAGE_ACTION_TYPE;
93 }
94
95 ExtensionInfoBarDelegate*
96     ExtensionInfoBarDelegate::AsExtensionInfoBarDelegate() {
97   return this;
98 }
99
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();
106 }
107
108 void ExtensionInfoBarDelegate::Observe(
109     int type,
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();
116 }