Index: content/browser/debugger/devtools_browser_target.cc |
diff --git a/content/browser/debugger/devtools_browser_target.cc b/content/browser/debugger/devtools_browser_target.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4188cea822b11efb33f41d6ee4834f257aa7aaab |
--- /dev/null |
+++ b/content/browser/debugger/devtools_browser_target.cc |
@@ -0,0 +1,87 @@ |
+// 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 "content/browser/debugger/devtools_browser_target.h" |
+ |
+#include "base/json/json_reader.h" |
+#include "base/json/json_writer.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
+ |
+ |
+namespace content { |
+ |
+DevToolsBrowserTarget::DevToolsBrowserTarget(int connection_id) |
+ : connection_id_(connection_id) { |
+} |
+ |
+DevToolsBrowserTarget::~DevToolsBrowserTarget() { |
+ for (HandlersMap::iterator i = handlers_.begin(); i != handlers_.end(); ++i) |
+ delete i->second; |
+} |
+ |
+void DevToolsBrowserTarget::RegisterHandler(const std::string& domain, |
+ Handler* handler) { |
+ handlers_[domain] = handler; |
pfeldman
2012/12/14 18:35:05
nit: calling it twice with the same handler will b
bulach
2012/12/14 19:53:39
added a DCHECK
|
+} |
+ |
+std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { |
+ scoped_ptr<base::Value> command(base::JSONReader::Read(data)); |
+ |
+ if (!command || !command->IsType(base::Value::TYPE_DICTIONARY)) |
+ return CreateErrorResponse(-1, "Invalid request"); |
pfeldman
2012/12/14 18:35:05
nit: since we send it to the client, we can use Re
bulach
2012/12/14 19:53:39
Done.
|
+ |
+ int request_id; |
+ std::string domain; |
+ std::string method; |
+ base::DictionaryValue* command_dict = NULL; |
+ command->GetAsDictionary(&command_dict); |
pfeldman
2012/12/14 18:35:05
All these getters can return false. You should ret
bulach
2012/12/14 19:53:39
Done.
|
+ |
+ command_dict->GetInteger("id", &request_id); |
+ command_dict->GetString("method", &method); |
+ |
+ base::DictionaryValue* params = NULL; |
+ command_dict->GetDictionary("params", ¶ms); |
+ size_t pos = method.find("."); |
+ if (pos == std::string::npos) |
+ return CreateErrorResponse(request_id, "Method unsupported"); |
+ |
+ domain = method.substr(0, pos); |
+ if (domain.empty() || handlers_.find(domain) == handlers_.end()) |
+ return CreateErrorResponse(request_id, "Domain unsupported"); |
+ |
+ base::Value* domain_result = handlers_[domain]->OnProtocolCommand( |
+ method, params); |
+ |
+ if (!domain_result) |
+ return CreateErrorResponse(request_id, "Invalid call"); |
pfeldman
2012/12/14 18:35:05
nit: you could pass error_message by reference int
bulach
2012/12/14 19:53:39
good point! done, except by ** rather than ref (st
|
+ |
+ DictionaryValue* response = new DictionaryValue(); |
+ response->Set("result", domain_result); |
+ return CreateResponse(request_id, response); |
+} |
+ |
+std::string DevToolsBrowserTarget::CreateErrorResponse( |
+ int request_id, const std::string& message) { |
+ base::DictionaryValue* error_response = new base::DictionaryValue(); |
+ error_response->SetString("error", message); |
pfeldman
2012/12/14 18:35:05
In fact, error is an object with the code and mess
bulach
2012/12/14 19:53:39
oh, so many layers... :) thanks for pointing this
|
+ return CreateResponse(request_id, error_response); |
+} |
+ |
+std::string DevToolsBrowserTarget::CreateResponse( |
+ int request_id, base::Value* response) { |
+ scoped_ptr<base::DictionaryValue> ret(new base::DictionaryValue()); |
+ ret->SetInteger("id", request_id); |
+ ret->Set("response", response); |
+ |
+ // Serialize response. |
+ std::string json_response; |
+ base::JSONWriter::WriteWithOptions(ret.get(), |
+ base::JSONWriter::OPTIONS_PRETTY_PRINT, |
+ &json_response); |
+ return json_response; |
+} |
+ |
+} // namespace content |