| OLD | NEW |
| (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 #include "chrome/browser/debugger/devtools_remote_service.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/json/json_writer.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/debugger/devtools_manager.h" | |
| 14 #include "chrome/browser/debugger/devtools_protocol_handler.h" | |
| 15 #include "chrome/browser/debugger/devtools_remote_message.h" | |
| 16 #include "chrome/browser/debugger/inspectable_tab_proxy.h" | |
| 17 #include "chrome/browser/sessions/restore_tab_helper.h" | |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 19 #include "content/browser/tab_contents/navigation_controller.h" | |
| 20 #include "content/browser/tab_contents/navigation_entry.h" | |
| 21 #include "content/common/devtools_messages.h" | |
| 22 | |
| 23 const char DevToolsRemoteServiceCommand::kPing[] = "ping"; | |
| 24 const char DevToolsRemoteServiceCommand::kVersion[] = "version"; | |
| 25 const char DevToolsRemoteServiceCommand::kListTabs[] = "list_tabs"; | |
| 26 | |
| 27 const char DevToolsRemoteService::kToolName[] = "DevToolsService"; | |
| 28 | |
| 29 namespace { | |
| 30 const char kCommandKey[] = "command"; | |
| 31 const char kDataKey[] = "data"; | |
| 32 const char kResultKey[] = "result"; | |
| 33 } // namespace | |
| 34 | |
| 35 DevToolsRemoteService::DevToolsRemoteService(DevToolsProtocolHandler* delegate) | |
| 36 : delegate_(delegate) {} | |
| 37 | |
| 38 DevToolsRemoteService::~DevToolsRemoteService() {} | |
| 39 | |
| 40 void DevToolsRemoteService::HandleMessage( | |
| 41 const DevToolsRemoteMessage& message) { | |
| 42 scoped_ptr<Value> request(base::JSONReader::Read(message.content(), false)); | |
| 43 if (request.get() == NULL) { | |
| 44 // Bad JSON | |
| 45 NOTREACHED(); | |
| 46 return; | |
| 47 } | |
| 48 DictionaryValue* json; | |
| 49 if (request->IsType(Value::TYPE_DICTIONARY)) { | |
| 50 json = static_cast<DictionaryValue*>(request.get()); | |
| 51 if (!json->HasKey(kCommandKey)) { | |
| 52 NOTREACHED(); // Broken protocol - no "command" specified | |
| 53 return; | |
| 54 } | |
| 55 } else { | |
| 56 NOTREACHED(); // Broken protocol - not a JS object | |
| 57 return; | |
| 58 } | |
| 59 ProcessJson(json, message); | |
| 60 } | |
| 61 | |
| 62 void DevToolsRemoteService::ProcessJson(DictionaryValue* json, | |
| 63 const DevToolsRemoteMessage& message) { | |
| 64 static const std::string kOkResponse = "ok"; // "Ping" response | |
| 65 static const std::string kVersion = "0.1"; // Current protocol version | |
| 66 std::string command; | |
| 67 DictionaryValue response; | |
| 68 | |
| 69 json->GetString(kCommandKey, &command); | |
| 70 response.SetString(kCommandKey, command); | |
| 71 | |
| 72 if (command == DevToolsRemoteServiceCommand::kPing) { | |
| 73 response.SetInteger(kResultKey, Result::kOk); | |
| 74 response.SetString(kDataKey, kOkResponse); | |
| 75 } else if (command == DevToolsRemoteServiceCommand::kVersion) { | |
| 76 response.SetInteger(kResultKey, Result::kOk); | |
| 77 response.SetString(kDataKey, kVersion); | |
| 78 } else if (command == DevToolsRemoteServiceCommand::kListTabs) { | |
| 79 ListValue* data = new ListValue(); | |
| 80 const InspectableTabProxy::TabMap& tab_map = | |
| 81 delegate_->inspectable_tab_proxy()->tab_map(); | |
| 82 for (InspectableTabProxy::TabMap::const_iterator it = | |
| 83 tab_map.begin(), end = tab_map.end(); it != end; ++it) { | |
| 84 NavigationEntry* entry = it->second->controller().GetActiveEntry(); | |
| 85 if (entry == NULL) { | |
| 86 continue; | |
| 87 } | |
| 88 if (entry->url().is_valid()) { | |
| 89 ListValue* tab = new ListValue(); | |
| 90 tab->Append(Value::CreateIntegerValue( | |
| 91 it->second->restore_tab_helper()->session_id().id())); | |
| 92 tab->Append(Value::CreateStringValue(entry->url().spec())); | |
| 93 data->Append(tab); | |
| 94 } | |
| 95 } | |
| 96 response.SetInteger(kResultKey, Result::kOk); | |
| 97 response.Set(kDataKey, data); | |
| 98 } else { | |
| 99 // Unknown protocol command. | |
| 100 NOTREACHED(); | |
| 101 response.SetInteger(kResultKey, Result::kUnknownCommand); | |
| 102 } | |
| 103 std::string response_json; | |
| 104 base::JSONWriter::Write(&response, false, &response_json); | |
| 105 scoped_ptr<DevToolsRemoteMessage> response_message( | |
| 106 DevToolsRemoteMessageBuilder::instance().Create(message.tool(), | |
| 107 message.destination(), | |
| 108 response_json)); | |
| 109 delegate_->Send(*response_message.get()); | |
| 110 } | |
| OLD | NEW |