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.
4
5#include "chrome/browser/notifications/balloon.h"
6
7#include "base/logging.h"
8#include "chrome/browser/extensions/extension_service.h"
9#include "chrome/browser/notifications/balloon_collection.h"
10#include "chrome/browser/notifications/notification.h"
11#include "chrome/browser/profiles/profile.h"
12#include "ui/gfx/rect.h"
13#include "ui/gfx/size.h"
14
15#if !defined(OS_WIN) && !defined(USE_AURA)
16// static
17int BalloonView::GetHorizontalMargin() {
18  // TODO: implement for linux (non-aura) and mac.
19  return 0;
20}
21#endif
22
23Balloon::Balloon(const Notification& notification, Profile* profile,
24                 BalloonCollection* collection)
25    : profile_(profile),
26      notification_(new Notification(notification)),
27      collection_(collection) {
28}
29
30Balloon::~Balloon() {
31}
32
33void Balloon::SetPosition(const gfx::Point& upper_left, bool reposition) {
34  position_ = upper_left;
35  if (reposition && balloon_view_.get())
36    balloon_view_->RepositionToBalloon();
37}
38
39void Balloon::ResizeDueToAutoResize(const gfx::Size& size) {
40  collection_->ResizeBalloon(this, size);
41}
42
43void Balloon::set_view(BalloonView* balloon_view) {
44  balloon_view_.reset(balloon_view);
45}
46
47void Balloon::Show() {
48  notification_->Display();
49  if (balloon_view_.get()) {
50    balloon_view_->Show(this);
51    balloon_view_->RepositionToBalloon();
52  }
53}
54
55void Balloon::Update(const Notification& notification) {
56  notification_->Close(false);
57  notification_.reset(new Notification(notification));
58  notification_->Display();
59  if (balloon_view_.get()) {
60    balloon_view_->Update();
61  }
62}
63
64void Balloon::OnClick() {
65  notification_->Click();
66}
67
68void Balloon::OnClose(bool by_user) {
69  notification_->Close(by_user);
70  collection_->OnBalloonClosed(this);
71}
72
73void Balloon::OnButtonClick(int button_index) {
74  notification_->ButtonClick(button_index);
75}
76
77void Balloon::CloseByScript() {
78  // A user-initiated close begins with the view and then closes this object;
79  // we simulate that with a script-initiated close but pass |by_user|=false.
80  DCHECK(balloon_view_.get());
81  balloon_view_->Close(false);
82}
83
84std::string Balloon::GetExtensionId() {
85  const ExtensionService* service = profile()->GetExtensionService();
86  const extensions::Extension* extension =
87      service->extensions()->GetExtensionOrAppByURL(
88          notification().origin_url());
89  return extension ? extension->id() : std::string();
90}
91