OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" | 5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
14 #include "chrome/browser/extensions/web_intent_callbacks.h" | |
15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_intents_dispatcher.h" | |
19 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
20 #include "webkit/glue/web_intent_data.h" | |
21 | 18 |
22 namespace extensions { | 19 namespace extensions { |
23 | 20 |
24 namespace { | 21 namespace { |
25 | 22 |
26 const char kIntentIdKey[] = "intentId"; | |
27 const char kIntentSuccessKey[] = "success"; | |
28 const char kIntentDataKey[] = "data"; | |
29 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; | 23 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; |
30 const char kOnRestartedEvent[] = "app.runtime.onRestarted"; | 24 const char kOnRestartedEvent[] = "app.runtime.onRestarted"; |
31 | 25 |
32 const char kCallbackNotFoundError[] = | |
33 "WebIntent callback not found; perhaps already responded to"; | |
34 | |
35 void DispatchOnLaunchedEventImpl(const std::string& extension_id, | 26 void DispatchOnLaunchedEventImpl(const std::string& extension_id, |
36 scoped_ptr<base::ListValue> args, | 27 scoped_ptr<base::ListValue> args, |
37 Profile* profile) { | 28 Profile* profile) { |
38 extensions::ExtensionSystem* system = | 29 extensions::ExtensionSystem* system = |
39 extensions::ExtensionSystem::Get(profile); | 30 extensions::ExtensionSystem::Get(profile); |
40 // Special case: normally, extensions add their own lazy event listeners. | 31 // Special case: normally, extensions add their own lazy event listeners. |
41 // However, since the extension might have just been enabled, it hasn't had a | 32 // However, since the extension might have just been enabled, it hasn't had a |
42 // chance to register for events. So we register on its behalf. If the | 33 // chance to register for events. So we register on its behalf. If the |
43 // extension does not actually have a listener, the event will just be | 34 // extension does not actually have a listener, the event will just be |
44 // ignored (but an app that doesn't listen for the onLaunched event doesn't | 35 // ignored (but an app that doesn't listen for the onLaunched event doesn't |
(...skipping 20 matching lines...) Expand all Loading... |
65 Profile* profile, const Extension* extension) { | 56 Profile* profile, const Extension* extension) { |
66 scoped_ptr<ListValue> arguments(new ListValue()); | 57 scoped_ptr<ListValue> arguments(new ListValue()); |
67 scoped_ptr<Event> event(new Event(kOnRestartedEvent, arguments.Pass())); | 58 scoped_ptr<Event> event(new Event(kOnRestartedEvent, arguments.Pass())); |
68 event->restrict_to_profile = profile; | 59 event->restrict_to_profile = profile; |
69 extensions::ExtensionSystem::Get(profile)->event_router()-> | 60 extensions::ExtensionSystem::Get(profile)->event_router()-> |
70 DispatchEventToExtension(extension->id(), event.Pass()); | 61 DispatchEventToExtension(extension->id(), event.Pass()); |
71 } | 62 } |
72 | 63 |
73 // static. | 64 // static. |
74 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 65 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
75 Profile* profile, const Extension* extension, const string16& action, | 66 Profile* profile, const Extension* extension, |
76 const std::string& handler_id, const std::string& mime_type, | 67 const std::string& handler_id, const std::string& mime_type, |
77 const std::string& file_system_id, const std::string& base_name) { | 68 const std::string& file_system_id, const std::string& base_name) { |
78 scoped_ptr<ListValue> args(new ListValue()); | 69 scoped_ptr<ListValue> args(new ListValue()); |
79 DictionaryValue* launch_data = new DictionaryValue(); | 70 DictionaryValue* launch_data = new DictionaryValue(); |
80 launch_data->SetString("id", handler_id); | 71 launch_data->SetString("id", handler_id); |
81 DictionaryValue* launch_item = new DictionaryValue; | 72 DictionaryValue* launch_item = new DictionaryValue; |
82 launch_item->SetString("fileSystemId", file_system_id); | 73 launch_item->SetString("fileSystemId", file_system_id); |
83 launch_item->SetString("baseName", base_name); | 74 launch_item->SetString("baseName", base_name); |
84 launch_item->SetString("mimeType", mime_type); | 75 launch_item->SetString("mimeType", mime_type); |
85 ListValue* items = new ListValue; | 76 ListValue* items = new ListValue; |
86 items->Append(launch_item); | 77 items->Append(launch_item); |
87 launch_data->Set("items", items); | 78 launch_data->Set("items", items); |
88 args->Append(launch_data); | 79 args->Append(launch_data); |
89 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); | 80 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); |
90 } | 81 } |
91 | 82 |
92 // static. | |
93 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | |
94 Profile* profile, const Extension* extension, | |
95 content::WebIntentsDispatcher* intents_dispatcher, | |
96 content::WebContents* source) { | |
97 #if defined(ENABLE_WEB_INTENTS) | |
98 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent(); | |
99 scoped_ptr<ListValue> args(new ListValue()); | |
100 DictionaryValue* launch_data = new DictionaryValue(); | |
101 DictionaryValue* intent = new DictionaryValue(); | |
102 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); | |
103 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); | |
104 launch_data->Set("intent", intent); | |
105 args->Append(launch_data); | |
106 DictionaryValue* intent_data; | |
107 switch (web_intent_data.data_type) { | |
108 case webkit_glue::WebIntentData::SERIALIZED: | |
109 intent_data = new DictionaryValue(); | |
110 intent_data->SetString("format", "serialized"); | |
111 intent_data->SetString("data", UTF16ToUTF8(web_intent_data.data)); | |
112 // NOTE: This second argument is dropped before being dispatched to the | |
113 // client code. | |
114 args->Append(intent_data); | |
115 break; | |
116 case webkit_glue::WebIntentData::UNSERIALIZED: | |
117 args->Append(Value::CreateNullValue()); | |
118 intent->SetString("data", UTF16ToUTF8(web_intent_data.unserialized_data)); | |
119 break; | |
120 case webkit_glue::WebIntentData::BLOB: | |
121 intent_data = new DictionaryValue(); | |
122 intent_data->SetString("format", "blob"); | |
123 intent_data->SetString("blobFileName", web_intent_data.blob_file.value()); | |
124 intent_data->SetString("blobLength", | |
125 base::Int64ToString(web_intent_data.blob_length)); | |
126 // NOTE: This second argument is dropped before being dispatched to the | |
127 // client code. | |
128 args->Append(intent_data); | |
129 break; | |
130 case webkit_glue::WebIntentData::FILESYSTEM: | |
131 intent_data = new DictionaryValue(); | |
132 intent_data->SetString("format", "filesystem"); | |
133 intent_data->SetString("fileSystemId", web_intent_data.filesystem_id); | |
134 intent_data->SetString("baseName", web_intent_data.root_name); | |
135 args->Append(intent_data); | |
136 break; | |
137 default: | |
138 NOTREACHED(); | |
139 break; | |
140 } | |
141 DCHECK(args->GetSize() == 2); // intent_id must be our third argument. | |
142 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile); | |
143 int intent_id = | |
144 callbacks->RegisterCallback(extension, intents_dispatcher, source); | |
145 args->Append(base::Value::CreateIntegerValue(intent_id)); | |
146 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); | |
147 #endif | |
148 } | |
149 | |
150 bool AppRuntimePostIntentResponseFunction::RunImpl() { | 83 bool AppRuntimePostIntentResponseFunction::RunImpl() { |
151 #if defined(ENABLE_WEB_INTENTS) | |
152 DictionaryValue* details = NULL; | |
153 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
154 | |
155 int intent_id = 0; | |
156 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id)); | |
157 | |
158 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile()); | |
159 content::WebIntentsDispatcher* intents_dispatcher = | |
160 callbacks->RetrieveCallback(GetExtension(), intent_id); | |
161 if (!intents_dispatcher) { | |
162 error_ = kCallbackNotFoundError; | |
163 return false; | |
164 } | |
165 | |
166 webkit_glue::WebIntentReplyType reply_type = | |
167 webkit_glue::WEB_INTENT_REPLY_FAILURE; | |
168 bool success; | |
169 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIntentSuccessKey, &success)); | |
170 if (success) | |
171 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS; | |
172 | |
173 std::string data; | |
174 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data)); | |
175 | |
176 intents_dispatcher->SendReply(webkit_glue::WebIntentReply( | |
177 reply_type, UTF8ToUTF16(data))); | |
178 | |
179 return true; | |
180 #else | |
181 return false; | 84 return false; |
182 #endif | |
183 } | 85 } |
184 | 86 |
185 } // namespace extensions | 87 } // namespace extensions |
OLD | NEW |