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 "WebDevToolsFrontendImpl.h"
33
34#include "BoundObject.h"
35#include "ContextMenuController.h"
36#include "ContextMenuItem.h"
37#include "DOMWindow.h"
38#include "Document.h"
39#include "Event.h"
40#include "Frame.h"
41#include "InspectorController.h"
42#include "InspectorFrontendClientImpl.h"
43#include "InspectorFrontendHost.h"
44#include "Node.h"
45#include "Page.h"
46#include "Pasteboard.h"
47#include "PlatformString.h"
48#include "SecurityOrigin.h"
49#include "Settings.h"
50#include "V8Binding.h"
51#include "V8DOMWrapper.h"
52#include "V8InspectorFrontendHost.h"
53#include "V8MouseEvent.h"
54#include "V8Node.h"
55#include "V8Proxy.h"
56#include "V8Utilities.h"
57#include "WebDevToolsFrontendClient.h"
58#include "WebFrameImpl.h"
59#include "WebScriptSource.h"
60#include "WebViewImpl.h"
61#include <wtf/OwnPtr.h>
62#include <wtf/Vector.h>
63
64using namespace WebCore;
65
66namespace WebKit {
67
68static v8::Local<v8::String> ToV8String(const String& s)
69{
70    if (s.isNull())
71        return v8::Local<v8::String>();
72
73    return v8::String::New(reinterpret_cast<const uint16_t*>(s.characters()), s.length());
74}
75
76WebDevToolsFrontend* WebDevToolsFrontend::create(
77    WebView* view,
78    WebDevToolsFrontendClient* client,
79    const WebString& applicationLocale)
80{
81    return new WebDevToolsFrontendImpl(
82      static_cast<WebViewImpl*>(view),
83      client,
84      applicationLocale);
85}
86
87WebDevToolsFrontendImpl::WebDevToolsFrontendImpl(
88    WebViewImpl* webViewImpl,
89    WebDevToolsFrontendClient* client,
90    const String& applicationLocale)
91    : m_webViewImpl(webViewImpl)
92    , m_client(client)
93    , m_applicationLocale(applicationLocale)
94{
95    InspectorController* ic = m_webViewImpl->page()->inspectorController();
96    ic->setInspectorFrontendClient(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this));
97
98    // Put each DevTools frontend Page into its own (single page) group so that it's not
99    // deferred along with the inspected page.
100    m_webViewImpl->page()->setGroupName(String());
101}
102
103WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl()
104{
105}
106
107void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message)
108{
109    WebFrameImpl* frame = m_webViewImpl->mainFrameImpl();
110    v8::HandleScope scope;
111    v8::Handle<v8::Context> frameContext = V8Proxy::context(frame->frame());
112    v8::Context::Scope contextScope(frameContext);
113    v8::Handle<v8::Value> inspectorBackendValue = frameContext->Global()->Get(v8::String::New("InspectorBackend"));
114    if (!inspectorBackendValue->IsObject())
115        return;
116    v8::Handle<v8::Object> inspectorBackend = v8::Handle<v8::Object>::Cast(inspectorBackendValue);
117    v8::Handle<v8::Value> dispatchFunction = inspectorBackend->Get(v8::String::New("dispatch"));
118     // The frame might have navigated away from the front-end page (which is still weird).
119    if (!dispatchFunction->IsFunction())
120        return;
121    v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
122    Vector< v8::Handle<v8::Value> > args;
123    args.append(ToV8String(message));
124    v8::TryCatch tryCatch;
125    tryCatch.SetVerbose(true);
126    function->Call(inspectorBackend, args.size(), args.data());
127}
128
129void WebDevToolsFrontendImpl::frontendLoaded()
130{
131    m_client->sendFrontendLoaded();
132}
133
134} // namespace WebKit
135