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/values.h" | 11 #include "base/values.h" |
11 #include "chrome/test/chromedriver/devtools_event_listener.h" | 12 #include "chrome/test/chromedriver/devtools_event_listener.h" |
12 #include "chrome/test/chromedriver/net/sync_websocket.h" | 13 #include "chrome/test/chromedriver/net/sync_websocket.h" |
13 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 14 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
14 #include "chrome/test/chromedriver/status.h" | 15 #include "chrome/test/chromedriver/status.h" |
15 | 16 |
16 namespace internal { | 17 namespace internal { |
17 | 18 |
18 InspectorEvent::InspectorEvent() {} | 19 InspectorEvent::InspectorEvent() {} |
19 | 20 |
(...skipping 17 matching lines...) Expand all Loading... |
37 DevToolsClientImpl::DevToolsClientImpl( | 38 DevToolsClientImpl::DevToolsClientImpl( |
38 const SyncWebSocketFactory& factory, | 39 const SyncWebSocketFactory& factory, |
39 const std::string& url, | 40 const std::string& url, |
40 const ParserFunc& parser_func) | 41 const ParserFunc& parser_func) |
41 : socket_(factory.Run().Pass()), | 42 : socket_(factory.Run().Pass()), |
42 url_(url), | 43 url_(url), |
43 parser_func_(parser_func), | 44 parser_func_(parser_func), |
44 connected_(false), | 45 connected_(false), |
45 next_id_(1) {} | 46 next_id_(1) {} |
46 | 47 |
47 DevToolsClientImpl::~DevToolsClientImpl() {} | 48 DevToolsClientImpl::~DevToolsClientImpl() { |
| 49 for (ResponseMap::iterator iter = cmd_response_map_.begin(); |
| 50 iter != cmd_response_map_.end(); ++iter) { |
| 51 LOG(WARNING) << "Finished with no response for command " << iter->first; |
| 52 delete iter->second; |
| 53 } |
| 54 } |
| 55 |
| 56 void DevToolsClientImpl::SetParserFuncForTesting( |
| 57 const ParserFunc& parser_func) { |
| 58 parser_func_ = parser_func; |
| 59 } |
48 | 60 |
49 Status DevToolsClientImpl::SendCommand( | 61 Status DevToolsClientImpl::SendCommand( |
50 const std::string& method, | 62 const std::string& method, |
51 const base::DictionaryValue& params) { | 63 const base::DictionaryValue& params) { |
52 scoped_ptr<base::DictionaryValue> result; | 64 scoped_ptr<base::DictionaryValue> result; |
53 return SendCommandInternal(method, params, &result); | 65 return SendCommandInternal(method, params, &result); |
54 } | 66 } |
55 | 67 |
56 Status DevToolsClientImpl::SendCommandAndGetResult( | 68 Status DevToolsClientImpl::SendCommandAndGetResult( |
57 const std::string& method, | 69 const std::string& method, |
(...skipping 15 matching lines...) Expand all Loading... |
73 } | 85 } |
74 | 86 |
75 Status DevToolsClientImpl::HandleEventsUntil( | 87 Status DevToolsClientImpl::HandleEventsUntil( |
76 const ConditionalFunc& conditional_func) { | 88 const ConditionalFunc& conditional_func) { |
77 internal::InspectorMessageType type; | 89 internal::InspectorMessageType type; |
78 internal::InspectorEvent event; | 90 internal::InspectorEvent event; |
79 internal::InspectorCommandResponse response; | 91 internal::InspectorCommandResponse response; |
80 | 92 |
81 while (socket_->HasNextMessage() || !conditional_func.Run()) { | 93 while (socket_->HasNextMessage() || !conditional_func.Run()) { |
82 Status status = ReceiveNextMessage(-1, &type, &event, &response); | 94 Status status = ReceiveNextMessage(-1, &type, &event, &response); |
83 if (status.IsError()) { | 95 if (status.IsError()) |
84 return status; | 96 return status; |
85 } else if (type == internal::kCommandResponseMessageType) { | |
86 return Status(kUnknownError, "unexpected command message"); | |
87 } | |
88 } | 97 } |
89 return Status(kOk); | 98 return Status(kOk); |
90 } | 99 } |
91 | 100 |
92 Status DevToolsClientImpl::SendCommandInternal( | 101 Status DevToolsClientImpl::SendCommandInternal( |
93 const std::string& method, | 102 const std::string& method, |
94 const base::DictionaryValue& params, | 103 const base::DictionaryValue& params, |
95 scoped_ptr<base::DictionaryValue>* result) { | 104 scoped_ptr<base::DictionaryValue>* result) { |
96 if (!connected_) { | 105 if (!connected_) { |
97 if (!socket_->Connect(url_)) | 106 if (!socket_->Connect(url_)) |
98 return Status(kDisconnected, "unable to connect to renderer"); | 107 return Status(kDisconnected, "unable to connect to renderer"); |
99 connected_ = true; | 108 connected_ = true; |
100 } | 109 } |
101 | 110 |
102 int command_id = next_id_++; | 111 int command_id = next_id_++; |
103 base::DictionaryValue command; | 112 base::DictionaryValue command; |
104 command.SetInteger("id", command_id); | 113 command.SetInteger("id", command_id); |
105 command.SetString("method", method); | 114 command.SetString("method", method); |
106 command.Set("params", params.DeepCopy()); | 115 command.Set("params", params.DeepCopy()); |
107 std::string message; | 116 std::string message; |
108 base::JSONWriter::Write(&command, &message); | 117 base::JSONWriter::Write(&command, &message); |
109 if (!socket_->Send(message)) { | 118 if (!socket_->Send(message)) { |
110 connected_ = false; | 119 connected_ = false; |
111 return Status(kDisconnected, "unable to send message to renderer"); | 120 return Status(kDisconnected, "unable to send message to renderer"); |
112 } | 121 } |
113 | |
114 return ReceiveCommandResponse(command_id, result); | 122 return ReceiveCommandResponse(command_id, result); |
115 } | 123 } |
116 | 124 |
117 Status DevToolsClientImpl::ReceiveCommandResponse( | 125 Status DevToolsClientImpl::ReceiveCommandResponse( |
118 int command_id, | 126 int command_id, |
119 scoped_ptr<base::DictionaryValue>* result) { | 127 scoped_ptr<base::DictionaryValue>* result) { |
120 internal::InspectorMessageType type; | 128 internal::InspectorMessageType type; |
121 internal::InspectorEvent event; | 129 internal::InspectorEvent event; |
122 internal::InspectorCommandResponse response; | 130 internal::InspectorCommandResponse response; |
123 while (true) { | 131 cmd_response_map_[command_id] = NULL; |
| 132 while (cmd_response_map_[command_id] == NULL) { |
124 Status status = ReceiveNextMessage(command_id, &type, &event, &response); | 133 Status status = ReceiveNextMessage(command_id, &type, &event, &response); |
125 if (status.IsError()) { | 134 if (status.IsError()) |
126 return status; | 135 return status; |
127 } else if (type == internal::kCommandResponseMessageType) { | |
128 if (response.id != command_id) { | |
129 return Status(kUnknownError, | |
130 "received response for unknown command ID"); | |
131 } | |
132 if (response.result) { | |
133 result->reset(response.result.release()); | |
134 return Status(kOk); | |
135 } | |
136 return Status(kUnknownError, "inspector error: " + response.error); | |
137 } | |
138 } | 136 } |
| 137 result->reset(cmd_response_map_[command_id]); |
| 138 cmd_response_map_.erase(command_id); |
| 139 return Status(kOk); |
139 } | 140 } |
140 | 141 |
141 Status DevToolsClientImpl::ReceiveNextMessage( | 142 Status DevToolsClientImpl::ReceiveNextMessage( |
142 int expected_id, | 143 int expected_id, |
143 internal::InspectorMessageType* type, | 144 internal::InspectorMessageType* type, |
144 internal::InspectorEvent* event, | 145 internal::InspectorEvent* event, |
145 internal::InspectorCommandResponse* response) { | 146 internal::InspectorCommandResponse* response) { |
146 std::string message; | 147 std::string message; |
147 if (!socket_->ReceiveNextMessage(&message)) { | 148 if (!socket_->ReceiveNextMessage(&message)) { |
148 connected_ = false; | 149 connected_ = false; |
149 return Status(kDisconnected, | 150 return Status(kDisconnected, |
150 "unable to receive message from renderer"); | 151 "unable to receive message from renderer"); |
151 } | 152 } |
152 if (!parser_func_.Run(message, expected_id, type, event, response)) | 153 if (!parser_func_.Run(message, expected_id, type, event, response)) |
153 return Status(kUnknownError, "bad inspector message: " + message); | 154 return Status(kUnknownError, "bad inspector message: " + message); |
154 if (*type == internal::kEventMessageType) | 155 if (*type == internal::kEventMessageType) |
155 return NotifyEventListeners(event->method, *event->params); | 156 return NotifyEventListeners(event->method, *event->params); |
| 157 if (*type == internal::kCommandResponseMessageType) { |
| 158 if (cmd_response_map_.count(response->id) == 0) { |
| 159 return Status(kUnknownError, "unexpected command message"); |
| 160 } else if (response->result) { |
| 161 cmd_response_map_[response->id] = response->result.release(); |
| 162 } else { |
| 163 return Status(kUnknownError, "inspector error: " + response->error); |
| 164 } |
| 165 } |
156 return Status(kOk); | 166 return Status(kOk); |
157 } | 167 } |
158 | 168 |
159 Status DevToolsClientImpl::NotifyEventListeners( | 169 Status DevToolsClientImpl::NotifyEventListeners( |
160 const std::string& method, | 170 const std::string& method, |
161 const base::DictionaryValue& params) { | 171 const base::DictionaryValue& params) { |
162 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); | 172 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); |
163 iter != listeners_.end(); ++iter) { | 173 iter != listeners_.end(); ++iter) { |
164 (*iter)->OnEvent(method, params); | 174 (*iter)->OnEvent(method, params); |
165 } | 175 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 if (unscoped_result) | 220 if (unscoped_result) |
211 command_response->result.reset(unscoped_result->DeepCopy()); | 221 command_response->result.reset(unscoped_result->DeepCopy()); |
212 else | 222 else |
213 base::JSONWriter::Write(unscoped_error, &command_response->error); | 223 base::JSONWriter::Write(unscoped_error, &command_response->error); |
214 return true; | 224 return true; |
215 } | 225 } |
216 return false; | 226 return false; |
217 } | 227 } |
218 | 228 |
219 } // namespace internal | 229 } // namespace internal |
OLD | NEW |