Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorFrontendHost.cpp
index 20401f6..f88bebe 100644 (file)
 #include "config.h"
 #include "core/inspector/InspectorFrontendHost.h"
 
-#include "bindings/v8/ScriptFunctionCall.h"
-#include "bindings/v8/ScriptState.h"
-#include "core/dom/Pasteboard.h"
+#include "bindings/core/v8/ScriptFunctionCall.h"
+#include "bindings/core/v8/ScriptState.h"
+#include "core/clipboard/Pasteboard.h"
+#include "core/dom/ExecutionContext.h"
+#include "core/events/Event.h"
+#include "core/events/EventTarget.h"
 #include "core/fetch/ResourceFetcher.h"
-#include "core/frame/Frame.h"
+#include "core/frame/LocalDOMWindow.h"
+#include "core/frame/LocalFrame.h"
 #include "core/html/parser/TextResourceDecoder.h"
 #include "core/inspector/InspectorController.h"
 #include "core/inspector/InspectorFrontendClient.h"
 #include "core/page/ContextMenuProvider.h"
 #include "core/page/Page.h"
 #include "core/rendering/RenderTheme.h"
-#include "modules/filesystem/DOMFileSystem.h"
 #include "platform/ContextMenu.h"
 #include "platform/ContextMenuItem.h"
-#include "platform/JSONValues.h"
 #include "platform/SharedBuffer.h"
 #include "platform/UserGestureIndicator.h"
 #include "platform/network/ResourceError.h"
 #include "platform/network/ResourceRequest.h"
 #include "platform/network/ResourceResponse.h"
 
-namespace WebCore {
+namespace blink {
 
 class FrontendMenuProvider FINAL : public ContextMenuProvider {
 public:
-    static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
+    static PassRefPtr<FrontendMenuProvider> create(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
     {
         return adoptRef(new FrontendMenuProvider(frontendHost, frontendApiObject, items));
     }
 
     void disconnect()
     {
-        m_frontendApiObject = ScriptObject();
+        m_frontendApiObject = ScriptValue();
         m_frontendHost = 0;
     }
 
 private:
-    FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptObject frontendApiObject, const Vector<ContextMenuItem>& items)
+    FrontendMenuProvider(InspectorFrontendHost* frontendHost, ScriptValue frontendApiObject, const Vector<ContextMenuItem>& items)
         : m_frontendHost(frontendHost)
         , m_frontendApiObject(frontendApiObject)
         , m_items(items)
@@ -108,10 +110,11 @@ private:
             m_frontendHost->m_menuProvider = 0;
         }
         m_items.clear();
+        m_frontendHost = 0;
     }
 
     InspectorFrontendHost* m_frontendHost;
-    ScriptObject m_frontendApiObject;
+    ScriptValue m_frontendApiObject;
     Vector<ContextMenuItem> m_items;
 };
 
@@ -120,7 +123,6 @@ InspectorFrontendHost::InspectorFrontendHost(InspectorFrontendClient* client, Pa
     , m_frontendPage(frontendPage)
     , m_menuProvider(0)
 {
-    ScriptWrappable::init(this);
 }
 
 InspectorFrontendHost::~InspectorFrontendHost()
@@ -128,32 +130,40 @@ InspectorFrontendHost::~InspectorFrontendHost()
     ASSERT(!m_client);
 }
 
+void InspectorFrontendHost::trace(Visitor* visitor)
+{
+    visitor->trace(m_frontendPage);
+}
+
 void InspectorFrontendHost::disconnectClient()
 {
     m_client = 0;
     if (m_menuProvider)
         m_menuProvider->disconnect();
-    m_frontendPage = 0;
+    m_frontendPage = nullptr;
 }
 
 void InspectorFrontendHost::setZoomFactor(float zoom)
 {
-    m_frontendPage->mainFrame()->setPageAndTextZoomFactors(zoom, 1);
+    if (!m_frontendPage)
+        return;
+    if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
+        frame->setPageAndTextZoomFactors(zoom, 1);
 }
 
 float InspectorFrontendHost::zoomFactor()
 {
-    return m_frontendPage->mainFrame()->pageZoomFactor();
-}
-
-void InspectorFrontendHost::inspectedURLChanged(const String& newURL)
-{
-    if (m_client)
-        m_client->inspectedURLChanged(newURL);
+    if (!m_frontendPage)
+        return 1;
+    if (LocalFrame* frame = m_frontendPage->deprecatedLocalMainFrame())
+        return frame->pageZoomFactor();
+    return 1;
 }
 
 void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
 {
+    if (!m_frontendPage)
+        return;
     m_frontendPage->inspectorController().setInjectedScriptForOrigin(origin, script);
 }
 
@@ -162,16 +172,54 @@ void InspectorFrontendHost::copyText(const String& text)
     Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
 }
 
+static String escapeUnicodeNonCharacters(const String& str)
+{
+    const UChar nonChar = 0xD800;
+
+    unsigned i = 0;
+    while (i < str.length() && str[i] < nonChar)
+        ++i;
+    if (i == str.length())
+        return str;
+
+    StringBuilder dst;
+    dst.append(str, 0, i);
+    for (; i < str.length(); ++i) {
+        UChar c = str[i];
+        if (c >= nonChar) {
+            unsigned symbol = static_cast<unsigned>(c);
+            String symbolCode = String::format("\\u%04X", symbol);
+            dst.append(symbolCode);
+        } else {
+            dst.append(c);
+        }
+    }
+    return dst.toString();
+}
+
 void InspectorFrontendHost::sendMessageToBackend(const String& message)
 {
     if (m_client)
-        m_client->sendMessageToBackend(message);
+        m_client->sendMessageToBackend(escapeUnicodeNonCharacters(message));
 }
 
 void InspectorFrontendHost::sendMessageToEmbedder(const String& message)
 {
     if (m_client)
-        m_client->sendMessageToEmbedder(message);
+        m_client->sendMessageToEmbedder(escapeUnicodeNonCharacters(message));
+}
+
+void InspectorFrontendHost::showContextMenu(Page* page, float x, float y, const Vector<ContextMenuItem>& items)
+{
+    ASSERT(m_frontendPage);
+    ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
+    ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
+    ASSERT(frontendApiObject.isObject());
+
+    RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
+    m_menuProvider = menuProvider.get();
+    float zoom = page->deprecatedLocalMainFrame()->pageZoomFactor();
+    page->inspectorController().showContextMenu(x * zoom, y * zoom, menuProvider);
 }
 
 void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMenuItem>& items)
@@ -180,14 +228,19 @@ void InspectorFrontendHost::showContextMenu(Event* event, const Vector<ContextMe
         return;
 
     ASSERT(m_frontendPage);
-    ScriptState* frontendScriptState = mainWorldScriptState(m_frontendPage->mainFrame());
-    ScriptObject frontendApiObject;
-    if (!ScriptGlobalObject::get(frontendScriptState, "InspectorFrontendAPI", frontendApiObject)) {
-        ASSERT_NOT_REACHED();
-        return;
+    ScriptState* frontendScriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
+    ScriptValue frontendApiObject = frontendScriptState->getFromGlobalObject("InspectorFrontendAPI");
+    ASSERT(frontendApiObject.isObject());
+
+    Page* targetPage = m_frontendPage;
+    if (event->target() && event->target()->executionContext() && event->target()->executionContext()->executingWindow()) {
+        LocalDOMWindow* window = event->target()->executionContext()->executingWindow();
+        if (window->document() && window->document()->page())
+            targetPage = window->document()->page();
     }
+
     RefPtr<FrontendMenuProvider> menuProvider = FrontendMenuProvider::create(this, frontendApiObject, items);
-    m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
+    targetPage->contextMenuController().showContextMenu(event, menuProvider);
     m_menuProvider = menuProvider.get();
 }
 
@@ -201,28 +254,14 @@ String InspectorFrontendHost::getSelectionForegroundColor()
     return RenderTheme::theme().activeSelectionForegroundColor().serialized();
 }
 
-PassRefPtr<DOMFileSystem> InspectorFrontendHost::isolatedFileSystem(const String& fileSystemName, const String& rootURL)
-{
-    ExecutionContext* context = m_frontendPage->mainFrame()->document();
-    return DOMFileSystem::create(context, fileSystemName, FileSystemTypeIsolated, KURL(ParsedURLString, rootURL));
-}
-
-void InspectorFrontendHost::upgradeDraggedFileSystemPermissions(DOMFileSystem* domFileSystem)
+bool InspectorFrontendHost::isUnderTest()
 {
-    if (!m_client)
-        return;
-    RefPtr<JSONObject> message = JSONObject::create();
-    message->setNumber("id", 0);
-    message->setString("method", "upgradeDraggedFileSystemPermissions");
-    RefPtr<JSONArray> params = JSONArray::create();
-    message->setArray("params", params);
-    params->pushString(domFileSystem->rootURL().string());
-    sendMessageToEmbedder(message->toJSONString());
+    return m_client && m_client->isUnderTest();
 }
 
-bool InspectorFrontendHost::isUnderTest()
+bool InspectorFrontendHost::isHostedMode()
 {
-    return m_client && m_client->isUnderTest();
+    return false;
 }
 
-} // namespace WebCore
+} // namespace blink