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/logging.h" | 10 #include "base/logging.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 InspectorEvent::~InspectorEvent() {} | 41 InspectorEvent::~InspectorEvent() {} |
| 42 | 42 |
| 43 InspectorCommandResponse::InspectorCommandResponse() {} | 43 InspectorCommandResponse::InspectorCommandResponse() {} |
| 44 | 44 |
| 45 InspectorCommandResponse::~InspectorCommandResponse() {} | 45 InspectorCommandResponse::~InspectorCommandResponse() {} |
| 46 | 46 |
| 47 } // namespace internal | 47 } // namespace internal |
| 48 | 48 |
| 49 DevToolsClientImpl::DevToolsClientImpl( | 49 DevToolsClientImpl::DevToolsClientImpl( |
| 50 const SyncWebSocketFactory& factory, | 50 const SyncWebSocketFactory& factory, |
| 51 const std::string& url) | 51 const std::string& url, |
| 52 const FrontendCloserFunc& frontend_closer_func) | |
| 52 : socket_(factory.Run().Pass()), | 53 : socket_(factory.Run().Pass()), |
| 53 url_(url), | 54 url_(url), |
| 55 frontend_closer_func_(frontend_closer_func), | |
| 54 parser_func_(base::Bind(&internal::ParseInspectorMessage)), | 56 parser_func_(base::Bind(&internal::ParseInspectorMessage)), |
| 55 connected_(false), | 57 connected_(false), |
| 56 next_id_(1) {} | 58 next_id_(1) {} |
| 57 | 59 |
| 58 DevToolsClientImpl::DevToolsClientImpl( | 60 DevToolsClientImpl::DevToolsClientImpl( |
| 59 const SyncWebSocketFactory& factory, | 61 const SyncWebSocketFactory& factory, |
| 60 const std::string& url, | 62 const std::string& url, |
| 63 const FrontendCloserFunc& frontend_closer_func, | |
| 61 const ParserFunc& parser_func) | 64 const ParserFunc& parser_func) |
| 62 : socket_(factory.Run().Pass()), | 65 : socket_(factory.Run().Pass()), |
| 63 url_(url), | 66 url_(url), |
| 67 frontend_closer_func_(frontend_closer_func), | |
| 64 parser_func_(parser_func), | 68 parser_func_(parser_func), |
| 65 connected_(false), | 69 connected_(false), |
| 66 next_id_(1) {} | 70 next_id_(1) {} |
| 67 | 71 |
| 68 DevToolsClientImpl::~DevToolsClientImpl() { | 72 DevToolsClientImpl::~DevToolsClientImpl() { |
| 69 for (ResponseMap::iterator iter = cmd_response_map_.begin(); | 73 for (ResponseMap::iterator iter = cmd_response_map_.begin(); |
| 70 iter != cmd_response_map_.end(); ++iter) { | 74 iter != cmd_response_map_.end(); ++iter) { |
| 71 LOG(WARNING) << "Finished with no response for command " << iter->first; | 75 LOG(WARNING) << "Finished with no response for command " << iter->first; |
| 72 delete iter->second; | 76 delete iter->second; |
| 73 } | 77 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 internal::InspectorCommandResponse response; | 115 internal::InspectorCommandResponse response; |
| 112 | 116 |
| 113 while (socket_->HasNextMessage() || !conditional_func.Run()) { | 117 while (socket_->HasNextMessage() || !conditional_func.Run()) { |
| 114 Status status = ReceiveNextMessage(-1, &type, &event, &response); | 118 Status status = ReceiveNextMessage(-1, &type, &event, &response); |
| 115 if (status.IsError()) | 119 if (status.IsError()) |
| 116 return status; | 120 return status; |
| 117 } | 121 } |
| 118 return Status(kOk); | 122 return Status(kOk); |
| 119 } | 123 } |
| 120 | 124 |
| 121 Status DevToolsClientImpl::SendCommandInternal( | 125 Status DevToolsClientImpl::SendCommandToDevTools( |
|
kkania
2013/02/22 01:35:24
the naming of SendCommandToDevTools and SendComman
chrisgao (Use stgao instead)
2013/02/27 19:29:44
Change SendCommandInternal to SendCommandWithRetry
| |
| 122 const std::string& method, | 126 const std::string& method, |
| 123 const base::DictionaryValue& params, | 127 const base::DictionaryValue& params, |
| 124 scoped_ptr<base::DictionaryValue>* result) { | 128 scoped_ptr<base::DictionaryValue>* result, |
| 129 int* command_id) { | |
| 125 if (!connected_) { | 130 if (!connected_) { |
| 126 if (!socket_->Connect(url_)) | 131 if (!socket_->Connect(url_)) |
| 127 return Status(kDisconnected, "unable to connect to renderer"); | 132 return Status(kDisconnected, "unable to connect to renderer"); |
| 128 connected_ = true; | 133 connected_ = true; |
| 129 | 134 |
| 130 // OnConnected notification will be sent out in method ReceiveNextMessage. | 135 // OnConnected notification will be sent out in method ReceiveNextMessage. |
| 131 listeners_for_on_connected_ = listeners_; | 136 listeners_for_on_connected_ = listeners_; |
| 132 } | 137 } |
| 133 | 138 |
| 134 int command_id = next_id_++; | 139 int command_id_tmp = next_id_++; |
| 135 base::DictionaryValue command; | 140 base::DictionaryValue command; |
| 136 command.SetInteger("id", command_id); | 141 command.SetInteger("id", command_id_tmp); |
| 137 command.SetString("method", method); | 142 command.SetString("method", method); |
| 138 command.Set("params", params.DeepCopy()); | 143 command.Set("params", params.DeepCopy()); |
| 139 std::string message; | 144 std::string message; |
| 140 base::JSONWriter::Write(&command, &message); | 145 base::JSONWriter::Write(&command, &message); |
| 141 if (!socket_->Send(message)) { | 146 if (!socket_->Send(message)) { |
| 142 connected_ = false; | 147 connected_ = false; |
| 143 return Status(kDisconnected, "unable to send message to renderer"); | 148 return Status(kDisconnected, "unable to send message to renderer"); |
| 144 } | 149 } |
| 150 *command_id = command_id_tmp; | |
| 151 return Status(kOk); | |
| 152 } | |
| 153 | |
| 154 Status DevToolsClientImpl::SendCommandInternal( | |
| 155 const std::string& method, | |
| 156 const base::DictionaryValue& params, | |
| 157 scoped_ptr<base::DictionaryValue>* result) { | |
| 158 int command_id = -1; | |
| 159 Status status = SendCommandToDevTools(method, params, result, &command_id); | |
|
kkania
2013/02/22 01:35:24
have you tried this manually? can you try sending
chrisgao (Use stgao instead)
2013/02/27 19:29:44
If DevTools frontend is opened during re-connectio
| |
| 160 if (status.code() == kDisconnected) { | |
| 161 // Try to close devtools frontend UI and then reconnect. | |
| 162 status = frontend_closer_func_.Run(); | |
| 163 if (status.IsOk()) | |
| 164 status = SendCommandToDevTools(method, params, result, &command_id); | |
| 165 } | |
| 166 if (status.IsError()) | |
| 167 return Status(kDisconnected, "failed to reconnect to DevTools"); | |
|
kkania
2013/02/22 01:35:24
this error message may not be accurate, right? thi
chrisgao (Use stgao instead)
2013/02/27 19:29:44
Oh, yes. Updated message and including the old sta
| |
| 145 return ReceiveCommandResponse(command_id, result); | 168 return ReceiveCommandResponse(command_id, result); |
| 146 } | 169 } |
| 147 | 170 |
| 148 Status DevToolsClientImpl::ReceiveCommandResponse( | 171 Status DevToolsClientImpl::ReceiveCommandResponse( |
| 149 int command_id, | 172 int command_id, |
| 150 scoped_ptr<base::DictionaryValue>* result) { | 173 scoped_ptr<base::DictionaryValue>* result) { |
| 151 internal::InspectorMessageType type; | 174 internal::InspectorMessageType type; |
| 152 internal::InspectorEvent event; | 175 internal::InspectorEvent event; |
| 153 internal::InspectorCommandResponse response; | 176 internal::InspectorCommandResponse response; |
| 154 cmd_response_map_[command_id] = NULL; | 177 cmd_response_map_[command_id] = NULL; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 if (unscoped_result) | 284 if (unscoped_result) |
| 262 command_response->result.reset(unscoped_result->DeepCopy()); | 285 command_response->result.reset(unscoped_result->DeepCopy()); |
| 263 else | 286 else |
| 264 base::JSONWriter::Write(unscoped_error, &command_response->error); | 287 base::JSONWriter::Write(unscoped_error, &command_response->error); |
| 265 return true; | 288 return true; |
| 266 } | 289 } |
| 267 return false; | 290 return false; |
| 268 } | 291 } |
| 269 | 292 |
| 270 } // namespace internal | 293 } // namespace internal |
| OLD | NEW |