Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / zoom_bubble_view.cc
blobeff898a1bef2102752560165754c5e2fecc44b6e
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 bool ZoomBubbleView::IsShowing() {
105 // The bubble is considered showing while closing.
106 return zoom_bubble_ != NULL && (zoom_bubble_->GetWidget()->IsVisible() ||
107 zoom_bubble_->GetWidget()->IsClosed());
110 // static
111 const ZoomBubbleView* ZoomBubbleView::GetZoomBubbleForTest() {
112 return zoom_bubble_;
115 ZoomBubbleView::ZoomBubbleView(
116 views::View* anchor_view,
117 content::WebContents* web_contents,
118 bool auto_close,
119 ImmersiveModeController* immersive_mode_controller)
120 : ManagedFullScreenBubbleDelegateView(anchor_view, web_contents),
121 image_button_(NULL),
122 label_(NULL),
123 web_contents_(web_contents),
124 auto_close_(auto_close),
125 immersive_mode_controller_(immersive_mode_controller) {
126 // Compensate for built-in vertical padding in the anchor view's image.
127 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
128 set_notify_enter_exit_on_child(true);
129 immersive_mode_controller_->AddObserver(this);
132 ZoomBubbleView::~ZoomBubbleView() {
133 if (immersive_mode_controller_)
134 immersive_mode_controller_->RemoveObserver(this);
137 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
138 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
139 event->type() != ui::ET_GESTURE_TAP) {
140 return;
143 auto_close_ = false;
144 StopTimer();
145 event->SetHandled();
148 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
149 StopTimer();
152 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
153 StartTimerIfNecessary();
156 void ZoomBubbleView::Init() {
157 // Set up the layout of the zoom bubble. A grid layout is used because
158 // sometimes an extension icon is shown next to the zoom label.
159 views::GridLayout* grid_layout = new views::GridLayout(this);
160 SetLayoutManager(grid_layout);
161 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
162 // First row.
163 if (extension_info_.icon_image) {
164 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2,
165 views::GridLayout::USE_PREF, 0, 0);
167 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
168 views::GridLayout::USE_PREF, 0, 0);
169 grid_layout->StartRow(0, 0);
171 // If this zoom change was initiated by an extension, that extension will be
172 // attributed by showing its icon in the zoom bubble.
173 if (extension_info_.icon_image) {
174 image_button_ = new views::ImageButton(this);
175 image_button_->SetTooltipText(
176 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
177 base::UTF8ToUTF16(extension_info_.name)));
178 image_button_->SetImage(views::Button::STATE_NORMAL,
179 &extension_info_.icon_image->image_skia());
180 grid_layout->AddView(image_button_);
183 // Add zoom label with the new zoom percent.
184 ui_zoom::ZoomController* zoom_controller =
185 ui_zoom::ZoomController::FromWebContents(web_contents_);
186 int zoom_percent = zoom_controller->GetZoomPercent();
187 label_ = new views::Label(
188 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
189 label_->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
190 ui::ResourceBundle::MediumFont));
191 grid_layout->AddView(label_);
193 // Second row.
194 grid_layout->AddPaddingRow(0, 8);
195 columns = grid_layout->AddColumnSet(1);
196 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
197 views::GridLayout::USE_PREF, 0, 0);
198 grid_layout->StartRow(0, 1);
200 // Add "Reset to Default" button.
201 views::LabelButton* set_default_button = new views::LabelButton(
202 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
203 set_default_button->SetStyle(views::Button::STYLE_BUTTON);
204 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
205 grid_layout->AddView(set_default_button);
207 StartTimerIfNecessary();
210 void ZoomBubbleView::WindowClosing() {
211 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
212 // call this right away). Only set to NULL when it's this bubble.
213 if (zoom_bubble_ == this)
214 zoom_bubble_ = NULL;
217 void ZoomBubbleView::Close() {
218 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
219 // this. Additionally web_contents_ may have been destroyed.
220 zoom_bubble_ = NULL;
221 web_contents_ = NULL;
222 ManagedFullScreenBubbleDelegateView::Close();
225 void ZoomBubbleView::ButtonPressed(views::Button* sender,
226 const ui::Event& event) {
227 if (sender == image_button_) {
228 DCHECK(extension_info_.icon_image) << "Invalid button press.";
229 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
230 chrome::AddSelectedTabWithURL(
231 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
232 extension_info_.id.c_str())),
233 ui::PAGE_TRANSITION_FROM_API);
234 } else {
235 ui_zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
239 void ZoomBubbleView::OnImmersiveRevealStarted() {
240 CloseBubble();
243 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
244 immersive_mode_controller_ = NULL;
247 void ZoomBubbleView::OnExtensionIconImageChanged(
248 extensions::IconImage* /* image */) {
249 image_button_->SetImage(views::Button::STATE_NORMAL,
250 &extension_info_.icon_image->image_skia());
251 image_button_->SchedulePaint();
254 void ZoomBubbleView::Refresh() {
255 ui_zoom::ZoomController* zoom_controller =
256 ui_zoom::ZoomController::FromWebContents(web_contents_);
257 int zoom_percent = zoom_controller->GetZoomPercent();
258 label_->SetText(
259 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
260 StartTimerIfNecessary();
263 void ZoomBubbleView::SetExtensionInfo(const extensions::Extension* extension) {
264 DCHECK(extension);
265 extension_info_.id = extension->id();
266 extension_info_.name = extension->name();
268 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
269 const gfx::ImageSkia& default_extension_icon_image =
270 *rb.GetImageSkiaNamed(IDR_EXTENSIONS_FAVICON);
271 int icon_size = gfx::kFaviconSize;
273 // We give first preference to an icon from the extension's icon set that
274 // matches the size of the default. But not all extensions will declare an
275 // icon set, or may not have an icon of the default size (we don't want the
276 // bubble to display, for example, a very large icon). In that case, if there
277 // is a browser-action icon (size-19) this is an acceptable alternative.
278 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension);
279 bool has_default_sized_icon =
280 !icons.Get(gfx::kFaviconSize, ExtensionIconSet::MATCH_EXACTLY).empty();
281 if (has_default_sized_icon) {
282 extension_info_.icon_image.reset(
283 new extensions::IconImage(web_contents_->GetBrowserContext(),
284 extension,
285 icons,
286 icon_size,
287 default_extension_icon_image,
288 this));
289 return;
292 const extensions::ActionInfo* browser_action =
293 extensions::ActionInfo::GetBrowserActionInfo(extension);
294 if (!browser_action || browser_action->default_icon.empty())
295 return;
297 icon_size = browser_action->default_icon.map().begin()->first;
298 extension_info_.icon_image.reset(
299 new extensions::IconImage(web_contents_->GetBrowserContext(),
300 extension,
301 browser_action->default_icon,
302 icon_size,
303 default_extension_icon_image,
304 this));
307 void ZoomBubbleView::StartTimerIfNecessary() {
308 if (auto_close_) {
309 if (timer_.IsRunning()) {
310 timer_.Reset();
311 } else {
312 // The number of milliseconds the bubble should stay on the screen if it
313 // will close automatically.
314 const int kBubbleCloseDelay = 1500;
315 timer_.Start(
316 FROM_HERE,
317 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
318 this,
319 &ZoomBubbleView::Close);
324 void ZoomBubbleView::StopTimer() {
325 timer_.Stop();
328 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
330 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}