summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/desktop_notification_service_win.cc
blob: 41e3e298170e0abd21a2925b0ca14211c12e81f1 (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
// Copyright (c) 2009 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/notifications/desktop_notification_service.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/notifications/notification_object_proxy.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/browser/worker_host/worker_process_host.h"
#include "chrome/common/child_process_host.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"

// Creates a data:xxxx URL which contains the full HTML for a notification
// using supplied icon, title, and text, run through a template which contains
// the standard formatting for notifications.
static string16 CreateDataUrl(const GURL& icon_url, const string16& title,
    const string16& body) {
  const base::StringPiece template_html(
      ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_NOTIFICATION_HTML));

  if (template_html.empty()) {
    NOTREACHED() << "unable to load template. ID: " << IDR_NOTIFICATION_HTML;
    return EmptyString16();
  }

  std::vector<string16> subst;
  if (icon_url.is_valid())
    subst.push_back(UTF8ToUTF16(icon_url.spec()));
  else
    subst.push_back(EmptyString16());

  subst.push_back(title);
  subst.push_back(body);

  if (icon_url.is_valid()) {
    subst.push_back(ASCIIToUTF16("block"));
    subst.push_back(ASCIIToUTF16("60"));
  } else {
    subst.push_back(ASCIIToUTF16("none"));
    subst.push_back(ASCIIToUTF16("5"));
  }

  string16 format_string = ASCIIToUTF16("data:text/html;charset=utf-8,"
                                        + template_html.as_string());
  return ReplaceStringPlaceholders(format_string, subst, NULL);
}

// This will call the notification manager on the UI thread to actually
// put the notification with the requested parameters on the desktop.
void DesktopNotificationService::ShowNotification(
    const Notification& notification) {
  SiteInstance* site_instance = SiteInstance::CreateSiteInstance(profile_);
  // TODO(johnnyg): When UI Manager is available, add from here.
  //  ui_manager_->Add(notification, profile_, site_instance);
}

// Shows a notification bubble which contains the contents of url.
bool DesktopNotificationService::ShowDesktopNotification(
    const GURL& origin, const GURL& url, int process_id, int route_id,
    NotificationSource source, int notification_id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  NotificationObjectProxy* proxy =
      new NotificationObjectProxy(process_id, route_id,
                                  notification_id,
                                  source == WorkerNotification);
  Notification notif(origin, url, proxy);
  ShowNotification(notif);
  return true;
}

// Shows a notification constructed from an icon, title, and text.
bool DesktopNotificationService::ShowDesktopNotificationText(
    const GURL& origin, const GURL& icon, const string16& title,
    const string16& text, int process_id, int route_id,
    NotificationSource source, int notification_id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  NotificationObjectProxy* proxy =
      new NotificationObjectProxy(process_id, route_id,
                                  notification_id,
                                  source == WorkerNotification);
  // "upconvert" the string parameters to a data: URL.
  string16 data_url = CreateDataUrl(icon, title, text);
  Notification notif(origin, GURL(data_url), proxy);
  ShowNotification(notif);
  return true;
}