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
158
159
160
161
162
163
|
// 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/app_runtime/app_runtime_api.h"
#include "base/json/json_writer.h"
#include "base/string16.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/web_intent_callbacks.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_intents_dispatcher.h"
#include "googleurl/src/gurl.h"
#include "webkit/glue/web_intent_data.h"
namespace {
const char kIntentIdKey[] = "intentId";
const char kIntentSuccessKey[] = "success";
const char kIntentDataKey[] = "data";
const char kOnLaunchedEvent[] = "app.runtime.onLaunched";
const char kOnRestartedEvent[] = "app.runtime.onRestarted";
const char kCallbackNotFoundError[] =
"WebIntent callback not found; perhaps already responded to";
} // anonymous namespace
namespace extensions {
// static.
void AppEventRouter::DispatchOnLaunchedEvent(
Profile* profile, const Extension* extension) {
scoped_ptr<ListValue> arguments(new ListValue());
extensions::ExtensionSystem::Get(profile)->event_router()->
DispatchEventToExtension(extension->id(), kOnLaunchedEvent,
arguments.Pass(), profile, GURL());
}
// static.
void AppEventRouter::DispatchOnRestartedEvent(
Profile* profile, const Extension* extension) {
scoped_ptr<ListValue> arguments(new ListValue());
extensions::ExtensionSystem::Get(profile)->event_router()->
DispatchEventToExtension(extension->id(), kOnRestartedEvent,
arguments.Pass(), profile, GURL());
}
// static.
void AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
Profile* profile, const Extension* extension, const string16& action,
const std::string& handler_id, const std::string& mime_type,
const std::string& file_system_id, const std::string& base_name) {
scoped_ptr<ListValue> args(new ListValue());
DictionaryValue* launch_data = new DictionaryValue();
launch_data->SetString("id", handler_id);
DictionaryValue* launch_item = new DictionaryValue;
launch_item->SetString("fileSystemId", file_system_id);
launch_item->SetString("baseName", base_name);
launch_item->SetString("mimeType", mime_type);
ListValue* items = new ListValue;
items->Append(launch_item);
launch_data->Set("items", items);
args->Append(launch_data);
extensions::ExtensionSystem::Get(profile)->event_router()->
DispatchEventToExtension(extension->id(), kOnLaunchedEvent, args.Pass(),
profile, GURL());
}
// static.
void AppEventRouter::DispatchOnLaunchedEventWithWebIntent(
Profile* profile, const Extension* extension,
content::WebIntentsDispatcher* intents_dispatcher,
content::WebContents* source) {
webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent();
scoped_ptr<ListValue> args(new ListValue());
DictionaryValue* launch_data = new DictionaryValue();
DictionaryValue* intent = new DictionaryValue();
intent->SetString("action", UTF16ToUTF8(web_intent_data.action));
intent->SetString("type", UTF16ToUTF8(web_intent_data.type));
launch_data->Set("intent", intent);
args->Append(launch_data);
DictionaryValue* intent_data;
switch (web_intent_data.data_type) {
case webkit_glue::WebIntentData::SERIALIZED:
intent_data = new DictionaryValue();
intent_data->SetString("format", "serialized");
intent_data->SetString("data", UTF16ToUTF8(web_intent_data.data));
// NOTE: This second argument is dropped before being dispatched to the
// client code.
args->Append(intent_data);
break;
case webkit_glue::WebIntentData::UNSERIALIZED:
args->Append(Value::CreateNullValue());
intent->SetString("data", UTF16ToUTF8(web_intent_data.unserialized_data));
break;
case webkit_glue::WebIntentData::BLOB:
intent_data = new DictionaryValue();
intent_data->SetString("format", "blob");
intent_data->SetString("blobFileName", web_intent_data.blob_file.value());
intent_data->SetString("blobLength",
base::Int64ToString(web_intent_data.blob_length));
// NOTE: This second argument is dropped before being dispatched to the
// client code.
args->Append(intent_data);
break;
case webkit_glue::WebIntentData::FILESYSTEM:
intent_data = new DictionaryValue();
intent_data->SetString("format", "filesystem");
intent_data->SetString("fileSystemId", web_intent_data.filesystem_id);
intent_data->SetString("baseName", web_intent_data.root_name);
args->Append(intent_data);
break;
default:
NOTREACHED();
break;
}
DCHECK(args->GetSize() == 2); // intent_id must be our third argument.
WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile);
int intent_id =
callbacks->RegisterCallback(extension, intents_dispatcher, source);
args->Append(base::Value::CreateIntegerValue(intent_id));
extensions::ExtensionSystem::Get(profile)->event_router()->
DispatchEventToExtension(extension->id(), kOnLaunchedEvent, args.Pass(),
profile, GURL());
}
bool AppRuntimePostIntentResponseFunction::RunImpl() {
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
int intent_id = 0;
EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id));
WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile());
content::WebIntentsDispatcher* intents_dispatcher =
callbacks->RetrieveCallback(GetExtension(), intent_id);
if (!intents_dispatcher) {
error_ = kCallbackNotFoundError;
return false;
}
webkit_glue::WebIntentReplyType reply_type =
webkit_glue::WEB_INTENT_REPLY_FAILURE;
bool success;
EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIntentSuccessKey, &success));
if (success)
reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS;
std::string data;
EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data));
intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data));
return true;
}
} // namespace extensions
|