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
|
// 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 = ¶ms_->options;
scoped_ptr<DictionaryValue> options_dict(options->ToValue());
src_id_ = ExtractSrcId(options_dict.get());
event_notifier_ = CreateEventNotifier(src_id_);
GURL icon_url(UTF8ToUTF16(options->icon_url));
string16 title(UTF8ToUTF16(options->title));
string16 message(UTF8ToUTF16(options->message));
// TEMP fields that are here to demonstrate usage of... fields.
// TODO(miket): replace with real fields from BaseFormatView.
string16 extra_field;
if (options->extra_field.get())
extra_field = UTF8ToUTF16(*options->extra_field);
string16 second_extra_field;
if (options->second_extra_field.get())
second_extra_field = UTF8ToUTF16(*options->second_extra_field);
string16 replace_id(UTF8ToUTF16(options->replace_id));
ui::notifications::NotificationType type;
scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue());
// TODO(miket): this is a lazy hacky way to distinguish the old and new
// notification types. Once we have something more than just "old" and "new,"
// we'll probably want to pass the type all the way up from the JS, and then
// we won't need this hack at all.
if (extra_field.empty()) {
type = ui::notifications::NOTIFICATION_TYPE_SIMPLE;
} else {
type = ui::notifications::NOTIFICATION_TYPE_BASE_FORMAT;
optional_fields->SetString(ui::notifications::kExtraFieldKey,
extra_field);
optional_fields->SetString(ui::notifications::kSecondExtraFieldKey,
second_extra_field);
}
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
|