summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/notifications/balloon_view.cc
blob: 61ee2259f5510223e0cc8f4ce5b75f260682d8fe (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
// Copyright (c) 2011 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/chromeos/notifications/balloon_view.h"

#include <vector>

#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/notifications/balloon_view_host.h"
#include "chrome/browser/chromeos/notifications/notification_panel.h"
#include "chrome/browser/notifications/balloon.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/views/notifications/balloon_view_host.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/render_widget_host_view.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"
#include "content/common/notification_type.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "views/background.h"
#include "views/controls/button/button.h"
#include "views/controls/button/image_button.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/label.h"
#include "views/controls/menu/menu_2.h"
#include "views/controls/menu/view_menu_delegate.h"
#include "views/widget/root_view.h"
#include "views/widget/widget.h"

namespace {
// Menu commands
const int kNoopCommand = 0;
const int kRevokePermissionCommand = 1;

// Vertical margin between close button and menu button.
const int kControlButtonsMargin = 6;

// Top, Right margin for notification control view.
const int kControlViewTopMargin = 4;
const int kControlViewRightMargin = 6;
}  // namespace

namespace chromeos {

// NotificationControlView has close and menu buttons and
// overlays on top of renderer view.
class NotificationControlView : public views::View,
                                public views::ViewMenuDelegate,
                                public ui::SimpleMenuModel::Delegate,
                                public views::ButtonListener {
 public:
  explicit NotificationControlView(BalloonViewImpl* view)
      : balloon_view_(view),
        close_button_(NULL),
        options_menu_contents_(NULL),
        options_menu_menu_(NULL),
        options_menu_button_(NULL) {
    // TODO(oshima): make background transparent.
    set_background(views::Background::CreateSolidBackground(SK_ColorWHITE));

    ResourceBundle& rb = ResourceBundle::GetSharedInstance();

    SkBitmap* close_button_n = rb.GetBitmapNamed(IDR_TAB_CLOSE);
    SkBitmap* close_button_m = rb.GetBitmapNamed(IDR_TAB_CLOSE_MASK);
    SkBitmap* close_button_h = rb.GetBitmapNamed(IDR_TAB_CLOSE_H);
    SkBitmap* close_button_p = rb.GetBitmapNamed(IDR_TAB_CLOSE_P);

    close_button_ = new views::ImageButton(this);
    close_button_->SetImage(views::CustomButton::BS_NORMAL, close_button_n);
    close_button_->SetImage(views::CustomButton::BS_HOT, close_button_h);
    close_button_->SetImage(views::CustomButton::BS_PUSHED, close_button_p);
    close_button_->SetBackground(
        SK_ColorBLACK, close_button_n, close_button_m);

    AddChildView(close_button_);

    options_menu_button_
        = new views::MenuButton(NULL, std::wstring(), this, false);
    options_menu_button_->SetFont(rb.GetFont(ResourceBundle::SmallFont));
    options_menu_button_->SetIcon(*rb.GetBitmapNamed(IDR_NOTIFICATION_MENU));
    options_menu_button_->set_border(NULL);

    options_menu_button_->set_icon_placement(views::TextButton::ICON_ON_RIGHT);
    AddChildView(options_menu_button_);

    // The control view will never be resized, so just layout once.
    gfx::Size options_size = options_menu_button_->GetPreferredSize();
    gfx::Size button_size = close_button_->GetPreferredSize();

    int height = std::max(options_size.height(), button_size.height());
    options_menu_button_->SetBounds(
        0, (height - options_size.height()) / 2,
        options_size.width(), options_size.height());

    close_button_->SetBounds(
        options_size.width() + kControlButtonsMargin,
        (height - button_size.height()) / 2,
        button_size.width(), button_size.height());

    SizeToPreferredSize();
  }

  virtual gfx::Size GetPreferredSize() {
    gfx::Rect total_bounds =
        close_button_->bounds().Union(options_menu_button_->bounds());
    return total_bounds.size();
  }

  // views::ViewMenuDelegate implements.
  virtual void RunMenu(views::View* source, const gfx::Point& pt) {
    CreateOptionsMenu();
    options_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
  }

  // views::ButtonListener implements.
  virtual void ButtonPressed(views::Button* sender, const views::Event&) {
    balloon_view_->Close(true);
  }

  // ui::SimpleMenuModel::Delegate impglements.
  virtual bool IsCommandIdChecked(int /* command_id */) const {
    // Nothing in the menu is checked.
    return false;
  }

  virtual bool IsCommandIdEnabled(int /* command_id */) const {
    // All the menu options are always enabled.
    return true;
  }

  virtual bool GetAcceleratorForCommandId(
      int /* command_id */, ui::Accelerator* /* accelerator */) {
    // Currently no accelerators.
    return false;
  }

  virtual void ExecuteCommand(int command_id) {
    switch (command_id) {
      case kRevokePermissionCommand:
        balloon_view_->DenyPermission();
      default:
        NOTIMPLEMENTED();
    }
  }

 private:
  void CreateOptionsMenu() {
    if (options_menu_contents_.get())
      return;
    const string16 source_label_text = l10n_util::GetStringFUTF16(
        IDS_NOTIFICATION_BALLOON_SOURCE_LABEL,
        balloon_view_->balloon_->notification().display_source());
    const string16 label_text = l10n_util::GetStringFUTF16(
        IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
        balloon_view_->balloon_->notification().display_source());

    options_menu_contents_.reset(new ui::SimpleMenuModel(this));
    // TODO(oshima): Showing the source info in the menu for now.
    // Figure out where to show the source info.
    options_menu_contents_->AddItem(kNoopCommand, source_label_text);
    options_menu_contents_->AddItem(kRevokePermissionCommand, label_text);

    options_menu_menu_.reset(new views::Menu2(options_menu_contents_.get()));
  }

  BalloonViewImpl* balloon_view_;

  views::ImageButton* close_button_;

  // The options menu.
  scoped_ptr<ui::SimpleMenuModel> options_menu_contents_;
  scoped_ptr<views::Menu2> options_menu_menu_;
  views::MenuButton* options_menu_button_;

  DISALLOW_COPY_AND_ASSIGN(NotificationControlView);
};

BalloonViewImpl::BalloonViewImpl(bool sticky, bool controls, bool web_ui)
    : balloon_(NULL),
      html_contents_(NULL),
      method_factory_(this),
      stale_(false),
      sticky_(sticky),
      controls_(controls),
      closed_(false),
      web_ui_(web_ui) {
  // This object is not to be deleted by the views hierarchy,
  // as it is owned by the balloon.
  set_parent_owned(false);
}

BalloonViewImpl::~BalloonViewImpl() {
  if (control_view_host_.get()) {
    control_view_host_->CloseNow();
  }
  if (html_contents_) {
    html_contents_->Shutdown();
  }
}

////////////////////////////////////////////////////////////////////////////////
// BallonViewImpl, BalloonView implementation.

void BalloonViewImpl::Show(Balloon* balloon) {
  balloon_ = balloon;
  html_contents_ = new BalloonViewHost(balloon);
  if (web_ui_)
    html_contents_->EnableWebUI();
  AddChildView(html_contents_->view());
  notification_registrar_.Add(this,
    NotificationType::NOTIFY_BALLOON_DISCONNECTED, Source<Balloon>(balloon));
}

void BalloonViewImpl::Update() {
  stale_ = false;
  if (html_contents_->render_view_host())
    html_contents_->render_view_host()->NavigateToURL(
        balloon_->notification().content_url());
}

void BalloonViewImpl::Close(bool by_user) {
  closed_ = true;
  MessageLoop::current()->PostTask(
      FROM_HERE,
      method_factory_.NewRunnableMethod(
          &BalloonViewImpl::DelayedClose, by_user));
}

gfx::Size BalloonViewImpl::GetSize() const {
  // Not used. The layout is managed by the Panel.
  return gfx::Size(0, 0);
}

BalloonHost* BalloonViewImpl::GetHost() const {
  return html_contents_;
}

void BalloonViewImpl::RepositionToBalloon() {
  // Not used. The layout is managed by the Panel.
}

////////////////////////////////////////////////////////////////////////////////
// views::View interface overrides.

void BalloonViewImpl::Layout() {
  gfx::Size size = balloon_->content_size();

  SetBounds(x(), y(), size.width(), size.height());

  html_contents_->view()->SetBounds(0, 0, size.width(), size.height());
  if (html_contents_->render_view_host()) {
    RenderWidgetHostView* view = html_contents_->render_view_host()->view();
    if (view)
      view->SetSize(size);
  }
}

void BalloonViewImpl::ViewHierarchyChanged(
    bool is_add, View* parent, View* child) {
  if (is_add && GetWidget() && !control_view_host_.get() && controls_) {
    control_view_host_.reset(new views::Widget);
    views::Widget::InitParams params(
        views::Widget::InitParams::TYPE_CONTROL);
    params.double_buffer = true;
    params.delete_on_destroy = false;
    params.parent = GetParentNativeView();
    control_view_host_->Init(params);
    NotificationControlView* control = new NotificationControlView(this);
    control_view_host_->SetContentsView(control);
  }
  if (!is_add && this == child && control_view_host_.get() && controls_)
    control_view_host_.release()->CloseNow();
}

////////////////////////////////////////////////////////////////////////////////
// NotificationObserver overrides.

void BalloonViewImpl::Observe(NotificationType type,
                              const NotificationSource& source,
                              const NotificationDetails& details) {
  if (type != NotificationType::NOTIFY_BALLOON_DISCONNECTED) {
    NOTREACHED();
    return;
  }

  // If the renderer process attached to this balloon is disconnected
  // (e.g., because of a crash), we want to close the balloon.
  notification_registrar_.Remove(this,
      NotificationType::NOTIFY_BALLOON_DISCONNECTED, Source<Balloon>(balloon_));
  Close(false);
}

////////////////////////////////////////////////////////////////////////////////
// BalloonViewImpl public.

bool BalloonViewImpl::IsFor(const Notification& notification) const {
  return balloon_->notification().notification_id() ==
      notification.notification_id();
}

void BalloonViewImpl::Activated() {
  if (!control_view_host_.get())
    return;

  // Get the size of Control View.
  gfx::Size size =
      control_view_host_->GetRootView()->GetChildViewAt(0)->GetPreferredSize();
  control_view_host_->Show();
  control_view_host_->SetBounds(
      gfx::Rect(width() - size.width() - kControlViewRightMargin,
                kControlViewTopMargin,
                size.width(), size.height()));
}

void BalloonViewImpl::Deactivated() {
  if (control_view_host_.get()) {
    control_view_host_->Hide();
  }
}

////////////////////////////////////////////////////////////////////////////////
// BalloonViewImpl private.

void BalloonViewImpl::DelayedClose(bool by_user) {
  html_contents_->Shutdown();
  html_contents_ = NULL;
  balloon_->OnClose(by_user);
}

void BalloonViewImpl::DenyPermission() {
  DesktopNotificationService* service =
      DesktopNotificationServiceFactory::GetForProfile(balloon_->profile());
  service->DenyPermission(balloon_->notification().origin_url());
}

gfx::NativeView BalloonViewImpl::GetParentNativeView() {
  RenderWidgetHostView* view = html_contents_->render_view_host()->view();
  DCHECK(view);
  return view->GetNativeView();
}

}  // namespace chromeos