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" |
11 #include "chrome/test/chromedriver/devtools_event_listener.h" | 11 #include "chrome/test/chromedriver/devtools_event_listener.h" |
12 #include "chrome/test/chromedriver/net/sync_websocket.h" | 12 #include "chrome/test/chromedriver/net/sync_websocket.h" |
13 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 13 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
14 #include "chrome/test/chromedriver/status.h" | 14 #include "chrome/test/chromedriver/status.h" |
15 | 15 |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 InspectorEvent::InspectorEvent() {} | 18 InspectorEvent::InspectorEvent() {} |
19 | 19 |
20 InspectorEvent::~InspectorEvent() {} | 20 InspectorEvent::~InspectorEvent() {} |
21 | 21 |
22 InspectorCommandResponse::InspectorCommandResponse() {} | 22 InspectorCommandResponse::InspectorCommandResponse() {} |
23 | 23 |
24 InspectorCommandResponse::~InspectorCommandResponse() {} | 24 InspectorCommandResponse::~InspectorCommandResponse() {} |
25 | 25 |
26 } // namespace internal | 26 } // namespace internal |
27 | 27 |
28 DevToolsClientImpl::DevToolsClientImpl( | 28 DevToolsClientImpl::DevToolsClientImpl( |
29 const SyncWebSocketFactory& factory, | 29 const SyncWebSocketFactory& factory, |
30 const std::string& url, | 30 const std::string& url) |
31 DevToolsEventListener* listener) | |
32 : socket_(factory.Run().Pass()), | 31 : socket_(factory.Run().Pass()), |
33 url_(url), | 32 url_(url), |
34 listener_(listener), | |
35 parser_func_(base::Bind(&internal::ParseInspectorMessage)), | 33 parser_func_(base::Bind(&internal::ParseInspectorMessage)), |
36 connected_(false), | 34 connected_(false), |
37 next_id_(1) {} | 35 next_id_(1) {} |
38 | 36 |
39 DevToolsClientImpl::DevToolsClientImpl( | 37 DevToolsClientImpl::DevToolsClientImpl( |
40 const SyncWebSocketFactory& factory, | 38 const SyncWebSocketFactory& factory, |
41 const std::string& url, | 39 const std::string& url, |
42 DevToolsEventListener* listener, | |
43 const ParserFunc& parser_func) | 40 const ParserFunc& parser_func) |
44 : socket_(factory.Run().Pass()), | 41 : socket_(factory.Run().Pass()), |
45 url_(url), | 42 url_(url), |
46 listener_(listener), | |
47 parser_func_(parser_func), | 43 parser_func_(parser_func), |
48 connected_(false), | 44 connected_(false), |
49 next_id_(1) {} | 45 next_id_(1) {} |
50 | 46 |
51 DevToolsClientImpl::~DevToolsClientImpl() {} | 47 DevToolsClientImpl::~DevToolsClientImpl() {} |
52 | 48 |
53 Status DevToolsClientImpl::SendCommand( | 49 Status DevToolsClientImpl::SendCommand( |
54 const std::string& method, | 50 const std::string& method, |
55 const base::DictionaryValue& params) { | 51 const base::DictionaryValue& params) { |
56 scoped_ptr<base::DictionaryValue> result; | 52 scoped_ptr<base::DictionaryValue> result; |
57 return SendCommandInternal(method, params, &result); | 53 return SendCommandInternal(method, params, &result); |
58 } | 54 } |
59 | 55 |
60 Status DevToolsClientImpl::SendCommandAndGetResult( | 56 Status DevToolsClientImpl::SendCommandAndGetResult( |
61 const std::string& method, | 57 const std::string& method, |
62 const base::DictionaryValue& params, | 58 const base::DictionaryValue& params, |
63 scoped_ptr<base::DictionaryValue>* result) { | 59 scoped_ptr<base::DictionaryValue>* result) { |
64 scoped_ptr<base::DictionaryValue> intermediate_result; | 60 scoped_ptr<base::DictionaryValue> intermediate_result; |
65 Status status = SendCommandInternal(method, params, &intermediate_result); | 61 Status status = SendCommandInternal(method, params, &intermediate_result); |
66 if (status.IsError()) | 62 if (status.IsError()) |
67 return status; | 63 return status; |
68 if (!intermediate_result) | 64 if (!intermediate_result) |
69 return Status(kUnknownError, "inspector response missing result"); | 65 return Status(kUnknownError, "inspector response missing result"); |
70 result->reset(intermediate_result.release()); | 66 result->reset(intermediate_result.release()); |
71 return Status(kOk); | 67 return Status(kOk); |
72 } | 68 } |
73 | 69 |
| 70 void DevToolsClientImpl::AddListener(DevToolsEventListener* listener) { |
| 71 DCHECK(listener); |
| 72 listeners_.push_back(listener); |
| 73 } |
| 74 |
74 Status DevToolsClientImpl::SendCommandInternal( | 75 Status DevToolsClientImpl::SendCommandInternal( |
75 const std::string& method, | 76 const std::string& method, |
76 const base::DictionaryValue& params, | 77 const base::DictionaryValue& params, |
77 scoped_ptr<base::DictionaryValue>* result) { | 78 scoped_ptr<base::DictionaryValue>* result) { |
78 if (!connected_) { | 79 if (!connected_) { |
79 if (!socket_->Connect(url_)) | 80 if (!socket_->Connect(url_)) |
80 return Status(kUnknownError, "unable to connect to renderer"); | 81 return Status(kUnknownError, "unable to connect to renderer"); |
81 connected_ = true; | 82 connected_ = true; |
82 } | 83 } |
83 | 84 |
(...skipping 11 matching lines...) Expand all Loading... |
95 while (true) { | 96 while (true) { |
96 std::string message; | 97 std::string message; |
97 if (!socket_->ReceiveNextMessage(&message)) | 98 if (!socket_->ReceiveNextMessage(&message)) |
98 return Status(kUnknownError, "unable to receive message from renderer"); | 99 return Status(kUnknownError, "unable to receive message from renderer"); |
99 internal::InspectorMessageType type; | 100 internal::InspectorMessageType type; |
100 internal::InspectorEvent event; | 101 internal::InspectorEvent event; |
101 internal::InspectorCommandResponse response; | 102 internal::InspectorCommandResponse response; |
102 if (!parser_func_.Run(message, command_id, &type, &event, &response)) | 103 if (!parser_func_.Run(message, command_id, &type, &event, &response)) |
103 return Status(kUnknownError, "bad inspector message: " + message); | 104 return Status(kUnknownError, "bad inspector message: " + message); |
104 if (type == internal::kEventMessageType) { | 105 if (type == internal::kEventMessageType) { |
105 if (listener_) | 106 NotifyEventListeners(event.method, *event.params); |
106 listener_->OnEvent(event.method, *event.params); | |
107 } else { | 107 } else { |
108 if (response.id != command_id) { | 108 if (response.id != command_id) { |
109 return Status(kUnknownError, | 109 return Status(kUnknownError, |
110 "received response for unknown command ID"); | 110 "received response for unknown command ID"); |
111 } | 111 } |
112 if (response.result) { | 112 if (response.result) { |
113 result->reset(response.result.release()); | 113 result->reset(response.result.release()); |
114 return Status(kOk); | 114 return Status(kOk); |
115 } | 115 } |
116 return Status(kUnknownError, "inspector error: " + response.error); | 116 return Status(kUnknownError, "inspector error: " + response.error); |
117 } | 117 } |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
| 121 void DevToolsClientImpl::NotifyEventListeners( |
| 122 const std::string& method, |
| 123 const base::DictionaryValue& params) { |
| 124 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); |
| 125 iter != listeners_.end(); ++iter) { |
| 126 (*iter)->OnEvent(method, params); |
| 127 } |
| 128 } |
| 129 |
121 namespace internal { | 130 namespace internal { |
122 | 131 |
123 bool ParseInspectorMessage( | 132 bool ParseInspectorMessage( |
124 const std::string& message, | 133 const std::string& message, |
125 int expected_id, | 134 int expected_id, |
126 InspectorMessageType* type, | 135 InspectorMessageType* type, |
127 InspectorEvent* event, | 136 InspectorEvent* event, |
128 InspectorCommandResponse* command_response) { | 137 InspectorCommandResponse* command_response) { |
129 scoped_ptr<base::Value> message_value(base::JSONReader::Read(message)); | 138 scoped_ptr<base::Value> message_value(base::JSONReader::Read(message)); |
130 base::DictionaryValue* message_dict; | 139 base::DictionaryValue* message_dict; |
(...skipping 27 matching lines...) Expand all Loading... |
158 if (unscoped_result) | 167 if (unscoped_result) |
159 command_response->result.reset(unscoped_result->DeepCopy()); | 168 command_response->result.reset(unscoped_result->DeepCopy()); |
160 else | 169 else |
161 base::JSONWriter::Write(unscoped_error, &command_response->error); | 170 base::JSONWriter::Write(unscoped_error, &command_response->error); |
162 return true; | 171 return true; |
163 } | 172 } |
164 return false; | 173 return false; |
165 } | 174 } |
166 | 175 |
167 } // namespace internal | 176 } // namespace internal |
OLD | NEW |