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/notifications/balloon.h"
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"
15 #if !defined(USE_AURA)
17 int BalloonView::GetHorizontalMargin() {
18 // TODO: implement for linux (non-aura) and mac.
23 Balloon::Balloon(const Notification
& notification
, Profile
* profile
,
24 BalloonCollection
* collection
)
26 notification_(new Notification(notification
)),
27 collection_(collection
) {
33 void Balloon::SetPosition(const gfx::Point
& upper_left
, bool reposition
) {
34 position_
= upper_left
;
35 if (reposition
&& balloon_view_
.get())
36 balloon_view_
->RepositionToBalloon();
39 void Balloon::ResizeDueToAutoResize(const gfx::Size
& size
) {
40 collection_
->ResizeBalloon(this, size
);
43 void Balloon::set_view(BalloonView
* balloon_view
) {
44 balloon_view_
.reset(balloon_view
);
47 void Balloon::Show() {
48 notification_
->Display();
49 if (balloon_view_
.get()) {
50 balloon_view_
->Show(this);
51 balloon_view_
->RepositionToBalloon();
55 void 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();
64 void Balloon::OnClick() {
65 notification_
->Click();
68 void Balloon::OnClose(bool by_user
) {
69 notification_
->Close(by_user
);
70 collection_
->OnBalloonClosed(this);
73 void Balloon::OnButtonClick(int button_index
) {
74 notification_
->ButtonClick(button_index
);
77 void 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);
84 std::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();