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