Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/chromedriver/devtools_client_impl.h" | 5 #include "chrome/test/chromedriver/devtools_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 DevToolsClientImpl::DevToolsClientImpl( | 37 DevToolsClientImpl::DevToolsClientImpl( |
| 38 const SyncWebSocketFactory& factory, | 38 const SyncWebSocketFactory& factory, |
| 39 const std::string& url, | 39 const std::string& url, |
| 40 const ParserFunc& parser_func) | 40 const ParserFunc& parser_func) |
| 41 : socket_(factory.Run().Pass()), | 41 : socket_(factory.Run().Pass()), |
| 42 url_(url), | 42 url_(url), |
| 43 parser_func_(parser_func), | 43 parser_func_(parser_func), |
| 44 connected_(false), | 44 connected_(false), |
| 45 next_id_(1) {} | 45 next_id_(1) {} |
| 46 | 46 |
| 47 DevToolsClientImpl::~DevToolsClientImpl() {} | 47 DevToolsClientImpl::~DevToolsClientImpl() { |
| 48 for (ResponseMap::iterator iter = cmd_response_map_.begin(); | |
| 49 iter != cmd_response_map_.end(); ++iter) { | |
| 50 LOG(WARNING) << "Finished with no response for command " << iter->first; | |
| 51 delete iter->second; | |
| 52 } | |
| 53 } | |
| 48 | 54 |
| 49 Status DevToolsClientImpl::SendCommand( | 55 Status DevToolsClientImpl::SendCommand( |
| 50 const std::string& method, | 56 const std::string& method, |
| 51 const base::DictionaryValue& params) { | 57 const base::DictionaryValue& params) { |
| 52 scoped_ptr<base::DictionaryValue> result; | 58 scoped_ptr<base::DictionaryValue> result; |
| 53 return SendCommandInternal(method, params, &result); | 59 return SendCommandInternal(method, params, &result); |
| 54 } | 60 } |
| 55 | 61 |
| 56 Status DevToolsClientImpl::SendCommandAndGetResult( | 62 Status DevToolsClientImpl::SendCommandAndGetResult( |
| 57 const std::string& method, | 63 const std::string& method, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 connected_ = true; | 105 connected_ = true; |
| 100 } | 106 } |
| 101 | 107 |
| 102 int command_id = next_id_++; | 108 int command_id = next_id_++; |
| 103 base::DictionaryValue command; | 109 base::DictionaryValue command; |
| 104 command.SetInteger("id", command_id); | 110 command.SetInteger("id", command_id); |
| 105 command.SetString("method", method); | 111 command.SetString("method", method); |
| 106 command.Set("params", params.DeepCopy()); | 112 command.Set("params", params.DeepCopy()); |
| 107 std::string message; | 113 std::string message; |
| 108 base::JSONWriter::Write(&command, &message); | 114 base::JSONWriter::Write(&command, &message); |
| 115 LOG(INFO) << "SEND: " << message; | |
|
craigdh
2013/01/30 01:44:24
Whoops. Will delete in the next patch.
| |
| 109 if (!socket_->Send(message)) { | 116 if (!socket_->Send(message)) { |
| 110 connected_ = false; | 117 connected_ = false; |
| 111 return Status(kDisconnected, "unable to send message to renderer"); | 118 return Status(kDisconnected, "unable to send message to renderer"); |
| 112 } | 119 } |
| 113 | 120 |
| 114 return ReceiveCommandResponse(command_id, result); | 121 return ReceiveCommandResponse(command_id, result); |
| 115 } | 122 } |
| 116 | 123 |
| 117 Status DevToolsClientImpl::ReceiveCommandResponse( | 124 Status DevToolsClientImpl::ReceiveCommandResponse( |
| 118 int command_id, | 125 int command_id, |
| 119 scoped_ptr<base::DictionaryValue>* result) { | 126 scoped_ptr<base::DictionaryValue>* result) { |
| 120 internal::InspectorMessageType type; | 127 internal::InspectorMessageType type; |
| 121 internal::InspectorEvent event; | 128 internal::InspectorEvent event; |
| 122 internal::InspectorCommandResponse response; | 129 internal::InspectorCommandResponse response; |
| 123 while (true) { | 130 cmd_response_map_[command_id] = NULL; |
| 131 while (cmd_response_map_[command_id] == NULL) { | |
| 124 Status status = ReceiveNextMessage(command_id, &type, &event, &response); | 132 Status status = ReceiveNextMessage(command_id, &type, &event, &response); |
| 125 if (status.IsError()) { | 133 if (status.IsError()) { |
| 126 return status; | 134 return status; |
| 127 } else if (type == internal::kCommandResponseMessageType) { | 135 } else if (type == internal::kCommandResponseMessageType) { |
| 128 if (response.id != command_id) { | 136 if (cmd_response_map_.count(response.id) == 0) { |
| 129 return Status(kUnknownError, | 137 return Status(kUnknownError, |
| 130 "received response for unknown command ID"); | 138 "received response for unknown command ID"); |
| 139 } else if (response.result) { | |
| 140 cmd_response_map_[response.id] = response.result.release(); | |
| 141 } else { | |
| 142 return Status(kUnknownError, "inspector error: " + response.error); | |
| 131 } | 143 } |
| 132 if (response.result) { | |
| 133 result->reset(response.result.release()); | |
| 134 return Status(kOk); | |
| 135 } | |
| 136 return Status(kUnknownError, "inspector error: " + response.error); | |
| 137 } | 144 } |
| 138 } | 145 } |
| 146 result->reset(cmd_response_map_[command_id]); | |
| 147 cmd_response_map_.erase(command_id); | |
| 148 return Status(kOk); | |
| 139 } | 149 } |
| 140 | 150 |
| 141 Status DevToolsClientImpl::ReceiveNextMessage( | 151 Status DevToolsClientImpl::ReceiveNextMessage( |
| 142 int expected_id, | 152 int expected_id, |
| 143 internal::InspectorMessageType* type, | 153 internal::InspectorMessageType* type, |
| 144 internal::InspectorEvent* event, | 154 internal::InspectorEvent* event, |
| 145 internal::InspectorCommandResponse* response) { | 155 internal::InspectorCommandResponse* response) { |
| 146 std::string message; | 156 std::string message; |
| 147 if (!socket_->ReceiveNextMessage(&message)) { | 157 if (!socket_->ReceiveNextMessage(&message)) { |
| 148 connected_ = false; | 158 connected_ = false; |
| 149 return Status(kDisconnected, | 159 return Status(kDisconnected, |
| 150 "unable to receive message from renderer"); | 160 "unable to receive message from renderer"); |
| 151 } | 161 } |
| 152 if (!parser_func_.Run(message, expected_id, type, event, response)) | 162 if (!parser_func_.Run(message, expected_id, type, event, response)) |
| 153 return Status(kUnknownError, "bad inspector message: " + message); | 163 return Status(kUnknownError, "bad inspector message: " + message); |
| 164 LOG(INFO) << "RECV: " << message; | |
|
craigdh
2013/01/30 01:44:24
ditto.
| |
| 154 if (*type == internal::kEventMessageType) | 165 if (*type == internal::kEventMessageType) |
| 155 return NotifyEventListeners(event->method, *event->params); | 166 return NotifyEventListeners(event->method, *event->params); |
| 156 return Status(kOk); | 167 return Status(kOk); |
| 157 } | 168 } |
| 158 | 169 |
| 159 Status DevToolsClientImpl::NotifyEventListeners( | 170 Status DevToolsClientImpl::NotifyEventListeners( |
| 160 const std::string& method, | 171 const std::string& method, |
| 161 const base::DictionaryValue& params) { | 172 const base::DictionaryValue& params) { |
| 162 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); | 173 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); |
| 163 iter != listeners_.end(); ++iter) { | 174 iter != listeners_.end(); ++iter) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 if (unscoped_result) | 221 if (unscoped_result) |
| 211 command_response->result.reset(unscoped_result->DeepCopy()); | 222 command_response->result.reset(unscoped_result->DeepCopy()); |
| 212 else | 223 else |
| 213 base::JSONWriter::Write(unscoped_error, &command_response->error); | 224 base::JSONWriter::Write(unscoped_error, &command_response->error); |
| 214 return true; | 225 return true; |
| 215 } | 226 } |
| 216 return false; | 227 return false; |
| 217 } | 228 } |
| 218 | 229 |
| 219 } // namespace internal | 230 } // namespace internal |
| OLD | NEW |