Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: content/browser/debugger/extension_ports_remote_service.cc

Issue 7309011: Remove dependencies on extensions from content/browser/debugger. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to head. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Implementation of the ExtensionPortsRemoteService.
6
7 // Inspired significantly from debugger_remote_service
8 // and ../automation/extension_port_container.
9
10 #include "content/browser/debugger/extension_ports_remote_service.h"
11
12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h"
14 #include "base/message_loop.h"
15 #include "base/string_number_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
20 #include "chrome/common/extensions/extension_messages.h"
21 #include "content/browser/debugger/devtools_manager.h"
22 #include "content/browser/debugger/devtools_protocol_handler.h"
23 #include "content/browser/debugger/devtools_remote_message.h"
24 #include "content/browser/debugger/inspectable_tab_proxy.h"
25 #include "content/browser/tab_contents/tab_contents.h"
26 #include "content/common/devtools_messages.h"
27
28 namespace {
29
30 // Protocol is as follows:
31 //
32 // From external client:
33 // {"command": "connect",
34 // "data": {
35 // "extensionId": "<extension_id string>",
36 // "channelName": "<port name string>", (optional)
37 // "tabId": <numerical tab ID> (optional)
38 // }
39 // }
40 // To connect to a background page or tool strip, the tabId should be omitted.
41 // Tab IDs can be enumerated with the list_tabs DevToolsService command.
42 //
43 // Response:
44 // {"command": "connect",
45 // "result": 0, (assuming success)
46 // "data": {
47 // "portId": <numerical port ID>
48 // }
49 // }
50 //
51 // Posting a message from external client:
52 // Put the target message port ID in the devtools destination field.
53 // {"command": "postMessage",
54 // "data": <message body - arbitrary JSON>
55 // }
56 // Response:
57 // {"command": "postMessage",
58 // "result": 0 (Assuming success)
59 // }
60 // Note this is a confirmation from the devtools protocol layer, not
61 // a response from the extension.
62 //
63 // Message from an extension to the external client:
64 // The message port ID is in the devtools destination field.
65 // {"command": "onMessage",
66 // "result": 0, (Always 0)
67 // "data": <message body - arbitrary JSON>
68 // }
69 //
70 // The "disconnect" command from the external client, and
71 // "onDisconnect" notification from the ExtensionMessageService, are
72 // similar: with the message port ID in the destination field, but no
73 // "data" field in this case.
74
75 // Commands:
76 const char kConnect[] = "connect";
77 const char kDisconnect[] = "disconnect";
78 const char kPostMessage[] = "postMessage";
79 // Events:
80 const char kOnMessage[] = "onMessage";
81 const char kOnDisconnect[] = "onDisconnect";
82
83 // Constants for the JSON message fields.
84 // The type is wstring because the constant is used to get a
85 // DictionaryValue field (which requires a wide string).
86
87 // Mandatory.
88 const char kCommandKey[] = "command";
89
90 // Always present in messages sent to the external client.
91 const char kResultKey[] = "result";
92
93 // Field for command-specific parameters. Not strictly necessary, but
94 // makes it more similar to the remote debugger protocol, which should
95 // allow easier reuse of client code.
96 const char kDataKey[] = "data";
97
98 // Fields within the "data" dictionary:
99
100 // Required for "connect":
101 const char kExtensionIdKey[] = "extensionId";
102 // Optional in "connect":
103 const char kChannelNameKey[] = "channelName";
104 const char kTabIdKey[] = "tabId";
105
106 // Present under "data" in replies to a successful "connect" .
107 const char kPortIdKey[] = "portId";
108
109 } // namespace
110
111 const std::string ExtensionPortsRemoteService::kToolName = "ExtensionPorts";
112
113 ExtensionPortsRemoteService::ExtensionPortsRemoteService(
114 DevToolsProtocolHandler* delegate)
115 : delegate_(delegate), service_(NULL) {
116 // We need an ExtensionMessageService instance. It hangs off of
117 // |profile|. But we do not have a particular tab or RenderViewHost
118 // as context. I'll just use the first active profile not in
119 // incognito mode. But this is probably not the right way.
120 ProfileManager* profile_manager = g_browser_process->profile_manager();
121 if (!profile_manager) {
122 LOG(WARNING) << "No profile manager for ExtensionPortsRemoteService";
123 return;
124 }
125
126 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
127 for (size_t i = 0; i < profiles.size(); ++i) {
128 if (!profiles[i]->IsOffTheRecord()) {
129 service_ = profiles[i]->GetExtensionMessageService();
130 break;
131 }
132 }
133 if (!service_)
134 LOG(WARNING) << "No usable profile for ExtensionPortsRemoteService";
135 }
136
137 ExtensionPortsRemoteService::~ExtensionPortsRemoteService() {
138 }
139
140 void ExtensionPortsRemoteService::HandleMessage(
141 const DevToolsRemoteMessage& message) {
142 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
143 const std::string destinationString = message.destination();
144 scoped_ptr<Value> request(base::JSONReader::Read(message.content(), true));
145 if (request.get() == NULL) {
146 // Bad JSON
147 NOTREACHED();
148 return;
149 }
150 DictionaryValue* content;
151 if (!request->IsType(Value::TYPE_DICTIONARY)) {
152 NOTREACHED(); // Broken protocol :(
153 return;
154 }
155 content = static_cast<DictionaryValue*>(request.get());
156 if (!content->HasKey(kCommandKey)) {
157 NOTREACHED(); // Broken protocol :(
158 return;
159 }
160 std::string command;
161 DictionaryValue response;
162
163 content->GetString(kCommandKey, &command);
164 response.SetString(kCommandKey, command);
165
166 if (!service_) {
167 // This happens if we failed to obtain an ExtensionMessageService
168 // during initialization.
169 NOTREACHED();
170 response.SetInteger(kResultKey, RESULT_NO_SERVICE);
171 SendResponse(response, message.tool(), message.destination());
172 return;
173 }
174
175 int destination = -1;
176 if (!destinationString.empty())
177 base::StringToInt(destinationString, &destination);
178
179 if (command == kConnect) {
180 if (destination != -1) // destination should be empty for this command.
181 response.SetInteger(kResultKey, RESULT_UNKNOWN_COMMAND);
182 else
183 ConnectCommand(content, &response);
184 } else if (command == kDisconnect) {
185 if (destination == -1) // Destination required for this command.
186 response.SetInteger(kResultKey, RESULT_UNKNOWN_COMMAND);
187 else
188 DisconnectCommand(destination, &response);
189 } else if (command == kPostMessage) {
190 if (destination == -1) // Destination required for this command.
191 response.SetInteger(kResultKey, RESULT_UNKNOWN_COMMAND);
192 else
193 PostMessageCommand(destination, content, &response);
194 } else {
195 // Unknown command
196 NOTREACHED();
197 response.SetInteger(kResultKey, RESULT_UNKNOWN_COMMAND);
198 }
199 SendResponse(response, message.tool(), message.destination());
200 }
201
202 void ExtensionPortsRemoteService::OnConnectionLost() {
203 VLOG(1) << "OnConnectionLost";
204 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
205 DCHECK(service_);
206 for (PortIdSet::iterator it = openPortIds_.begin();
207 it != openPortIds_.end();
208 ++it)
209 service_->CloseChannel(*it);
210 openPortIds_.clear();
211 }
212
213 void ExtensionPortsRemoteService::SendResponse(
214 const Value& response, const std::string& tool,
215 const std::string& destination) {
216 std::string response_content;
217 base::JSONWriter::Write(&response, false, &response_content);
218 scoped_ptr<DevToolsRemoteMessage> response_message(
219 DevToolsRemoteMessageBuilder::instance().Create(
220 tool, destination, response_content));
221 delegate_->Send(*response_message.get());
222 }
223
224 bool ExtensionPortsRemoteService::Send(IPC::Message *message) {
225 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
226
227 IPC_BEGIN_MESSAGE_MAP(ExtensionPortsRemoteService, *message)
228 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke)
229 IPC_MESSAGE_UNHANDLED_ERROR()
230 IPC_END_MESSAGE_MAP()
231
232 delete message;
233 return true;
234 }
235
236 void ExtensionPortsRemoteService::OnExtensionMessageInvoke(
237 const std::string& extension_id,
238 const std::string& function_name,
239 const ListValue& args,
240 const GURL& event_url) {
241 if (function_name == ExtensionMessageService::kDispatchOnMessage) {
242 DCHECK_EQ(args.GetSize(), 2u);
243 std::string message;
244 int port_id;
245 if (args.GetString(0, &message) && args.GetInteger(1, &port_id))
246 OnExtensionMessage(message, port_id);
247 } else if (function_name == ExtensionMessageService::kDispatchOnDisconnect) {
248 DCHECK_EQ(args.GetSize(), 1u);
249 int port_id;
250 if (args.GetInteger(0, &port_id))
251 OnExtensionPortDisconnected(port_id);
252 } else if (function_name == ExtensionMessageService::kDispatchOnConnect) {
253 // There is no way for this service to be addressed and receive
254 // connections.
255 NOTREACHED() << function_name << " shouldn't be called.";
256 } else {
257 NOTREACHED() << function_name << " shouldn't be called.";
258 }
259 }
260
261 void ExtensionPortsRemoteService::OnExtensionMessage(
262 const std::string& message, int port_id) {
263 VLOG(1) << "Message event: from port " << port_id << ", < " << message << ">";
264 // Transpose the information into a JSON message for the external client.
265 DictionaryValue content;
266 content.SetString(kCommandKey, kOnMessage);
267 content.SetInteger(kResultKey, RESULT_OK);
268 // Turn the stringified message body back into JSON.
269 Value* data = base::JSONReader::Read(message, false);
270 if (!data) {
271 NOTREACHED();
272 return;
273 }
274 content.Set(kDataKey, data);
275 SendResponse(content, kToolName, base::IntToString(port_id));
276 }
277
278 void ExtensionPortsRemoteService::OnExtensionPortDisconnected(int port_id) {
279 VLOG(1) << "Disconnect event for port " << port_id;
280 openPortIds_.erase(port_id);
281 DictionaryValue content;
282 content.SetString(kCommandKey, kOnDisconnect);
283 content.SetInteger(kResultKey, RESULT_OK);
284 SendResponse(content, kToolName, base::IntToString(port_id));
285 }
286
287 void ExtensionPortsRemoteService::ConnectCommand(
288 DictionaryValue* content, DictionaryValue* response) {
289 // Parse out the parameters.
290 DictionaryValue* data;
291 if (!content->GetDictionary(kDataKey, &data)) {
292 response->SetInteger(kResultKey, RESULT_PARAMETER_ERROR);
293 return;
294 }
295 std::string extension_id;
296 if (!data->GetString(kExtensionIdKey, &extension_id)) {
297 response->SetInteger(kResultKey, RESULT_PARAMETER_ERROR);
298 return;
299 }
300 std::string channel_name = "";
301 data->GetString(kChannelNameKey, &channel_name); // optional.
302 int tab_id = -1;
303 data->GetInteger(kTabIdKey, &tab_id); // optional.
304 int port_id;
305 if (tab_id != -1) { // Resolve the tab ID.
306 const InspectableTabProxy::TabMap& tab_map =
307 delegate_->inspectable_tab_proxy()->tab_map();
308 InspectableTabProxy::TabMap::const_iterator it = tab_map.find(tab_id);
309 TabContents* tab_contents = NULL;
310 if (it != tab_map.end())
311 tab_contents = it->second->tab_contents();
312 if (!tab_contents) {
313 VLOG(1) << "tab not found: " << tab_id;
314 response->SetInteger(kResultKey, RESULT_TAB_NOT_FOUND);
315 return;
316 }
317 // Ask the ExtensionMessageService to open the channel.
318 VLOG(1) << "Connect: extension_id <" << extension_id
319 << ">, channel_name <" << channel_name
320 << ">, tab " << tab_id;
321 DCHECK(service_);
322 port_id = service_->OpenSpecialChannelToTab(
323 extension_id, channel_name, tab_contents, this);
324 } else { // no tab: channel to an extension' background page / toolstrip.
325 // Ask the ExtensionMessageService to open the channel.
326 VLOG(1) << "Connect: extension_id <" << extension_id
327 << ">, channel_name <" << channel_name << ">";
328 DCHECK(service_);
329 port_id = service_->OpenSpecialChannelToExtension(
330 extension_id, channel_name, "null", this);
331 }
332 if (port_id == -1) {
333 // Failure: probably the extension ID doesn't exist.
334 VLOG(1) << "Connect failed";
335 response->SetInteger(kResultKey, RESULT_CONNECT_FAILED);
336 return;
337 }
338 VLOG(1) << "Connected: port " << port_id;
339 openPortIds_.insert(port_id);
340 // Reply to external client with the port ID assigned to the new channel.
341 DictionaryValue* reply_data = new DictionaryValue();
342 reply_data->SetInteger(kPortIdKey, port_id);
343 response->Set(kDataKey, reply_data);
344 response->SetInteger(kResultKey, RESULT_OK);
345 }
346
347 void ExtensionPortsRemoteService::DisconnectCommand(
348 int port_id, DictionaryValue* response) {
349 VLOG(1) << "Disconnect port " << port_id;
350 PortIdSet::iterator portEntry = openPortIds_.find(port_id);
351 if (portEntry == openPortIds_.end()) { // unknown port ID.
352 VLOG(1) << "unknown port: " << port_id;
353 response->SetInteger(kResultKey, RESULT_UNKNOWN_PORT);
354 return;
355 }
356 DCHECK(service_);
357 service_->CloseChannel(port_id);
358 openPortIds_.erase(portEntry);
359 response->SetInteger(kResultKey, RESULT_OK);
360 }
361
362 void ExtensionPortsRemoteService::PostMessageCommand(
363 int port_id, DictionaryValue* content, DictionaryValue* response) {
364 Value* data;
365 if (!content->Get(kDataKey, &data)) {
366 response->SetInteger(kResultKey, RESULT_PARAMETER_ERROR);
367 return;
368 }
369 std::string message;
370 // Stringified the JSON message body.
371 base::JSONWriter::Write(data, false, &message);
372 VLOG(1) << "postMessage: port " << port_id
373 << ", message: <" << message << ">";
374 PortIdSet::iterator portEntry = openPortIds_.find(port_id);
375 if (portEntry == openPortIds_.end()) { // Unknown port ID.
376 VLOG(1) << "unknown port: " << port_id;
377 response->SetInteger(kResultKey, RESULT_UNKNOWN_PORT);
378 return;
379 }
380 // Post the message through the ExtensionMessageService.
381 DCHECK(service_);
382 service_->PostMessageFromRenderer(port_id, message);
383 // Confirm to the external client that we sent its message.
384 response->SetInteger(kResultKey, RESULT_OK);
385 }
OLDNEW
« no previous file with comments | « content/browser/debugger/extension_ports_remote_service.h ('k') | content/browser/mock_content_browser_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698