| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/debugger/devtools_browser_target.h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/values.h" | |
| 12 | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 DevToolsBrowserTarget::DevToolsBrowserTarget(int connection_id) | |
| 17 : connection_id_(connection_id) { | |
| 18 } | |
| 19 | |
| 20 DevToolsBrowserTarget::~DevToolsBrowserTarget() { | |
| 21 for (HandlersMap::iterator i = handlers_.begin(); i != handlers_.end(); ++i) | |
| 22 delete i->second; | |
| 23 } | |
| 24 | |
| 25 void DevToolsBrowserTarget::RegisterHandler(Handler* handler) { | |
| 26 std::string domain = handler->Domain(); | |
| 27 DCHECK(handlers_.find(domain) == handlers_.end()); | |
| 28 handlers_[domain] = handler; | |
| 29 } | |
| 30 | |
| 31 std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { | |
| 32 int error_code; | |
| 33 std::string error_message; | |
| 34 scoped_ptr<base::Value> command( | |
| 35 base::JSONReader::ReadAndReturnError( | |
| 36 data, 0, &error_code, &error_message)); | |
| 37 | |
| 38 if (!command || !command->IsType(base::Value::TYPE_DICTIONARY)) | |
| 39 return SerializeErrorResponse( | |
| 40 -1, CreateErrorObject(error_code, error_message)); | |
| 41 | |
| 42 int request_id; | |
| 43 std::string domain; | |
| 44 std::string method; | |
| 45 base::DictionaryValue* command_dict = NULL; | |
| 46 bool ok = true; | |
| 47 ok &= command->GetAsDictionary(&command_dict); | |
| 48 ok &= command_dict->GetInteger("id", &request_id); | |
| 49 ok &= command_dict->GetString("method", &method); | |
| 50 if (!ok) | |
| 51 return SerializeErrorResponse( | |
| 52 request_id, CreateErrorObject(-1, "Malformed request")); | |
| 53 | |
| 54 base::DictionaryValue* params = NULL; | |
| 55 command_dict->GetDictionary("params", ¶ms); | |
| 56 | |
| 57 size_t pos = method.find("."); | |
| 58 if (pos == std::string::npos) | |
| 59 return SerializeErrorResponse( | |
| 60 request_id, CreateErrorObject(-1, "Method unsupported")); | |
| 61 | |
| 62 domain = method.substr(0, pos); | |
| 63 if (domain.empty() || handlers_.find(domain) == handlers_.end()) | |
| 64 return SerializeErrorResponse( | |
| 65 request_id, CreateErrorObject(-1, "Domain unsupported")); | |
| 66 | |
| 67 base::Value* error_object = NULL; | |
| 68 base::Value* domain_result = handlers_[domain]->OnProtocolCommand( | |
| 69 method, params, &error_object); | |
| 70 | |
| 71 if (error_object) | |
| 72 return SerializeErrorResponse(request_id, error_object); | |
| 73 | |
| 74 if (!domain_result) | |
| 75 return SerializeErrorResponse( | |
| 76 request_id, CreateErrorObject(-1, "Invalid call")); | |
| 77 | |
| 78 DictionaryValue* response = new DictionaryValue(); | |
| 79 response->Set("result", domain_result); | |
| 80 return SerializeResponse(request_id, response); | |
| 81 } | |
| 82 | |
| 83 std::string DevToolsBrowserTarget::SerializeErrorResponse( | |
| 84 int request_id, base::Value* error_object) { | |
| 85 scoped_ptr<base::DictionaryValue> error_response(new base::DictionaryValue()); | |
| 86 error_response->SetInteger("id", request_id); | |
| 87 error_response->Set("error", error_object); | |
| 88 // Serialize response. | |
| 89 std::string json_response; | |
| 90 base::JSONWriter::WriteWithOptions(error_response.get(), | |
| 91 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 92 &json_response); | |
| 93 return json_response; | |
| 94 } | |
| 95 | |
| 96 base::Value* DevToolsBrowserTarget::CreateErrorObject( | |
| 97 int error_code, const std::string& message) { | |
| 98 base::DictionaryValue* error_object = new base::DictionaryValue(); | |
| 99 error_object->SetInteger("code", error_code); | |
| 100 error_object->SetString("message", message); | |
| 101 return error_object; | |
| 102 } | |
| 103 | |
| 104 std::string DevToolsBrowserTarget::SerializeResponse( | |
| 105 int request_id, base::Value* response) { | |
| 106 scoped_ptr<base::DictionaryValue> ret(new base::DictionaryValue()); | |
| 107 ret->SetInteger("id", request_id); | |
| 108 ret->Set("response", response); | |
| 109 | |
| 110 // Serialize response. | |
| 111 std::string json_response; | |
| 112 base::JSONWriter::WriteWithOptions(ret.get(), | |
| 113 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 114 &json_response); | |
| 115 return json_response; | |
| 116 } | |
| 117 | |
| 118 } // namespace content | |
| OLD | NEW |