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 | |
75 void DevToolsClientImpl::NotifyEventListeners( | |
kkania
2013/01/16 16:57:33
i think style guide says follow order in .h
craigdh
2013/01/16 17:38:53
Good catch, thanks.
| |
76 const std::string& method, | |
77 const base::DictionaryValue& params) { | |
78 for (std::list<DevToolsEventListener*>::iterator iter = listeners_.begin(); | |
79 iter != listeners_.end(); ++iter) { | |
80 (*iter)->OnEvent(method, params); | |
81 } | |
82 } | |
83 | |
74 Status DevToolsClientImpl::SendCommandInternal( | 84 Status DevToolsClientImpl::SendCommandInternal( |
75 const std::string& method, | 85 const std::string& method, |
76 const base::DictionaryValue& params, | 86 const base::DictionaryValue& params, |
77 scoped_ptr<base::DictionaryValue>* result) { | 87 scoped_ptr<base::DictionaryValue>* result) { |
78 if (!connected_) { | 88 if (!connected_) { |
79 if (!socket_->Connect(url_)) | 89 if (!socket_->Connect(url_)) |
80 return Status(kUnknownError, "unable to connect to renderer"); | 90 return Status(kUnknownError, "unable to connect to renderer"); |
81 connected_ = true; | 91 connected_ = true; |
82 } | 92 } |
83 | 93 |
(...skipping 11 matching lines...) Expand all Loading... | |
95 while (true) { | 105 while (true) { |
96 std::string message; | 106 std::string message; |
97 if (!socket_->ReceiveNextMessage(&message)) | 107 if (!socket_->ReceiveNextMessage(&message)) |
98 return Status(kUnknownError, "unable to receive message from renderer"); | 108 return Status(kUnknownError, "unable to receive message from renderer"); |
99 internal::InspectorMessageType type; | 109 internal::InspectorMessageType type; |
100 internal::InspectorEvent event; | 110 internal::InspectorEvent event; |
101 internal::InspectorCommandResponse response; | 111 internal::InspectorCommandResponse response; |
102 if (!parser_func_.Run(message, command_id, &type, &event, &response)) | 112 if (!parser_func_.Run(message, command_id, &type, &event, &response)) |
103 return Status(kUnknownError, "bad inspector message: " + message); | 113 return Status(kUnknownError, "bad inspector message: " + message); |
104 if (type == internal::kEventMessageType) { | 114 if (type == internal::kEventMessageType) { |
105 if (listener_) | 115 NotifyEventListeners(event.method, *event.params); |
kkania
2013/01/16 16:57:33
two spaces
craigdh
2013/01/16 17:38:53
Done.
| |
106 listener_->OnEvent(event.method, *event.params); | |
107 } else { | 116 } else { |
108 if (response.id != command_id) { | 117 if (response.id != command_id) { |
109 return Status(kUnknownError, | 118 return Status(kUnknownError, |
110 "received response for unknown command ID"); | 119 "received response for unknown command ID"); |
111 } | 120 } |
112 if (response.result) { | 121 if (response.result) { |
113 result->reset(response.result.release()); | 122 result->reset(response.result.release()); |
114 return Status(kOk); | 123 return Status(kOk); |
115 } | 124 } |
116 return Status(kUnknownError, "inspector error: " + response.error); | 125 return Status(kUnknownError, "inspector error: " + response.error); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |