content_setting_image_view.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/location_bar/content_setting_image_view.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/tab_specific_content_settings.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/content_settings/content_setting_image_model.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/content_setting_bubble_contents.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "grit/theme_resources.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/gfx/color_utils.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/views/controls/image_view.h"
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/views/controls/label.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const int kBackgroundImages[] = IMAGE_GRID(IDR_OMNIBOX_CONTENT_SETTING_BUBBLE);
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const int kStayOpenTimeMS = 3200;  // Time spent with animation fully open.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const int ContentSettingImageView::kOpenTimeMS = 150;
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const int ContentSettingImageView::kAnimationDurationMS =
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    (kOpenTimeMS * 2) + kStayOpenTimeMS;
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSettingImageView::ContentSettingImageView(
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsType content_type,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LocationBarView* parent,
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::FontList& font_list,
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SkColor text_color,
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SkColor parent_background_color)
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : parent_(parent),
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content_setting_image_model_(
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ContentSettingImageModel::CreateContentSettingImageModel(
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              content_type)),
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      background_painter_(
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          views::Painter::CreateImageGridPainter(kBackgroundImages)),
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      icon_(new views::ImageView),
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      text_label_(new views::Label(string16(), font_list)),
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      slide_animator_(this),
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pause_animation_(false),
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pause_animation_state_(0.0),
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bubble_widget_(NULL) {
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  icon_->SetHorizontalAlignment(views::ImageView::LEADING);
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddChildView(icon_);
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetVisible(false);
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetEnabledColor(text_color);
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Calculate the actual background color for the label.  The background images
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // are painted atop |parent_background_color|.  We grab the color of the
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // middle pixel of the middle image of the background, which we treat as the
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // representative color of the entire background (reasonable, given the
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // current appearance of these images).  Then we alpha-blend it over the
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // parent background color to determine the actual color the label text will
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // sit atop.
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SkBitmap& bitmap(
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
6568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          kBackgroundImages[4])->GetRepresentation(1.0f).sk_bitmap());
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SkAutoLockPixels pixel_lock(bitmap);
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SkColor background_image_color =
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2);
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Tricky bit: We alpha blend an opaque version of |background_image_color|
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // against |parent_background_color| using the original image grid color's
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // even if |a| is a color with non-255 alpha.
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetBackgroundColor(
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      color_utils::AlphaBlend(SkColorSetA(background_image_color, 255),
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              parent_background_color,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              SkColorGetA(background_image_color)));
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetElideBehavior(views::Label::NO_ELIDE);
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddChildView(text_label_);
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
81ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  LocationBarView::InitTouchableLocationBarChildView(this);
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  slide_animator_.SetSlideDuration(kAnimationDurationMS);
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  slide_animator_.SetTweenType(gfx::Tween::LINEAR);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSettingImageView::~ContentSettingImageView() {
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (bubble_widget_)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bubble_widget_->RemoveObserver(this);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void ContentSettingImageView::UpdatePreLayout(
9358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    content::WebContents* web_contents) {
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Note: We explicitly want to call this even if |web_contents| is NULL, so we
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // get hidden properly while the user is editing the omnibox.
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  content_setting_image_model_->UpdateFromWebContents(web_contents);
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!content_setting_image_model_->is_visible()) {
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetVisible(false);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content_setting_image_model_->get_icon()));
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  icon_->SetTooltipText(
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      UTF8ToUTF16(content_setting_image_model_->get_tooltip()));
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetVisible(true);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If the content blockage should be indicated to the user, start the
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // animation and record that we indicated the blockage.
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TabSpecificContentSettings* content_settings = web_contents ?
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      TabSpecificContentSettings::FromWebContents(web_contents) : NULL;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!content_settings || content_settings->IsBlockageIndicated(
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content_setting_image_model_->get_content_settings_type()))
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // We just ignore this blockage if we're already showing some other string to
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the user.  If this becomes a problem, we could design some sort of queueing
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // mechanism to show one after the other, but it doesn't seem important now.
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int string_id = content_setting_image_model_->explanatory_string_id();
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (string_id && !background_showing()) {
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    text_label_->SetText(l10n_util::GetStringUTF16(string_id));
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    text_label_->SetVisible(true);
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    slide_animator_.Show();
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
12658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Since indicating blockage may include showing a bubble, which must be done
12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // in UpdatePostLayout() in order for the bubble to have the right anchor
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // coordinates, we delay calling SetBlockageHasBeeenIndicated() until that
12958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // function completes.
13058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
13158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void ContentSettingImageView::UpdatePostLayout(
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    content::WebContents* web_contents) {
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!content_setting_image_model_->is_visible())
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  TabSpecificContentSettings* content_settings = web_contents ?
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)     TabSpecificContentSettings::FromWebContents(web_contents) : NULL;
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!content_settings)
14058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!content_settings->IsBlockageIndicated(
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content_setting_image_model_->get_content_settings_type())) {
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (content_setting_image_model_->ShouldShowBubbleOnBlockage())
14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      CreateBubble(web_contents);
14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    content_settings->SetBlockageHasBeenIndicated(
14758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        content_setting_image_model_->get_content_settings_type());
14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int ContentSettingImageView::GetBubbleOuterPadding(bool by_icon) {
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return LocationBarView::GetItemPadding() - LocationBarView::kBubblePadding +
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (by_icon ? 0 : LocationBarView::kIconInternalPadding);
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
157d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void ContentSettingImageView::AnimationEnded(const gfx::Animation* animation) {
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  slide_animator_.Reset();
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!pause_animation_) {
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    text_label_->SetVisible(false);
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    parent_->Layout();
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    parent_->SchedulePaint();
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ContentSettingImageView::AnimationProgressed(
167d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!pause_animation_) {
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    parent_->Layout();
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    parent_->SchedulePaint();
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ContentSettingImageView::AnimationCanceled(
175d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AnimationEnded(animation);
17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)gfx::Size ContentSettingImageView::GetPreferredSize() {
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Height will be ignored by the LocationBarView.
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Size size(icon_->GetPreferredSize());
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (background_showing()) {
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    double state = slide_animator_.GetCurrentValue();
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // The fraction of the animation we'll spend animating the string into view,
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // which is also the fraction we'll spend animating it closed; total
186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // animation (slide out, show, then slide in) is 1.0.
187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const double kOpenFraction =
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        static_cast<double>(kOpenTimeMS) / kAnimationDurationMS;
189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    double size_fraction = 1.0;
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (state < kOpenFraction)
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      size_fraction = state / kOpenFraction;
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (state > (1.0 - kOpenFraction))
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      size_fraction = (1.0 - state) / kOpenFraction;
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    size.Enlarge(
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        size_fraction * (text_label_->GetPreferredSize().width() +
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            GetTotalSpacingWhileAnimating()), 0);
197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    size.SetToMax(background_painter_->GetMinimumSize());
19890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return size;
20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
20190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ContentSettingImageView::Layout() {
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const int icon_width = icon_->GetPreferredSize().width();
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  icon_->SetBounds(
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      std::min((width() - icon_width) / 2, GetBubbleOuterPadding(true)), 0,
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      icon_width, height());
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  text_label_->SetBounds(
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      icon_->bounds().right() + LocationBarView::GetItemPadding(), 0,
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      std::max(width() - GetTotalSpacingWhileAnimating() - icon_width, 0),
2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      height());
21190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
21290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool ContentSettingImageView::OnMousePressed(const ui::MouseEvent& event) {
21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // We want to show the bubble on mouse release; that is the standard behavior
21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // for buttons.
21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ContentSettingImageView::OnMouseReleased(const ui::MouseEvent& event) {
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (HitTestPoint(event.location()))
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    OnClick();
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ContentSettingImageView::OnGestureEvent(ui::GestureEvent* event) {
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (event->type() == ui::ET_GESTURE_TAP)
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    OnClick();
227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if ((event->type() == ui::ET_GESTURE_TAP) ||
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (event->type() == ui::ET_GESTURE_TAP_DOWN))
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    event->SetHandled();
23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ContentSettingImageView::OnPaintBackground(gfx::Canvas* canvas) {
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (background_showing())
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    background_painter_->Paint(canvas, size());
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ContentSettingImageView::OnWidgetDestroying(views::Widget* widget) {
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK_EQ(bubble_widget_, widget);
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bubble_widget_->RemoveObserver(this);
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bubble_widget_ = NULL;
241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (pause_animation_) {
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    slide_animator_.Reset(pause_animation_state_);
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    pause_animation_ = false;
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    slide_animator_.Show();
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
24790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
24890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int ContentSettingImageView::GetTotalSpacingWhileAnimating() const {
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return GetBubbleOuterPadding(true) + LocationBarView::GetItemPadding() +
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      GetBubbleOuterPadding(false);
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ContentSettingImageView::OnClick() {
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (slide_animator_.is_animating()) {
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!pause_animation_) {
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pause_animation_ = true;
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pause_animation_state_ = slide_animator_.GetCurrentValue();
259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    slide_animator_.Reset();
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WebContents* web_contents = parent_->GetWebContents();
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (web_contents && !bubble_widget_) {
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CreateBubble(web_contents);
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
26858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
26958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void ContentSettingImageView::CreateBubble(content::WebContents* web_contents) {
27058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bubble_widget_ =
27158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      parent_->delegate()->CreateViewsBubble(new ContentSettingBubbleContents(
27258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          ContentSettingBubbleModel::CreateContentSettingBubbleModel(
27358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              parent_->delegate()->GetContentSettingBubbleModelDelegate(),
27458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              web_contents, parent_->profile(),
27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              content_setting_image_model_->get_content_settings_type()),
2764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          this, views::BubbleBorder::TOP_RIGHT));
27758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bubble_widget_->AddObserver(this);
27858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bubble_widget_->Show();
27958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
280