| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "web/WebDevToolsFrontendImpl.h" | 32 #include "web/WebDevToolsFrontendImpl.h" |
| 33 | 33 |
| 34 #include "bindings/core/v8/ScriptController.h" | 34 #include "bindings/core/v8/ScriptController.h" |
| 35 #include "bindings/core/v8/V8Binding.h" | |
| 36 #include "bindings/core/v8/V8DOMWrapper.h" | |
| 37 #include "bindings/core/v8/V8InspectorFrontendHost.h" | 35 #include "bindings/core/v8/V8InspectorFrontendHost.h" |
| 38 #include "bindings/core/v8/V8MouseEvent.h" | |
| 39 #include "bindings/core/v8/V8Node.h" | |
| 40 #include "core/clipboard/Pasteboard.h" | |
| 41 #include "core/dom/Document.h" | |
| 42 #include "core/dom/Node.h" | |
| 43 #include "core/events/Event.h" | |
| 44 #include "core/frame/LocalDOMWindow.h" | |
| 45 #include "core/frame/LocalFrame.h" | 36 #include "core/frame/LocalFrame.h" |
| 46 #include "core/frame/Settings.h" | |
| 47 #include "core/inspector/InspectorController.h" | 37 #include "core/inspector/InspectorController.h" |
| 48 #include "core/inspector/InspectorFrontendHost.h" | 38 #include "core/inspector/InspectorFrontendHost.h" |
| 49 #include "core/page/ContextMenuController.h" | |
| 50 #include "core/page/Page.h" | 39 #include "core/page/Page.h" |
| 51 #include "platform/ContextMenuItem.h" | 40 #include "public/platform/WebString.h" |
| 52 #include "platform/weborigin/SecurityOrigin.h" | |
| 53 #include "public/web/WebDevToolsFrontendClient.h" | 41 #include "public/web/WebDevToolsFrontendClient.h" |
| 54 #include "public/web/WebScriptSource.h" | |
| 55 #include "web/InspectorFrontendClientImpl.h" | |
| 56 #include "web/WebLocalFrameImpl.h" | |
| 57 #include "web/WebViewImpl.h" | 42 #include "web/WebViewImpl.h" |
| 58 #include "wtf/OwnPtr.h" | |
| 59 #include "wtf/Vector.h" | |
| 60 | 43 |
| 61 namespace blink { | 44 namespace blink { |
| 62 | 45 |
| 63 class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDO
MObject { | |
| 64 WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver); | |
| 65 public: | |
| 66 InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontend
Impl, Document* document) | |
| 67 : ActiveDOMObject(document) | |
| 68 , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl) | |
| 69 { | |
| 70 suspendIfNeeded(); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 virtual void resume() OVERRIDE | |
| 75 { | |
| 76 m_webDevToolsFrontendImpl->resume(); | |
| 77 } | |
| 78 | |
| 79 WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl; | |
| 80 }; | |
| 81 | |
| 82 WebDevToolsFrontend* WebDevToolsFrontend::create( | 46 WebDevToolsFrontend* WebDevToolsFrontend::create( |
| 83 WebView* view, | 47 WebView* view, |
| 84 WebDevToolsFrontendClient* client, | 48 WebDevToolsFrontendClient* client, |
| 85 const WebString& applicationLocale) | 49 const WebString& applicationLocale) |
| 86 { | 50 { |
| 87 return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationL
ocale); | 51 return new WebDevToolsFrontendImpl(toWebViewImpl(view), client); |
| 88 } | 52 } |
| 89 | 53 |
| 90 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl( | 54 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl( |
| 91 WebViewImpl* webViewImpl, | 55 WebViewImpl* webViewImpl, |
| 92 WebDevToolsFrontendClient* client, | 56 WebDevToolsFrontendClient* client) |
| 93 const String& applicationLocale) | |
| 94 : m_webViewImpl(webViewImpl) | 57 : m_webViewImpl(webViewImpl) |
| 95 , m_client(client) | 58 , m_client(client) |
| 96 , m_applicationLocale(applicationLocale) | |
| 97 , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDisp
atch) | |
| 98 { | 59 { |
| 99 m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adop
tPtrWillBeNoop(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client,
this))); | 60 m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(this
); |
| 100 } | 61 } |
| 101 | 62 |
| 102 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl() | 63 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl() |
| 103 { | 64 { |
| 65 ASSERT(!m_frontendHost); |
| 104 } | 66 } |
| 105 | 67 |
| 106 void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& messa
ge) | 68 void WebDevToolsFrontendImpl::dispose() |
| 107 { | 69 { |
| 108 m_messages.append(message); | 70 if (m_frontendHost) { |
| 109 maybeDispatch(0); | 71 m_frontendHost->disconnectClient(); |
| 72 m_frontendHost = nullptr; |
| 73 } |
| 74 m_client = 0; |
| 110 } | 75 } |
| 111 | 76 |
| 112 void WebDevToolsFrontendImpl::resume() | 77 void WebDevToolsFrontendImpl::windowObjectCleared() |
| 113 { | 78 { |
| 114 // We should call maybeDispatch asynchronously here because we are not allow
ed to update activeDOMObjects list in | 79 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 115 // resume (See ExecutionContext::resumeActiveDOMObjects). | 80 Page* page = m_webViewImpl->page(); |
| 116 if (!m_inspectorFrontendDispatchTimer.isActive()) | 81 ASSERT(page->mainFrame()); |
| 117 m_inspectorFrontendDispatchTimer.startOneShot(0, FROM_HERE); | 82 ScriptState* scriptState = ScriptState::forMainWorld(page->deprecatedLocalMa
inFrame()); |
| 118 } | 83 ScriptState::Scope scope(scriptState); |
| 119 | 84 |
| 120 void WebDevToolsFrontendImpl::maybeDispatch(Timer<WebDevToolsFrontendImpl>*) | 85 if (m_frontendHost) |
| 121 { | 86 m_frontendHost->disconnectClient(); |
| 122 while (!m_messages.isEmpty()) { | 87 m_frontendHost = InspectorFrontendHost::create(this, page); |
| 123 Document* document = m_webViewImpl->page()->deprecatedLocalMainFrame()->
document(); | 88 v8::Handle<v8::Object> global = scriptState->context()->Global(); |
| 124 if (document->activeDOMObjectsAreSuspended()) { | 89 v8::Handle<v8::Value> frontendHostObj = toV8(m_frontendHost.get(), global, s
criptState->isolate()); |
| 125 m_inspectorFrontendResumeObserver = adoptPtr(new InspectorFrontendRe
sumeObserver(this, document)); | 90 |
| 126 return; | 91 global->Set(v8::String::NewFromUtf8(isolate, "InspectorFrontendHost"), front
endHostObj); |
| 127 } | 92 ScriptController* scriptController = page->mainFrame() ? &page->deprecatedLo
calMainFrame()->script() : 0; |
| 128 m_inspectorFrontendResumeObserver.clear(); | 93 if (scriptController) { |
| 129 doDispatchOnInspectorFrontend(m_messages.takeFirst()); | 94 String installAdditionalAPI = |
| 95 "" // Wrap messages that go to embedder. |
| 96 "(function(host, methodEntries) {" |
| 97 " host._lastCallId = 0;" |
| 98 " host._callbacks = [];" |
| 99 " host.embedderMessageAck = function(id, error)" |
| 100 " {" |
| 101 " var callback = host._callbacks[id];" |
| 102 " delete host._callbacks[id];" |
| 103 " if (callback)" |
| 104 " callback(error);" |
| 105 " };" |
| 106 " function dispatch(methodName, argumentCount)" |
| 107 " {" |
| 108 " var callId = ++host._lastCallId;" |
| 109 " var argsArray = Array.prototype.slice.call(arguments, 2);" |
| 110 " var callback = argsArray[argsArray.length - 1];" |
| 111 " if (typeof callback === \"function\") {" |
| 112 " argsArray.pop();" |
| 113 " host._callbacks[callId] = callback;" |
| 114 " }" |
| 115 " var message = { \"id\": callId, \"method\": methodName };" |
| 116 " argsArray = argsArray.slice(0, argumentCount);" |
| 117 " if (argsArray.length)" |
| 118 " message.params = argsArray;" |
| 119 " host.sendMessageToEmbedder(JSON.stringify(message));" |
| 120 " };" |
| 121 " methodEntries.forEach(function(methodEntry) { host[methodEntry[
0]] = dispatch.bind(null, methodEntry[0], methodEntry[1]); });" |
| 122 "})(InspectorFrontendHost," |
| 123 " [['addFileSystem', 0]," |
| 124 " ['append', 2]," |
| 125 " ['bringToFront', 0]," |
| 126 " ['closeWindow', 0]," |
| 127 " ['indexPath', 2]," |
| 128 " ['inspectElementCompleted', 0]," |
| 129 " ['inspectedURLChanged', 1]," |
| 130 " ['moveWindowBy', 2]," |
| 131 " ['openInNewTab', 1]," |
| 132 " ['openUrlOnRemoteDeviceAndInspect', 2]," |
| 133 " ['removeFileSystem', 1]," |
| 134 " ['requestFileSystems', 0]," |
| 135 " ['resetZoom', 0]," |
| 136 " ['save', 3]," |
| 137 " ['searchInPath', 3]," |
| 138 " ['setDeviceCountUpdatesEnabled', 1]," |
| 139 " ['setDevicesUpdatesEnabled', 1]," |
| 140 " ['setWhitelistedShortcuts', 1]," |
| 141 " ['setContentsResizingStrategy', 2]," |
| 142 " ['setInspectedPageBounds', 1]," |
| 143 " ['setIsDocked', 1]," |
| 144 " ['stopIndexing', 1]," |
| 145 " ['zoomIn', 0]," |
| 146 " ['zoomOut', 0]]);" |
| 147 "" |
| 148 "" // Support for legacy front-ends (<M34). Do not add items here. |
| 149 "InspectorFrontendHost.requestSetDockSide = function(dockSide)" |
| 150 "{" |
| 151 " InspectorFrontendHost.setIsDocked(dockSide !== \"undocked\");" |
| 152 "};" |
| 153 "InspectorFrontendHost.supportsFileSystems = function() { return tru
e; };" |
| 154 "" |
| 155 "" // Support for legacy front-ends (<M28). Do not add items here. |
| 156 "InspectorFrontendHost.canInspectWorkers = function() { return true;
};" |
| 157 "InspectorFrontendHost.canSaveAs = function() { return true; };" |
| 158 "InspectorFrontendHost.canSave = function() { return true; };" |
| 159 "InspectorFrontendHost.loaded = function() {};" |
| 160 "InspectorFrontendHost.hiddenPanels = function() { return ""; };" |
| 161 "InspectorFrontendHost.localizedStringsURL = function() { return "";
};" |
| 162 "InspectorFrontendHost.close = function(url) { };"; |
| 163 scriptController->executeScriptInMainWorld(installAdditionalAPI, ScriptC
ontroller::ExecuteScriptWhenScriptsDisabled); |
| 130 } | 164 } |
| 131 } | 165 } |
| 132 | 166 |
| 133 void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& mes
sage) | 167 void WebDevToolsFrontendImpl::sendMessageToBackend(const String& message) |
| 134 { | 168 { |
| 135 WebLocalFrameImpl* frame = m_webViewImpl->mainFrameImpl(); | 169 if (m_client) |
| 136 if (!frame->frame()) | 170 m_client->sendMessageToBackend(message); |
| 137 return; | 171 } |
| 138 v8::Isolate* isolate = toIsolate(frame->frame()); | 172 |
| 139 ScriptState* scriptState = ScriptState::forMainWorld(frame->frame()); | 173 void WebDevToolsFrontendImpl::sendMessageToEmbedder(const String& message) |
| 140 ScriptState::Scope scope(scriptState); | 174 { |
| 141 v8::Handle<v8::Value> inspectorFrontendApiValue = scriptState->context()->Gl
obal()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI")); | 175 if (m_client) |
| 142 if (!inspectorFrontendApiValue->IsObject()) | 176 m_client->sendMessageToEmbedder(message); |
| 143 return; | 177 } |
| 144 v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspe
ctorFrontendApiValue); | 178 |
| 145 v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::N
ewFromUtf8(isolate, "dispatchMessage")); | 179 bool WebDevToolsFrontendImpl::isUnderTest() |
| 146 // The frame might have navigated away from the front-end page (which is sti
ll weird), | 180 { |
| 147 // OR the older version of frontend might have a dispatch method in a differ
ent place. | 181 return m_client ? m_client->isUnderTest() : false; |
| 148 // FIXME(kaznacheev): Remove when Chrome for Android M18 is retired. | |
| 149 if (!dispatchFunction->IsFunction()) { | |
| 150 v8::Handle<v8::Value> inspectorBackendApiValue = scriptState->context()-
>Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend")); | |
| 151 if (!inspectorBackendApiValue->IsObject()) | |
| 152 return; | |
| 153 dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue
); | |
| 154 dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate
, "dispatch")); | |
| 155 if (!dispatchFunction->IsFunction()) | |
| 156 return; | |
| 157 } | |
| 158 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchF
unction); | |
| 159 Vector< v8::Handle<v8::Value> > args; | |
| 160 args.append(v8String(isolate, message)); | |
| 161 v8::TryCatch tryCatch; | |
| 162 tryCatch.SetVerbose(true); | |
| 163 ScriptController::callFunction(frame->frame()->document(), function, dispatc
herObject, args.size(), args.data(), isolate); | |
| 164 } | 182 } |
| 165 | 183 |
| 166 } // namespace blink | 184 } // namespace blink |
| OLD | NEW |