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(const std::string& domain, | |
26 Handler* handler) { | |
27 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
| |
28 } | |
29 | |
30 std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { | |
31 scoped_ptr<base::Value> command(base::JSONReader::Read(data)); | |
32 | |
33 if (!command || !command->IsType(base::Value::TYPE_DICTIONARY)) | |
34 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.
| |
35 | |
36 int request_id; | |
37 std::string domain; | |
38 std::string method; | |
39 base::DictionaryValue* command_dict = NULL; | |
40 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.
| |
41 | |
42 command_dict->GetInteger("id", &request_id); | |
43 command_dict->GetString("method", &method); | |
44 | |
45 base::DictionaryValue* params = NULL; | |
46 command_dict->GetDictionary("params", ¶ms); | |
47 size_t pos = method.find("."); | |
48 if (pos == std::string::npos) | |
49 return CreateErrorResponse(request_id, "Method unsupported"); | |
50 | |
51 domain = method.substr(0, pos); | |
52 if (domain.empty() || handlers_.find(domain) == handlers_.end()) | |
53 return CreateErrorResponse(request_id, "Domain unsupported"); | |
54 | |
55 base::Value* domain_result = handlers_[domain]->OnProtocolCommand( | |
56 method, params); | |
57 | |
58 if (!domain_result) | |
59 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
| |
60 | |
61 DictionaryValue* response = new DictionaryValue(); | |
62 response->Set("result", domain_result); | |
63 return CreateResponse(request_id, response); | |
64 } | |
65 | |
66 std::string DevToolsBrowserTarget::CreateErrorResponse( | |
67 int request_id, const std::string& message) { | |
68 base::DictionaryValue* error_response = new base::DictionaryValue(); | |
69 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
| |
70 return CreateResponse(request_id, error_response); | |
71 } | |
72 | |
73 std::string DevToolsBrowserTarget::CreateResponse( | |
74 int request_id, base::Value* response) { | |
75 scoped_ptr<base::DictionaryValue> ret(new base::DictionaryValue()); | |
76 ret->SetInteger("id", request_id); | |
77 ret->Set("response", response); | |
78 | |
79 // Serialize response. | |
80 std::string json_response; | |
81 base::JSONWriter::WriteWithOptions(ret.get(), | |
82 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
83 &json_response); | |
84 return json_response; | |
85 } | |
86 | |
87 } // namespace content | |
OLD | NEW |