| 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 "content/browser/devtools/devtools_browser_target.h" | 5 #include "content/browser/devtools/devtools_browser_target.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 | 46 |
| 47 typedef std::map<std::string, DevToolsBrowserTarget*> DomainMap; | 47 typedef std::map<std::string, DevToolsBrowserTarget*> DomainMap; |
| 48 base::LazyInstance<DomainMap>::Leaky g_used_domains = LAZY_INSTANCE_INITIALIZER; | 48 base::LazyInstance<DomainMap>::Leaky g_used_domains = LAZY_INSTANCE_INITIALIZER; |
| 49 | 49 |
| 50 void DevToolsBrowserTarget::HandleMessage(const std::string& data) { | 50 void DevToolsBrowserTarget::HandleMessage(const std::string& data) { |
| 51 DCHECK_EQ(message_loop_proxy_, base::MessageLoopProxy::current()); | 51 DCHECK_EQ(message_loop_proxy_, base::MessageLoopProxy::current()); |
| 52 std::string error_response; | 52 std::string error_response; |
| 53 scoped_refptr<DevToolsProtocol::Command> command = | 53 scoped_refptr<DevToolsProtocol::Command> command = |
| 54 DevToolsProtocol::ParseCommand(data, &error_response); | 54 DevToolsProtocol::ParseCommand(data, &error_response); |
| 55 if (!command) { | 55 if (!command.get()) { |
| 56 Respond(error_response); | 56 Respond(error_response); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 DomainHandlerMap::iterator it = handlers_.find(command->domain()); | 60 DomainHandlerMap::iterator it = handlers_.find(command->domain()); |
| 61 if (it == handlers_.end()) { | 61 if (it == handlers_.end()) { |
| 62 Respond(command->NoSuchMethodErrorResponse()->Serialize()); | 62 Respond(command->NoSuchMethodErrorResponse()->Serialize()); |
| 63 return; | 63 return; |
| 64 } | 64 } |
| 65 DomainMap& used_domains(g_used_domains.Get()); | 65 DomainMap& used_domains(g_used_domains.Get()); |
| 66 std::string domain = command->domain(); | 66 std::string domain = command->domain(); |
| 67 DomainMap::iterator jt = used_domains.find(domain); | 67 DomainMap::iterator jt = used_domains.find(domain); |
| 68 if (jt == used_domains.end()) { | 68 if (jt == used_domains.end()) { |
| 69 used_domains[domain] = this; | 69 used_domains[domain] = this; |
| 70 } else if (jt->second != this) { | 70 } else if (jt->second != this) { |
| 71 std::string message = | 71 std::string message = |
| 72 base::StringPrintf("'%s' is held by another connection", | 72 base::StringPrintf("'%s' is held by another connection", |
| 73 domain.c_str()); | 73 domain.c_str()); |
| 74 Respond(command->ServerErrorResponse(message)->Serialize()); | 74 Respond(command->ServerErrorResponse(message)->Serialize()); |
| 75 return; | 75 return; |
| 76 } | 76 } |
| 77 | 77 |
| 78 DevToolsProtocol::Handler* handler = it->second; | 78 DevToolsProtocol::Handler* handler = it->second; |
| 79 bool handle_directly = handle_on_ui_thread_.find(domain) == | 79 bool handle_directly = handle_on_ui_thread_.find(domain) == |
| 80 handle_on_ui_thread_.end(); | 80 handle_on_ui_thread_.end(); |
| 81 if (handle_directly) { | 81 if (handle_directly) { |
| 82 scoped_refptr<DevToolsProtocol::Response> response = | 82 scoped_refptr<DevToolsProtocol::Response> response = |
| 83 handler->HandleCommand(command); | 83 handler->HandleCommand(command); |
| 84 if (response && response->is_async_promise()) | 84 if (response.get() && response->is_async_promise()) |
| 85 return; | 85 return; |
| 86 if (response) | 86 if (response.get()) |
| 87 Respond(response->Serialize()); | 87 Respond(response->Serialize()); |
| 88 else | 88 else |
| 89 Respond(command->NoSuchMethodErrorResponse()->Serialize()); | 89 Respond(command->NoSuchMethodErrorResponse()->Serialize()); |
| 90 return; | 90 return; |
| 91 } | 91 } |
| 92 | 92 |
| 93 BrowserThread::PostTask( | 93 BrowserThread::PostTask( |
| 94 BrowserThread::UI, | 94 BrowserThread::UI, |
| 95 FROM_HERE, | 95 FROM_HERE, |
| 96 base::Bind(&DevToolsBrowserTarget::HandleCommandOnUIThread, | 96 base::Bind(&DevToolsBrowserTarget::HandleCommandOnUIThread, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // DCHECK that Detach has been called or no handler has ever been registered. | 141 // DCHECK that Detach has been called or no handler has ever been registered. |
| 142 DCHECK(handlers_.empty()); | 142 DCHECK(handlers_.empty()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void DevToolsBrowserTarget::HandleCommandOnUIThread( | 145 void DevToolsBrowserTarget::HandleCommandOnUIThread( |
| 146 DevToolsProtocol::Handler* handler, | 146 DevToolsProtocol::Handler* handler, |
| 147 scoped_refptr<DevToolsProtocol::Command> command) { | 147 scoped_refptr<DevToolsProtocol::Command> command) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 149 scoped_refptr<DevToolsProtocol::Response> response = | 149 scoped_refptr<DevToolsProtocol::Response> response = |
| 150 handler->HandleCommand(command); | 150 handler->HandleCommand(command); |
| 151 if (response && response->is_async_promise()) | 151 if (response.get() && response->is_async_promise()) |
| 152 return; | 152 return; |
| 153 | 153 |
| 154 if (response) | 154 if (response.get()) |
| 155 RespondFromUIThread(response->Serialize()); | 155 RespondFromUIThread(response->Serialize()); |
| 156 else | 156 else |
| 157 RespondFromUIThread(command->NoSuchMethodErrorResponse()->Serialize()); | 157 RespondFromUIThread(command->NoSuchMethodErrorResponse()->Serialize()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void DevToolsBrowserTarget::DeleteHandlersOnUIThread( | 160 void DevToolsBrowserTarget::DeleteHandlersOnUIThread( |
| 161 std::vector<DevToolsProtocol::Handler*> handlers) { | 161 std::vector<DevToolsProtocol::Handler*> handlers) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 163 STLDeleteElements(&handlers); | 163 STLDeleteElements(&handlers); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void DevToolsBrowserTarget::Respond(const std::string& message) { | 166 void DevToolsBrowserTarget::Respond(const std::string& message) { |
| 167 DCHECK_EQ(message_loop_proxy_, base::MessageLoopProxy::current()); | 167 DCHECK_EQ(message_loop_proxy_, base::MessageLoopProxy::current()); |
| 168 if (!http_server_) | 168 if (!http_server_) |
| 169 return; | 169 return; |
| 170 http_server_->SendOverWebSocket(connection_id_, message); | 170 http_server_->SendOverWebSocket(connection_id_, message); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void DevToolsBrowserTarget::RespondFromUIThread(const std::string& message) { | 173 void DevToolsBrowserTarget::RespondFromUIThread(const std::string& message) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 175 message_loop_proxy_->PostTask( | 175 message_loop_proxy_->PostTask( |
| 176 FROM_HERE, | 176 FROM_HERE, |
| 177 base::Bind(&DevToolsBrowserTarget::Respond, this, message)); | 177 base::Bind(&DevToolsBrowserTarget::Respond, this, message)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 } // namespace content | 180 } // namespace content |
| OLD | NEW |