summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/notification/notification_api.cc
blob: a873bcebd3a97898ef9ff6b0e8eeb7a6a6cb51af (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2012 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/extensions/api/notification/notification_api.h"

#include "base/callback.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/common/extensions/extension.h"
#include "googleurl/src/gurl.h"
#include "ui/notifications/notification_types.h"

const char kResultKey[] = "result";

namespace {

const char kNotificationPrefix[] = "extension.api.";

class NotificationApiDelegate : public NotificationDelegate {
 public:
  NotificationApiDelegate(extensions::ApiFunction* api_function,
                          extensions::ApiResourceEventNotifier* event_notifier)
      : api_function_(api_function),
        event_notifier_(event_notifier),
        id_(kNotificationPrefix + base::Uint64ToString(next_id_++)) {
    DCHECK(api_function_);
  }

  virtual void Display() OVERRIDE {
    // TODO(miket): propagate to JS
  }

  virtual void Error() OVERRIDE {
    // TODO(miket): propagate to JS
  }

  virtual void Close(bool by_user) OVERRIDE {
    // TODO(miket): propagate to JS
  }

  virtual void Click() OVERRIDE {
    // TODO(miket): propagate to JS
  }

  virtual std::string id() const OVERRIDE {
    return id_;
  }

  virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
    // We're holding a reference to api_function_, so we know it'll be valid as
    // long as we are, and api_function_ (as a UIThreadExtensionFunction)
    // listens to content::NOTIFICATION_RENDER_VIEW_HOST_DELETED and will
    // properly zero out its copy of render_view_host when the RVH goes away.
    return api_function_->render_view_host();
  }

 private:
  virtual ~NotificationApiDelegate() {}

  scoped_refptr<extensions::ApiFunction> api_function_;
  extensions::ApiResourceEventNotifier* event_notifier_;
  std::string id_;

  static uint64 next_id_;

  DISALLOW_COPY_AND_ASSIGN(NotificationApiDelegate);
};

uint64 NotificationApiDelegate::next_id_ = 0;

}  // namespace

namespace extensions {

NotificationShowFunction::NotificationShowFunction() {
}

NotificationShowFunction::~NotificationShowFunction() {
}

bool NotificationShowFunction::RunImpl() {
  params_ = api::experimental_notification::Show::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  api::experimental_notification::ShowOptions* options = &params_->options;
  scoped_ptr<DictionaryValue> options_dict(options->ToValue());
  src_id_ = ExtractSrcId(options_dict.get());
  event_notifier_ = CreateEventNotifier(src_id_);

  ui::notifications::NotificationType type =
      ui::notifications::StringToNotificationType(options->type);
  GURL icon_url(UTF8ToUTF16(options->icon_url));
  string16 title(UTF8ToUTF16(options->title));
  string16 message(UTF8ToUTF16(options->message));

  scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue());

  if (options->message_intent.get())
    optional_fields->SetString(ui::notifications::kMessageIntentKey,
                               UTF8ToUTF16(*options->message_intent));
  if (options->priority.get())
    optional_fields->SetInteger(ui::notifications::kPriorityKey,
                                *options->priority);
  if (options->timestamp.get())
    optional_fields->SetString(ui::notifications::kTimestampKey,
                               *options->timestamp);
  if (options->second_icon_url.get())
    optional_fields->SetString(ui::notifications::kSecondIconUrlKey,
                               UTF8ToUTF16(*options->second_icon_url));
  if (options->unread_count.get())
    optional_fields->SetInteger(ui::notifications::kUnreadCountKey,
                                *options->unread_count);
  if (options->button_one_title.get())
    optional_fields->SetString(ui::notifications::kButtonOneTitleKey,
                               UTF8ToUTF16(*options->button_one_title));
  if (options->button_one_intent.get())
    optional_fields->SetString(ui::notifications::kButtonOneIntentKey,
                               UTF8ToUTF16(*options->button_one_intent));
  if (options->button_two_title.get())
    optional_fields->SetString(ui::notifications::kButtonTwoTitleKey,
                               UTF8ToUTF16(*options->button_two_title));
  if (options->button_two_intent.get())
    optional_fields->SetString(ui::notifications::kButtonTwoIntentKey,
                               UTF8ToUTF16(*options->button_two_intent));
  if (options->expanded_message.get())
    optional_fields->SetString(ui::notifications::kExpandedMessageKey,
                               UTF8ToUTF16(*options->expanded_message));
  if (options->image_url.get())
    optional_fields->SetString(ui::notifications::kImageUrlKey,
                               UTF8ToUTF16(*options->image_url));

  string16 replace_id(UTF8ToUTF16(options->replace_id));

  Notification notification(type, icon_url, title, message,
                            WebKit::WebTextDirectionDefault,
                            string16(), replace_id,
                            optional_fields.get(),
                            new NotificationApiDelegate(this,
                                                        event_notifier_));
  g_browser_process->notification_ui_manager()->Add(notification, profile());

  // TODO(miket): why return a result if it's always true?
  DictionaryValue* result = new DictionaryValue();
  result->SetBoolean(kResultKey, true);
  SetResult(result);
  SendResponse(true);

  return true;
}

}  // namespace extensions