OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/tools/test_shell/test_shell_devtools_agent.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "grit/webkit_chromium_resources.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
12 #include "webkit/tools/test_shell/test_shell_devtools_callargs.h" | |
13 #include "webkit/tools/test_shell/test_shell_devtools_client.h" | |
14 #include "webkit/glue/webkit_glue.h" | |
15 | |
16 using WebKit::WebCString; | |
17 using WebKit::WebDevToolsAgent; | |
18 using WebKit::WebDevToolsMessageData; | |
19 using WebKit::WebString; | |
20 using WebKit::WebView; | |
21 | |
22 namespace { | |
23 | |
24 class WebKitClientMessageLoopImpl | |
25 : public WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop { | |
26 public: | |
27 WebKitClientMessageLoopImpl() : message_loop_(MessageLoop::current()) { } | |
28 virtual ~WebKitClientMessageLoopImpl() { | |
29 message_loop_ = NULL; | |
30 } | |
31 virtual void run() { | |
32 MessageLoop::ScopedNestableTaskAllower allow(message_loop_); | |
33 message_loop_->Run(); | |
34 } | |
35 virtual void quitNow() { | |
36 message_loop_->QuitNow(); | |
37 } | |
38 private: | |
39 MessageLoop* message_loop_; | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 TestShellDevToolsAgent::TestShellDevToolsAgent() | |
45 : weak_factory_(this), | |
46 dev_tools_client_(NULL) { | |
47 static int dev_tools_agent_counter; | |
48 routing_id_ = ++dev_tools_agent_counter; | |
49 } | |
50 | |
51 TestShellDevToolsAgent::~TestShellDevToolsAgent() { | |
52 } | |
53 | |
54 void TestShellDevToolsAgent::SetWebView(WebKit::WebView* web_view) { | |
55 web_view_ = web_view; | |
56 } | |
57 | |
58 void TestShellDevToolsAgent::sendMessageToInspectorFrontend( | |
59 const WebString& data) { | |
60 if (dev_tools_client_) | |
61 dev_tools_client_->AsyncCall(TestShellDevToolsCallArgs(data)); | |
62 } | |
63 | |
64 int TestShellDevToolsAgent::hostIdentifier() { | |
65 return routing_id_; | |
66 } | |
67 | |
68 void TestShellDevToolsAgent::runtimePropertyChanged( | |
69 const WebKit::WebString& name, | |
70 const WebKit::WebString& value) { | |
71 // TODO: Implement. | |
72 } | |
73 | |
74 WebKit::WebDevToolsAgentClient::WebKitClientMessageLoop* | |
75 TestShellDevToolsAgent::createClientMessageLoop() { | |
76 return new WebKitClientMessageLoopImpl(); | |
77 } | |
78 | |
79 void TestShellDevToolsAgent::AsyncCall(const TestShellDevToolsCallArgs &args) { | |
80 MessageLoop::current()->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&TestShellDevToolsAgent::Call, weak_factory_.GetWeakPtr(), | |
83 args)); | |
84 } | |
85 | |
86 void TestShellDevToolsAgent::Call(const TestShellDevToolsCallArgs &args) { | |
87 WebDevToolsAgent* web_agent = GetWebAgent(); | |
88 if (web_agent) | |
89 web_agent->dispatchOnInspectorBackend(args.data_); | |
90 if (TestShellDevToolsCallArgs::calls_count() == 1 && dev_tools_client_) | |
91 dev_tools_client_->all_messages_processed(); | |
92 } | |
93 | |
94 WebDevToolsAgent* TestShellDevToolsAgent::GetWebAgent() { | |
95 if (!web_view_) | |
96 return NULL; | |
97 return web_view_->devToolsAgent(); | |
98 } | |
99 | |
100 void TestShellDevToolsAgent::attach(TestShellDevToolsClient* client) { | |
101 DCHECK(!dev_tools_client_); | |
102 dev_tools_client_ = client; | |
103 WebDevToolsAgent *web_agent = GetWebAgent(); | |
104 if (web_agent) | |
105 web_agent->attach(); | |
106 } | |
107 | |
108 void TestShellDevToolsAgent::detach() { | |
109 DCHECK(dev_tools_client_); | |
110 WebDevToolsAgent* web_agent = GetWebAgent(); | |
111 if (web_agent) | |
112 web_agent->detach(); | |
113 dev_tools_client_ = NULL; | |
114 } | |
115 | |
116 bool TestShellDevToolsAgent::evaluateInWebInspector( | |
117 long call_id, | |
118 const std::string& script) { | |
119 WebDevToolsAgent* agent = GetWebAgent(); | |
120 if (!agent) | |
121 return false; | |
122 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script)); | |
123 return true; | |
124 } | |
OLD | NEW |