OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/views/infobars/extension_infobar.h" | 5 #include "chrome/browser/views/infobars/extension_infobar.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "app/slide_animation.h" | 8 #include "app/slide_animation.h" |
9 #include "chrome/browser/extensions/extension_context_menu_model.h" | 9 #include "chrome/browser/extensions/extension_context_menu_model.h" |
10 #include "chrome/browser/extensions/extension_infobar_delegate.h" | 10 #include "chrome/browser/extensions/extension_infobar_delegate.h" |
11 #include "chrome/browser/extensions/extension_host.h" | 11 #include "chrome/browser/extensions/extension_host.h" |
12 #include "chrome/browser/views/frame/browser_view.h" | 12 #include "chrome/browser/views/frame/browser_view.h" |
13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
14 #include "chrome/common/platform_util.h" | 14 #include "chrome/common/platform_util.h" |
15 #include "gfx/canvas.h" | 15 #include "gfx/canvas.h" |
16 #include "grit/browser_resources.h" | |
17 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
18 #include "views/controls/button/menu_button.h" | 17 #include "views/controls/button/menu_button.h" |
19 #include "views/controls/menu/menu_2.h" | 18 #include "views/controls/menu/menu_2.h" |
20 #include "views/widget/widget.h" | 19 #include "views/widget/widget.h" |
21 | 20 |
22 // The horizontal margin between the menu and the Extension (HTML) view. | 21 // The horizontal margin between the menu and the Extension (HTML) view. |
23 static const int kMenuHorizontalMargin = 1; | 22 static const int kMenuHorizontalMargin = 1; |
24 | 23 |
25 // The amount of space to the right of the Extension (HTML) view (to avoid | 24 // The amount of space to the right of the Extension (HTML) view (to avoid |
26 // overlapping the close button for the InfoBar). | 25 // overlapping the close button for the InfoBar). |
27 static const int kFarRightMargin = 30; | 26 static const int kFarRightMargin = 30; |
28 | 27 |
29 // The margin between the extension icon and the drop-down arrow bitmap. | 28 // The margin between the extension icon and the drop-down arrow bitmap. |
30 static const int kDropArrowLeftMargin = 3; | 29 static const int kDropArrowLeftMargin = 3; |
31 | 30 |
32 ExtensionInfoBar::ExtensionInfoBar(ExtensionInfoBarDelegate* delegate) | 31 ExtensionInfoBar::ExtensionInfoBar(ExtensionInfoBarDelegate* delegate) |
33 : InfoBar(delegate), | 32 : InfoBar(delegate), |
34 delegate_(delegate) { | 33 delegate_(delegate), |
| 34 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
35 ExtensionHost* extension_host = delegate_->extension_host(); | 35 ExtensionHost* extension_host = delegate_->extension_host(); |
36 | 36 |
37 // We set the target height for the InfoBar to be the height of the | 37 // We set the target height for the InfoBar to be the height of the |
38 // ExtensionView it contains (plus 1 because the view should not overlap the | 38 // ExtensionView it contains (plus 1 because the view should not overlap the |
39 // separator line at the bottom). When the InfoBar is first created, however, | 39 // separator line at the bottom). When the InfoBar is first created, however, |
40 // this value is 0 but becomes positive after the InfoBar has been shown. See | 40 // this value is 0 but becomes positive after the InfoBar has been shown. See |
41 // function: OnExtensionPreferredSizeChanged. | 41 // function: OnExtensionPreferredSizeChanged. |
42 gfx::Size sz = extension_host->view()->GetPreferredSize(); | 42 gfx::Size sz = extension_host->view()->GetPreferredSize(); |
43 if (sz.height() > 0) | 43 if (sz.height() > 0) |
44 sz.set_height(sz.height() + 1); | 44 sz.set_height(sz.height() + 1); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 menu_->SetBounds(x, | 84 menu_->SetBounds(x, |
85 (height() - sz.height()) / 2, | 85 (height() - sz.height()) / 2, |
86 sz.width(), sz.height()); | 86 sz.width(), sz.height()); |
87 x += sz.width() + kMenuHorizontalMargin; | 87 x += sz.width() + kMenuHorizontalMargin; |
88 | 88 |
89 // Layout the ExtensionView, showing the HTML InfoBar. | 89 // Layout the ExtensionView, showing the HTML InfoBar. |
90 ExtensionView* view = delegate_->extension_host()->view(); | 90 ExtensionView* view = delegate_->extension_host()->view(); |
91 view->SetBounds(x, 0, width() - x - kFarRightMargin - 1, height() - 1); | 91 view->SetBounds(x, 0, width() - x - kFarRightMargin - 1, height() - 1); |
92 } | 92 } |
93 | 93 |
| 94 void ExtensionInfoBar::OnImageLoaded( |
| 95 SkBitmap* image, ExtensionResource resource, int index) { |
| 96 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 97 |
| 98 // We fall back on the default extension icon on failure. |
| 99 SkBitmap* icon; |
| 100 if (!image || image->empty()) |
| 101 icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION); |
| 102 else |
| 103 icon = image; |
| 104 |
| 105 SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW); |
| 106 |
| 107 int image_size = Extension::EXTENSION_ICON_BITTY; |
| 108 scoped_ptr<gfx::Canvas> canvas( |
| 109 new gfx::Canvas(image_size + kDropArrowLeftMargin + drop_image->width(), |
| 110 image_size, false)); |
| 111 canvas->DrawBitmapInt(*icon, |
| 112 0, 0, icon->width(), icon->height(), |
| 113 0, 0, image_size, image_size, |
| 114 false); |
| 115 canvas->DrawBitmapInt(*drop_image, |
| 116 image_size + kDropArrowLeftMargin, |
| 117 image_size / 2); |
| 118 menu_->SetIcon(canvas->ExtractBitmap()); |
| 119 menu_->SetVisible(true); |
| 120 |
| 121 Layout(); |
| 122 } |
| 123 |
94 void ExtensionInfoBar::RunMenu(View* source, const gfx::Point& pt) { | 124 void ExtensionInfoBar::RunMenu(View* source, const gfx::Point& pt) { |
95 if (!options_menu_contents_.get()) { | 125 if (!options_menu_contents_.get()) { |
96 Browser* browser = BrowserView::GetBrowserViewForNativeWindow( | 126 Browser* browser = BrowserView::GetBrowserViewForNativeWindow( |
97 platform_util::GetTopLevel(source->GetWidget()->GetNativeView()))-> | 127 platform_util::GetTopLevel(source->GetWidget()->GetNativeView()))-> |
98 browser(); | 128 browser(); |
99 options_menu_contents_.reset(new ExtensionContextMenuModel( | 129 options_menu_contents_.reset(new ExtensionContextMenuModel( |
100 delegate_->extension_host()->extension(), browser, NULL)); | 130 delegate_->extension_host()->extension(), browser, NULL)); |
101 } | 131 } |
102 | 132 |
103 options_menu_menu_.reset(new views::Menu2(options_menu_contents_.get())); | 133 options_menu_menu_.reset(new views::Menu2(options_menu_contents_.get())); |
104 options_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPLEFT); | 134 options_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPLEFT); |
105 } | 135 } |
106 | 136 |
107 void ExtensionInfoBar::SetupIconAndMenu() { | 137 void ExtensionInfoBar::SetupIconAndMenu() { |
108 SkBitmap icon; | 138 menu_ = new views::MenuButton(NULL, std::wstring(), this, false); |
| 139 menu_->SetVisible(false); |
| 140 AddChildView(menu_); |
109 | 141 |
110 // TODO(finnur): http://crbug.com/38521. Use cached version of the Extension | |
111 // icon instead of loading it here. | |
112 ExtensionResource icon_resource; | 142 ExtensionResource icon_resource; |
113 Extension::Icons size = delegate_->extension_host()->extension()-> | 143 Extension::Icons size = delegate_->extension_host()->extension()-> |
114 GetIconPathAllowLargerSize(&icon_resource, | 144 GetIconPathAllowLargerSize(&icon_resource, |
115 Extension::EXTENSION_ICON_BITTY); | 145 Extension::EXTENSION_ICON_BITTY); |
116 if (!icon_resource.GetFilePath().empty()) { | 146 if (!icon_resource.relative_path().empty()) { |
117 scoped_ptr<SkBitmap> bitmap; | 147 // Create a tracker to load the image. It will report back on OnImageLoaded. |
118 Extension::DecodeIconFromPath(icon_resource.GetFilePath(), size, &bitmap); | 148 tracker_.LoadImage(icon_resource, gfx::Size(size, size)); |
119 if (bitmap.get()) | 149 } else { |
120 icon = *bitmap.release(); | 150 OnImageLoaded(NULL, icon_resource, 0); // |image|, |index|. |
121 } | 151 } |
122 | |
123 // Failure to get the path or failure to decode it causes fall-back to the | |
124 // default extension icon. | |
125 if (icon.empty()) { | |
126 icon = *ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
127 IDR_EXTENSION_DEFAULT_ICON); | |
128 } | |
129 | |
130 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
131 SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW); | |
132 menu_ = new views::MenuButton(NULL, std::wstring(), this, false); | |
133 | |
134 int image_size = Extension::EXTENSION_ICON_BITTY; | |
135 scoped_ptr<gfx::Canvas> canvas( | |
136 new gfx::Canvas(image_size + kDropArrowLeftMargin + drop_image->width(), | |
137 image_size, false)); | |
138 canvas->DrawBitmapInt(icon, | |
139 0, 0, icon.width(), icon.height(), | |
140 0, 0, image_size, image_size, | |
141 false); | |
142 canvas->DrawBitmapInt(*drop_image, | |
143 image_size + kDropArrowLeftMargin, | |
144 image_size / 2); | |
145 menu_->SetIcon(canvas->ExtractBitmap()); | |
146 | |
147 AddChildView(menu_); | |
148 } | 152 } |
149 | 153 |
150 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar() { | 154 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar() { |
151 return new ExtensionInfoBar(this); | 155 return new ExtensionInfoBar(this); |
152 } | 156 } |
OLD | NEW |