Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / zoom_bubble_view.cc
blob9308b6dc24fc9b7f18f8bccc2e623e6401425a37
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/ui/views/location_bar/zoom_bubble_view.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_tabstrip.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
16 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
17 #include "chrome/common/extensions/api/extension_action/action_info.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/ui/zoom/page_zoom.h"
20 #include "components/ui/zoom/zoom_controller.h"
21 #include "content/public/browser/notification_source.h"
22 #include "extensions/browser/extension_zoom_request_client.h"
23 #include "extensions/common/manifest_handlers/icons_handler.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/favicon_size.h"
28 #include "ui/views/controls/button/image_button.h"
29 #include "ui/views/controls/button/label_button.h"
30 #include "ui/views/controls/separator.h"
31 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/layout/layout_constants.h"
33 #include "ui/views/widget/widget.h"
35 // static
36 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL;
38 // static
39 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
40 bool auto_close) {
41 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
42 DCHECK(browser && browser->window() &&
43 browser->exclusive_access_manager()->fullscreen_controller());
45 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
46 bool is_fullscreen = browser_view->IsFullscreen();
47 bool anchor_to_view = !is_fullscreen ||
48 browser_view->immersive_mode_controller()->IsRevealed();
49 views::View* anchor_view = anchor_to_view ?
50 browser_view->GetLocationBarView()->zoom_view() : NULL;
52 // Find the extension that initiated the zoom change, if any.
53 ui_zoom::ZoomController* zoom_controller =
54 ui_zoom::ZoomController::FromWebContents(web_contents);
55 const ui_zoom::ZoomRequestClient* client = zoom_controller->last_client();
57 // If the bubble is already showing in this window and the zoom change was not
58 // initiated by an extension, then the bubble can be reused and only the label
59 // text needs to be updated.
60 if (zoom_bubble_ && zoom_bubble_->GetAnchorView() == anchor_view && !client) {
61 DCHECK_EQ(web_contents, zoom_bubble_->web_contents_);
62 zoom_bubble_->Refresh();
63 return;
66 // If the bubble is already showing but in a different tab, the current
67 // bubble must be closed and a new one created.
68 CloseBubble();
70 zoom_bubble_ = new ZoomBubbleView(anchor_view, web_contents, auto_close,
71 browser_view->immersive_mode_controller());
73 // If the zoom change was initiated by an extension, capture the relevent
74 // information from it.
75 if (client) {
76 zoom_bubble_->SetExtensionInfo(
77 static_cast<const extensions::ExtensionZoomRequestClient*>(client)
78 ->extension());
81 // If we do not have an anchor view, parent the bubble to the content area.
82 if (!anchor_to_view)
83 zoom_bubble_->set_parent_window(web_contents->GetNativeView());
85 views::BubbleDelegateView::CreateBubble(zoom_bubble_);
87 // Adjust for fullscreen after creation as it relies on the content size.
88 if (is_fullscreen)
89 zoom_bubble_->AdjustForFullscreen(browser_view->GetBoundsInScreen());
91 if (auto_close)
92 zoom_bubble_->GetWidget()->ShowInactive();
93 else
94 zoom_bubble_->GetWidget()->Show();
97 // static
98 void ZoomBubbleView::CloseBubble() {
99 if (zoom_bubble_)
100 zoom_bubble_->Close();
103 // static
104 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
105 return zoom_bubble_;
108 ZoomBubbleView::ZoomBubbleView(
109 views::View* anchor_view,
110 content::WebContents* web_contents,
111 bool auto_close,
112 ImmersiveModeController* immersive_mode_controller)
113 : ManagedFullScreenBubbleDelegateView(anchor_view, web_contents),
114 image_button_(NULL),
115 label_(NULL),
116 web_contents_(web_contents),
117 auto_close_(auto_close),
118 immersive_mode_controller_(immersive_mode_controller) {
119 // Compensate for built-in vertical padding in the anchor view's image.
120 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
121 set_notify_enter_exit_on_child(true);
122 immersive_mode_controller_->AddObserver(this);
125 ZoomBubbleView::~ZoomBubbleView() {
126 if (immersive_mode_controller_)
127 immersive_mode_controller_->RemoveObserver(this);
130 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
131 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
132 event->type() != ui::ET_GESTURE_TAP) {
133 return;
136 auto_close_ = false;
137 StopTimer();
138 event->SetHandled();
141 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
142 StopTimer();
145 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
146 StartTimerIfNecessary();
149 void ZoomBubbleView::Init() {
150 // Set up the layout of the zoom bubble. A grid layout is used because
151 // sometimes an extension icon is shown next to the zoom label.
152 views::GridLayout* grid_layout = new views::GridLayout(this);
153 SetLayoutManager(grid_layout);
154 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
155 // First row.
156 if (extension_info_.icon_image) {
157 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2,
158 views::GridLayout::USE_PREF, 0, 0);
160 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
161 views::GridLayout::USE_PREF, 0, 0);
162 grid_layout->StartRow(0, 0);
164 // If this zoom change was initiated by an extension, that extension will be
165 // attributed by showing its icon in the zoom bubble.
166 if (extension_info_.icon_image) {
167 image_button_ = new views::ImageButton(this);
168 image_button_->SetTooltipText(
169 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
170 base::UTF8ToUTF16(extension_info_.name)));
171 image_button_->SetImage(views::Button::STATE_NORMAL,
172 &extension_info_.icon_image->image_skia());
173 grid_layout->AddView(image_button_);
176 // Add zoom label with the new zoom percent.
177 ui_zoom::ZoomController* zoom_controller =
178 ui_zoom::ZoomController::FromWebContents(web_contents_);
179 int zoom_percent = zoom_controller->GetZoomPercent();
180 label_ = new views::Label(
181 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
182 label_->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
183 ui::ResourceBundle::MediumFont));
184 grid_layout->AddView(label_);
186 // Second row.
187 grid_layout->AddPaddingRow(0, 8);
188 columns = grid_layout->AddColumnSet(1);
189 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
190 views::GridLayout::USE_PREF, 0, 0);
191 grid_layout->StartRow(0, 1);
193 // Add "Reset to Default" button.
194 views::LabelButton* set_default_button = new views::LabelButton(
195 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
196 set_default_button->SetStyle(views::Button::STYLE_BUTTON);
197 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
198 grid_layout->AddView(set_default_button);
200 StartTimerIfNecessary();
203 void ZoomBubbleView::WindowClosing() {
204 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
205 // call this right away). Only set to NULL when it's this bubble.
206 if (zoom_bubble_ == this)
207 zoom_bubble_ = NULL;
210 void ZoomBubbleView::Close() {
211 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
212 // this. Additionally web_contents_ may have been destroyed.
213 zoom_bubble_ = NULL;
214 web_contents_ = NULL;
215 ManagedFullScreenBubbleDelegateView::Close();
218 void ZoomBubbleView::ButtonPressed(views::Button* sender,
219 const ui::Event& event) {
220 if (sender == image_button_) {
221 DCHECK(extension_info_.icon_image) << "Invalid button press.";
222 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
223 chrome::AddSelectedTabWithURL(
224 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
225 extension_info_.id.c_str())),
226 ui::PAGE_TRANSITION_FROM_API);
227 } else {
228 ui_zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
232 void ZoomBubbleView::OnImmersiveRevealStarted() {
233 CloseBubble();
236 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
237 immersive_mode_controller_ = NULL;
240 void ZoomBubbleView::OnExtensionIconImageChanged(
241 extensions::IconImage* /* image */) {
242 image_button_->SetImage(views::Button::STATE_NORMAL,
243 &extension_info_.icon_image->image_skia());
244 image_button_->SchedulePaint();
247 void ZoomBubbleView::Refresh() {
248 ui_zoom::ZoomController* zoom_controller =
249 ui_zoom::ZoomController::FromWebContents(web_contents_);
250 int zoom_percent = zoom_controller->GetZoomPercent();
251 label_->SetText(
252 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
253 StartTimerIfNecessary();
256 void ZoomBubbleView::SetExtensionInfo(const extensions::Extension* extension) {
257 DCHECK(extension);
258 extension_info_.id = extension->id();
259 extension_info_.name = extension->name();
261 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
262 const gfx::ImageSkia& default_extension_icon_image =
263 *rb.GetImageSkiaNamed(IDR_EXTENSIONS_FAVICON);
264 int icon_size = gfx::kFaviconSize;
266 // We give first preference to an icon from the extension's icon set that
267 // matches the size of the default. But not all extensions will declare an
268 // icon set, or may not have an icon of the default size (we don't want the
269 // bubble to display, for example, a very large icon). In that case, if there
270 // is a browser-action icon (size-19) this is an acceptable alternative.
271 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension);
272 bool has_default_sized_icon =
273 !icons.Get(gfx::kFaviconSize, ExtensionIconSet::MATCH_EXACTLY).empty();
274 if (has_default_sized_icon) {
275 extension_info_.icon_image.reset(
276 new extensions::IconImage(web_contents_->GetBrowserContext(),
277 extension,
278 icons,
279 icon_size,
280 default_extension_icon_image,
281 this));
282 return;
285 const extensions::ActionInfo* browser_action =
286 extensions::ActionInfo::GetBrowserActionInfo(extension);
287 if (!browser_action || browser_action->default_icon.empty())
288 return;
290 icon_size = browser_action->default_icon.map().begin()->first;
291 extension_info_.icon_image.reset(
292 new extensions::IconImage(web_contents_->GetBrowserContext(),
293 extension,
294 browser_action->default_icon,
295 icon_size,
296 default_extension_icon_image,
297 this));
300 void ZoomBubbleView::StartTimerIfNecessary() {
301 if (auto_close_) {
302 if (timer_.IsRunning()) {
303 timer_.Reset();
304 } else {
305 // The number of milliseconds the bubble should stay on the screen if it
306 // will close automatically.
307 const int kBubbleCloseDelay = 1500;
308 timer_.Start(
309 FROM_HERE,
310 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
311 this,
312 &ZoomBubbleView::Close);
317 void ZoomBubbleView::StopTimer() {
318 timer_.Stop();
321 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
323 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}