1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "DRTDevToolsAgent.h"
33
34#include "DRTDevToolsClient.h"
35
36#include "WebCString.h"
37#include "WebDevToolsAgent.h"
38#include "WebView.h"
39#include "webkit/support/webkit_support.h"
40
41using namespace WebKit;
42
43DRTDevToolsAgent::DRTDevToolsAgent()
44    : m_drtDevToolsClient(0)
45    , m_webView(0)
46{
47    static int devToolsAgentCounter = 0;
48
49    m_routingID = ++devToolsAgentCounter;
50    if (m_routingID == 1)
51        WebDevToolsAgent::setMessageLoopDispatchHandler(&DRTDevToolsAgent::dispatchMessageLoop);
52}
53
54void DRTDevToolsAgent::reset()
55{
56    m_taskList.revokeAll();
57}
58
59void DRTDevToolsAgent::setWebView(WebView* webView)
60{
61    m_webView = webView;
62}
63
64void DRTDevToolsAgent::sendMessageToInspectorFrontend(const WebString& data)
65{
66    if (m_drtDevToolsClient)
67         m_drtDevToolsClient->asyncCall(data);
68}
69
70void DRTDevToolsAgent::runtimePropertyChanged(const WebString& name, const WebString& value)
71{
72    // FIXME: Implement.
73}
74
75WebDevToolsAgentClient::WebKitClientMessageLoop* DRTDevToolsAgent::createClientMessageLoop()
76{
77    return webkit_support::CreateDevToolsMessageLoop();
78}
79
80void DRTDevToolsAgent::asyncCall(const WebString& args)
81{
82    postTask(new AsyncCallTask(this, args));
83}
84
85void DRTDevToolsAgent::call(const WebString& args)
86{
87    WebDevToolsAgent* agent = webDevToolsAgent();
88    if (agent)
89        agent->dispatchOnInspectorBackend(args);
90}
91
92void DRTDevToolsAgent::delayedFrontendLoaded()
93{
94    WebDevToolsAgent* agent = webDevToolsAgent();
95    if (agent)
96        agent->frontendLoaded();
97}
98
99
100WebDevToolsAgent* DRTDevToolsAgent::webDevToolsAgent()
101{
102    if (!m_webView)
103        return 0;
104    return m_webView->devToolsAgent();
105}
106
107void DRTDevToolsAgent::attach(DRTDevToolsClient* client)
108{
109    ASSERT(!m_drtDevToolsClient);
110    m_drtDevToolsClient = client;
111    WebDevToolsAgent* agent = webDevToolsAgent();
112    if (agent)
113        agent->attach();
114}
115
116void DRTDevToolsAgent::detach()
117{
118    ASSERT(m_drtDevToolsClient);
119    WebDevToolsAgent* agent = webDevToolsAgent();
120    if (agent)
121        agent->detach();
122    m_drtDevToolsClient = 0;
123}
124
125void DRTDevToolsAgent::frontendLoaded()
126{
127    postTask(new DelayedFrontendLoadedTask(this));
128}
129
130bool DRTDevToolsAgent::setTimelineProfilingEnabled(bool enabled)
131{
132    WebDevToolsAgent* agent = webDevToolsAgent();
133    if (!agent)
134        return false;
135    agent->setTimelineProfilingEnabled(enabled);
136    return true;
137}
138
139bool DRTDevToolsAgent::evaluateInWebInspector(long callID, const std::string& script)
140{
141    WebDevToolsAgent* agent = webDevToolsAgent();
142    if (!agent)
143        return false;
144    agent->evaluateInWebInspector(callID, WebString::fromUTF8(script));
145    return true;
146}
147
148// static method
149void DRTDevToolsAgent::dispatchMessageLoop()
150{
151    webkit_support::DispatchMessageLoop();
152}
153