summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/blocked_popup_container_view_views.cc
blob: 4feedba5d84801e2fd23b7a5bcec6242e8959591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Copyright (c) 2009 The Chromium Authors. All rights reserved.  Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

#include "chrome/browser/views/blocked_popup_container_view_views.h"

#include <math.h>
#if defined(OS_WIN)
#include <windows.h>
#endif

#include "app/gfx/canvas.h"
#include "app/gfx/path.h"
#include "app/gfx/scrollbar_size.h"
#include "app/l10n_util.h"
#include "app/menus/simple_menu_model.h"
#include "app/resource_bundle.h"
#include "app/slide_animation.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "views/background.h"
#include "views/controls/button/image_button.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/menu/menu_2.h"
#include "views/screen.h"

#if defined(OS_WIN)
#include "views/widget/widget_win.h"
#elif defined(OS_LINUX)
#include "views/widget/widget_gtk.h"
#endif

namespace {
// The minimal border around the edge of the notification.
const int kSmallPadding = 2;

// The background color of the blocked popup notification.
const SkColor kBackgroundColorTop = SkColorSetRGB(255, 242, 183);
const SkColor kBackgroundColorBottom = SkColorSetRGB(250, 230, 145);

// The border color of the blocked popup notification. This is the same as the
// border around the inside of the tab contents.
const SkColor kBorderColor = SkColorSetRGB(190, 205, 223);

// So that the MenuButton doesn't change its size as its text changes, during
// construction we feed it the strings it will be displaying, so it can set the
// max text width to the right value.  "99" should preallocate enough space for
// all numbers we'd show.
const int kWidestNumber = 99;

// Rounded corner radius (in pixels).
const int kBackgroundCornerRadius = 4;

// Rounded corner definition so the top corners are rounded, and the bottom are
// normal 90 degree angles.
const SkScalar kRoundedCornerRad[8] = {
  // Top left corner
  SkIntToScalar(kBackgroundCornerRadius),
  SkIntToScalar(kBackgroundCornerRadius),
  // Top right corner
  SkIntToScalar(kBackgroundCornerRadius),
  SkIntToScalar(kBackgroundCornerRadius),
  // Bottom right corner
  0,
  0,
  // Bottom left corner
  0,
  0
};

}  // namespace

#if defined(OS_WIN)

// BlockedPopupContainerViewWidget Win ----------------------------------------

class BlockedPopupContainerViewWidget : public views::WidgetWin {
 public:
  BlockedPopupContainerViewWidget(BlockedPopupContainerViewViews* container,
                                  gfx::NativeView parent);

  void SetBoundsAndShow(const gfx::Rect& bounds);
  void Show();
  void Hide();

  // Returns the size of our parent.
  gfx::Size GetParentSize();

 private:
  virtual void OnSize(UINT param, const CSize& size);

  BlockedPopupContainerViewViews* container_;

  DISALLOW_COPY_AND_ASSIGN(BlockedPopupContainerViewWidget);
};

BlockedPopupContainerViewWidget::BlockedPopupContainerViewWidget(
    BlockedPopupContainerViewViews* container, gfx::NativeView parent)
    : container_(container) {
  set_window_style(WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  WidgetWin::Init(parent, gfx::Rect());
}

void BlockedPopupContainerViewWidget::SetBoundsAndShow(
    const gfx::Rect& bounds) {
  SetWindowPos(HWND_TOP, bounds.x(), bounds.y(), bounds.width(),
               bounds.height(), SWP_SHOWWINDOW);
}

void BlockedPopupContainerViewWidget::Show() {
  SetWindowPos(HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}

void BlockedPopupContainerViewWidget::Hide() {
  SetWindowPos(HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_HIDEWINDOW);
}

gfx::Size BlockedPopupContainerViewWidget::GetParentSize() {
  HWND parent = GetParent();
  RECT client_rect;
  ::GetClientRect(parent, &client_rect);
  return gfx::Size(client_rect.right - client_rect.left,
                   client_rect.bottom - client_rect.top);
}

void BlockedPopupContainerViewWidget::OnSize(UINT param, const CSize& size) {
  container_->UpdateWidgetShape(this, gfx::Size(size.cx, size.cy));

  LayoutRootView();
}

#elif defined(OS_LINUX)

// BlockedPopupContainerViewWidget GTK ----------------------------------------
class BlockedPopupContainerViewWidget : public views::WidgetGtk {
 public:
  BlockedPopupContainerViewWidget(BlockedPopupContainerViewViews* container,
                                  gfx::NativeView parent);

  void SetBoundsAndShow(const gfx::Rect& bounds);

  // Returns the size of our parent.
  gfx::Size GetParentSize();

 private:
  virtual void OnSizeAllocate(GtkWidget* widget, GtkAllocation* allocation);

  BlockedPopupContainerViewViews* container_;

  DISALLOW_COPY_AND_ASSIGN(BlockedPopupContainerViewWidget);
};

BlockedPopupContainerViewWidget::BlockedPopupContainerViewWidget(
    BlockedPopupContainerViewViews* container,
    gfx::NativeView parent)
    : views::WidgetGtk(views::WidgetGtk::TYPE_CHILD),
      container_(container) {
  WidgetGtk::Init(parent, gfx::Rect());
}

void BlockedPopupContainerViewWidget::SetBoundsAndShow(
    const gfx::Rect& bounds) {
  SetBounds(bounds);
  Show();
}

gfx::Size BlockedPopupContainerViewWidget::GetParentSize() {
  GtkWidget* parent = gtk_widget_get_parent(GetNativeView());
  return gfx::Size(parent->allocation.width, parent->allocation.height);
}

void BlockedPopupContainerViewWidget::OnSizeAllocate(
    GtkWidget* widget, GtkAllocation* allocation) {
  WidgetGtk::OnSizeAllocate(widget, allocation);
  container_->UpdateWidgetShape(
      this, gfx::Size(allocation->width, allocation->height));
}

#endif

// BlockedPopupContainerInternalView ------------------------------------------

// The view presented to the user notifying them of the number of popups
// blocked. This view should only be used inside of BlockedPopupContainer.
class BlockedPopupContainerInternalView : public views::View,
                                  public views::ButtonListener,
                                  public menus::SimpleMenuModel::Delegate {
 public:
  explicit BlockedPopupContainerInternalView(
      BlockedPopupContainerViewViews* container);
  ~BlockedPopupContainerInternalView();

  // Sets the label on the menu button.
  void UpdateLabel();

  std::wstring label() const { return popup_count_label_->text(); }

  // Overridden from views::View:

  // Paints our border and background. (Does not paint children.)
  virtual void Paint(gfx::Canvas* canvas);
  // Sets positions of all child views.
  virtual void Layout();
  // Gets the desired size of the popup notification.
  virtual gfx::Size GetPreferredSize();

  // Overridden from views::ButtonListener:
  virtual void ButtonPressed(views::Button* sender, const views::Event& event);

  // Overridden from SimpleMenuModel::Delegate:

  // Displays the status of the "Show Blocked Popup Notification" item.
  virtual bool IsCommandIdChecked(int id) const;
  virtual bool IsCommandIdEnabled(int id) const { return true; }
  virtual bool GetAcceleratorForCommandId(int command_id,
                                          menus::Accelerator* accelerator) {
    return false;
  }
  // Called after user clicks a menu item.
  virtual void ExecuteCommand(int id);

 private:
  // Our owner and HWND parent.
  BlockedPopupContainerViewViews* container_;

  // The button which brings up the popup menu.
  views::MenuButton* popup_count_label_;

  // Our "X" button.
  views::ImageButton* close_button_;

  // Model for the menu.
  scoped_ptr<menus::SimpleMenuModel> launch_menu_model_;

  // Popup menu shown to user.
  scoped_ptr<views::Menu2> launch_menu_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(BlockedPopupContainerInternalView);
};


BlockedPopupContainerInternalView::BlockedPopupContainerInternalView(
    BlockedPopupContainerViewViews* container)
    : container_(container) {
  ResourceBundle &resource_bundle = ResourceBundle::GetSharedInstance();

  // Create a button with a multidigit number to reserve space.
  popup_count_label_ = new views::MenuButton(
      this,
      l10n_util::GetStringF(IDS_POPUPS_BLOCKED_COUNT,
                            IntToWString(kWidestNumber)),
      NULL, true);
  // Now set the text to the other possible display strings so that the button
  // will update its max text width (in case one of these string is longer).
  popup_count_label_->SetText(l10n_util::GetString(IDS_POPUPS_UNBLOCKED));
  popup_count_label_->SetText(l10n_util::GetString(IDS_BLOCKED_NOTICE_COUNT));
  popup_count_label_->set_alignment(views::TextButton::ALIGN_CENTER);
  AddChildView(popup_count_label_);

  // For now, we steal the Find close button, since it looks OK.
  close_button_ = new views::ImageButton(this);
  close_button_->SetFocusable(true);
  close_button_->SetImage(views::CustomButton::BS_NORMAL,
      resource_bundle.GetBitmapNamed(IDR_CLOSE_BAR));
  close_button_->SetImage(views::CustomButton::BS_HOT,
      resource_bundle.GetBitmapNamed(IDR_CLOSE_BAR_H));
  close_button_->SetImage(views::CustomButton::BS_PUSHED,
      resource_bundle.GetBitmapNamed(IDR_CLOSE_BAR_P));
  AddChildView(close_button_);

  set_background(views::Background::CreateStandardPanelBackground());
  UpdateLabel();
}

BlockedPopupContainerInternalView::~BlockedPopupContainerInternalView() {
}

void BlockedPopupContainerInternalView::UpdateLabel() {
  size_t blocked_notices = container_->model()->GetBlockedNoticeCount();
  size_t blocked_items = container_->model()->GetBlockedPopupCount() +
      blocked_notices;

  std::wstring label;
  if (blocked_items == 0) {
    label = l10n_util::GetString(IDS_POPUPS_UNBLOCKED);
  } else if (blocked_notices == 0) {
    label = l10n_util::GetStringF(IDS_POPUPS_BLOCKED_COUNT,
                                  UintToWString(blocked_items));
  } else {
    label = l10n_util::GetStringF(IDS_BLOCKED_NOTICE_COUNT,
                                  UintToWString(blocked_items));
  }
  popup_count_label_->SetText(label);

  Layout();
  SchedulePaint();
}

void BlockedPopupContainerInternalView::Paint(gfx::Canvas* canvas) {
  // Draw the standard background.
  View::Paint(canvas);

  SkRect rect;
  rect.set(0, 0, SkIntToScalar(width()), SkIntToScalar(height()));

  // Draw the border
  SkPaint border_paint;
  border_paint.setFlags(SkPaint::kAntiAlias_Flag);
  border_paint.setStyle(SkPaint::kStroke_Style);
  border_paint.setColor(kBorderColor);
  SkPath border_path;
  border_path.addRoundRect(rect, kRoundedCornerRad, SkPath::kCW_Direction);
  canvas->drawPath(border_path, border_paint);
}

void BlockedPopupContainerInternalView::Layout() {
  gfx::Size panel_size = GetPreferredSize();
  gfx::Size button_size = close_button_->GetPreferredSize();
  gfx::Size size = popup_count_label_->GetPreferredSize();

  popup_count_label_->SetBounds(kSmallPadding, kSmallPadding,
                                size.width(),
                                size.height());

  int close_button_padding =
      static_cast<int>(ceil(panel_size.height() / 2.0) -
                       ceil(button_size.height() / 2.0));
  close_button_->SetBounds(width() - button_size.width() - close_button_padding,
                           close_button_padding,
                           button_size.width(),
                           button_size.height());
}

gfx::Size BlockedPopupContainerInternalView::GetPreferredSize() {
  gfx::Size preferred_size = popup_count_label_->GetPreferredSize();
  preferred_size.Enlarge(close_button_->GetPreferredSize().width(), 0);
  // Add padding to all sides of the |popup_count_label_| except the right.
  preferred_size.Enlarge(kSmallPadding, 2 * kSmallPadding);

  // Add padding to the left and right side of |close_button_| equal to its
  // horizontal/vertical spacing.
  gfx::Size button_size = close_button_->GetPreferredSize();
  int close_button_padding =
      static_cast<int>(ceil(preferred_size.height() / 2.0) -
                       ceil(button_size.height() / 2.0));
  preferred_size.Enlarge(2 * close_button_padding, 0);

  return preferred_size;
}

void BlockedPopupContainerInternalView::ButtonPressed(
    views::Button* sender, const views::Event& event) {
  if (sender == close_button_) {
    container_->model()->set_dismissed();
    container_->model()->CloseAll();
  }

  if (sender != popup_count_label_)
    return;

  launch_menu_model_.reset(new menus::SimpleMenuModel(this));

  // Set items 1 .. popup_count as individual popups.
  size_t popup_count = container_->model()->GetBlockedPopupCount();
  for (size_t i = 0; i < popup_count; ++i) {
    std::wstring url, title;
    container_->GetURLAndTitleForPopup(i, &url, &title);
    // We can't just use the index into container_ here because Menu reserves
    // the value 0 as the nop command.
    launch_menu_model_->AddItem(i + 1,
        l10n_util::GetStringFUTF16(IDS_POPUP_TITLE_FORMAT, WideToUTF16(url),
                                   WideToUTF16(title)));
  }

  // Set items (kImpossibleNumberOfPopups + 1) ..
  // (kImpossibleNumberOfPopups + hosts.size()) as hosts.
  std::vector<std::string> hosts(container_->model()->GetHosts());
  if (!hosts.empty() && (popup_count > 0))
    launch_menu_model_->AddSeparator();
  size_t first_host = BlockedPopupContainer::kImpossibleNumberOfPopups + 1;
  for (size_t i = 0; i < hosts.size(); ++i) {
    launch_menu_model_->AddCheckItem(first_host + i,
        l10n_util::GetStringFUTF16(IDS_POPUP_HOST_FORMAT,
                                   UTF8ToUTF16(hosts[i])));
  }

  // Set items (kImpossibleNumberOfPopups + hosts.size() + 2) ..
  // (kImpossibleNumberOfPopups + hosts.size() + 1 + notice_count) as notices.
  size_t notice_count = container_->model()->GetBlockedNoticeCount();
  if (notice_count && (!hosts.empty() || (popup_count > 0)))
    launch_menu_model_->AddSeparator();
  size_t first_notice = first_host + hosts.size() + 1;
  for (size_t i = 0; i < notice_count; ++i) {
    std::string host;
    string16 reason;
    container_->model()->GetHostAndReasonForNotice(i, &host, &reason);
    launch_menu_model_->AddItem(first_notice + i,
        l10n_util::GetStringFUTF16(IDS_NOTICE_TITLE_FORMAT, ASCIIToUTF16(host),
                                   reason));
  }

  launch_menu_.reset(new views::Menu2(launch_menu_model_.get()));
  launch_menu_->RunContextMenuAt(views::Screen::GetCursorScreenPoint());
}

bool BlockedPopupContainerInternalView::IsCommandIdChecked(int id) const {
  // |id| should be > 0 since all index based commands have 1 added to them.
  DCHECK_GT(id, 0);
  size_t id_size_t = static_cast<size_t>(id);

  if (id_size_t > BlockedPopupContainer::kImpossibleNumberOfPopups) {
    id_size_t -= BlockedPopupContainer::kImpossibleNumberOfPopups + 1;
    if (id_size_t < container_->model()->GetPopupHostCount())
      return container_->model()->IsHostWhitelisted(id_size_t);
  }

  return false;
}

void BlockedPopupContainerInternalView::ExecuteCommand(int id) {
  // |id| should be > 0 since all index based commands have 1 added to them.
  DCHECK_GT(id, 0);
  size_t id_size_t = static_cast<size_t>(id);

  // Is this a click on a popup?
  if (id_size_t < BlockedPopupContainer::kImpossibleNumberOfPopups) {
    container_->model()->LaunchPopupAtIndex(id_size_t - 1);
    return;
  }

  // |id| shouldn't be == kImpossibleNumberOfPopups since the popups end before
  // this and the hosts start after it.  (If it is used, it is as a separator.)
  DCHECK_NE(id_size_t, BlockedPopupContainer::kImpossibleNumberOfPopups);
  id_size_t -= BlockedPopupContainer::kImpossibleNumberOfPopups + 1;

  // Is this a click on a host?
  size_t host_count = container_->model()->GetPopupHostCount();
  if (id_size_t < host_count) {
    container_->model()->ToggleWhitelistingForHost(id_size_t);
    return;
  }

  // |id shouldn't be == host_count since this is the separator between hosts
  // and notices.
  DCHECK_NE(id_size_t, host_count);
  id_size_t -= host_count + 1;

  // Nothing to do for now for notices.
}

// BlockedPopupContainerViewViews ---------------------------------------------

// static
BlockedPopupContainerView* BlockedPopupContainerView::Create(
    BlockedPopupContainer* container) {
  return new BlockedPopupContainerViewViews(container);
}

BlockedPopupContainerViewViews::~BlockedPopupContainerViewViews() {
}

void BlockedPopupContainerViewViews::GetURLAndTitleForPopup(
    size_t index, std::wstring* url, std::wstring* title) const {
  DCHECK(url);
  DCHECK(title);
  TabContents* tab_contents = model()->GetTabContentsAt(index);
  const GURL& tab_contents_url = tab_contents->GetURL().GetOrigin();
  *url = UTF8ToWide(tab_contents_url.possibly_invalid_spec());
  *title = UTF16ToWideHack(tab_contents->GetTitle());
}

// Overridden from AnimationDelegate:

void BlockedPopupContainerViewViews::AnimationStarted(
    const Animation* animation) {
  SetPosition();
}

void BlockedPopupContainerViewViews::AnimationEnded(const Animation* animation) {
  SetPosition();
}

void BlockedPopupContainerViewViews::AnimationProgressed(
    const Animation* animation) {
  SetPosition();
}

// Overridden from BlockedPopupContainerView:

void BlockedPopupContainerViewViews::SetPosition() {
  gfx::Size parent_size = widget_->GetParentSize();

  // TODO(erg): There's no way to detect whether scroll bars are
  // visible, so for beta, we're just going to assume that the
  // vertical scroll bar is visible, and not care about covering up
  // the horizontal scroll bar. Fixing this is half of
  // http://b/1118139.
  gfx::Point anchor_point(parent_size.width() - gfx::scrollbar_size(),
                          parent_size.height());

  gfx::Size size = container_view_->GetPreferredSize();
  int base_x = anchor_point.x() - size.width();

  int real_height =
      static_cast<int>(size.height() * slide_animation_->GetCurrentValue());
  int real_y = anchor_point.y() - real_height;

  if (real_height > 0) {
    int x;
    if (l10n_util::GetTextDirection() == l10n_util::LEFT_TO_RIGHT) {
      // Size this window using the anchor point as top-right corner.
      x = base_x;
    } else {
      // Size this window to the bottom left corner of top client window. In
      // Chrome, scrollbars always appear on the right, even for a RTL page or
      // when the UI is RTL (see http://crbug.com/6113 for more detail). Thus 0
      // is always a safe value for x-axis.
      x = 0;
    }
    widget_->SetBoundsAndShow(gfx::Rect(x, real_y, size.width(), real_height));
    container_view_->SchedulePaint();
  } else {
    widget_->Hide();
  }
}

void BlockedPopupContainerViewViews::ShowView() {
  widget_->Show();
  slide_animation_->Show();
}

void BlockedPopupContainerViewViews::UpdateLabel() {
  container_view_->UpdateLabel();
}

void BlockedPopupContainerViewViews::HideView() {
  slide_animation_->Hide();
}

void BlockedPopupContainerViewViews::Destroy() {
  widget_->CloseNow();
  delete this;
}

// private:

BlockedPopupContainerViewViews::BlockedPopupContainerViewViews(
    BlockedPopupContainer* container)
    : widget_(NULL),
      model_(container),
      container_view_(NULL),
      slide_animation_(new SlideAnimation(this)) {
  widget_ = new BlockedPopupContainerViewWidget(this,
      model_->GetConstrainingContents(NULL)->GetNativeView());
  container_view_ = new BlockedPopupContainerInternalView(this);
  widget_->SetContentsView(container_view_);
  SetPosition();
}

void BlockedPopupContainerViewViews::UpdateWidgetShape(
    BlockedPopupContainerViewWidget* widget, const gfx::Size& size) {
  // Set the shape so we have rounded corners on the top.
  SkRect rect;
  rect.set(0, 0, SkIntToScalar(size.width()), SkIntToScalar(size.height()));
  gfx::Path path;
  path.addRoundRect(rect, kRoundedCornerRad, SkPath::kCW_Direction);
  widget->SetShape(path.CreateNativeRegion());
}