system_notification.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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/chromeos/notifications/system_notification.h"
6
7#include "base/callback.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/chromeos/notifications/system_notification_factory.h"
10#include "chrome/browser/dom_ui/web_ui_util.h"
11#include "chrome/browser/notifications/notification.h"
12#include "chrome/browser/notifications/notification_ui_manager.h"
13
14namespace chromeos {
15
16void SystemNotification::Init(int icon_resource_id) {
17  collection_ = static_cast<BalloonCollectionImpl*>(
18       g_browser_process->notification_ui_manager()->balloon_collection());
19  std::string url = web_ui_util::GetImageDataUrlFromResource(icon_resource_id);
20  DCHECK(!url.empty());
21  GURL tmp_gurl(url);
22  icon_.Swap(&tmp_gurl);
23}
24
25SystemNotification::SystemNotification(Profile* profile,
26                                       NotificationDelegate* delegate,
27                                       int icon_resource_id,
28                                       const string16& title)
29    : profile_(profile),
30      collection_(NULL),
31      delegate_(delegate),
32      title_(title),
33      visible_(false),
34      urgent_(false) {
35  Init(icon_resource_id);
36}
37
38SystemNotification::SystemNotification(Profile* profile,
39                                       const std::string& id,
40                                       int icon_resource_id,
41                                       const string16& title)
42    : profile_(profile),
43      collection_(NULL),
44      delegate_(new Delegate(id)),
45      title_(title),
46      visible_(false),
47      urgent_(false) {
48  Init(icon_resource_id);
49}
50
51SystemNotification::~SystemNotification() {
52}
53
54void SystemNotification::Show(const string16& message,
55                              bool urgent,
56                              bool sticky) {
57  Show(message, string16(), NULL, urgent, sticky);
58}
59
60void SystemNotification::Show(const string16& message,
61                              const string16& link,
62                              MessageCallback* callback,
63                              bool urgent,
64                              bool sticky) {
65  Notification notify = SystemNotificationFactory::Create(icon_,
66      title_, message, link, delegate_.get());
67  if (visible_) {
68    // Force showing a user hidden notification on an urgent transition.
69    if (urgent && !urgent_) {
70      collection_->UpdateAndShowNotification(notify);
71    } else {
72      collection_->UpdateNotification(notify);
73    }
74  } else {
75    collection_->AddSystemNotification(notify, profile_,
76                                       sticky,
77                                       false /* no controls */);
78    collection_->AddWebUIMessageCallback(notify, "link", callback);
79  }
80  visible_ = true;
81  urgent_ = urgent;
82}
83
84void SystemNotification::Hide() {
85  if (visible_) {
86    collection_->RemoveById(delegate_->id());
87    visible_ = false;
88    urgent_ = false;
89  }
90}
91
92}  // namespace chromeos
93