Web Inspector: Encapsulate SetEmbedderData/GetEmbedderData
https://bugs.webkit.org/show_bug.cgi?id=113331

Patch by John J. Barton <johnjbarton@chromium.org> on 2013-04-02
Reviewed by Pavel Feldman.

Refactor to encapsulate uses of V8 Set/GetEmbedderData together
with uses of V8 Set/GetAlignedPointerInEmbedderData since these
functions operate on the same underyling array in V8.

* bindings/v8/ScriptController.cpp:
(WebCore::ScriptController::setContextDebugId):
(WebCore::ScriptController::contextDebugId):
* bindings/v8/V8DOMWindowShell.cpp:
(WebCore::setInjectedScriptContextDebugId):
* bindings/v8/V8PerContextData.cpp:
(WebCore::V8PerContextDebugData::setContextDebugData):
(WebCore):
(WebCore::V8PerContextDebugData::contextDebugId):
(WebCore::V8PerContextDebugData::createDebugData):
(WebCore::V8PerContextDebugData::getDebugData):
(WebCore::V8PerContextDebugData::setDebugData):
* bindings/v8/V8PerContextData.h:
(V8PerContextDebugData):
(WebCore):

git-svn-id: svn://svn.chromium.org/blink/trunk@147475 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 330a3bf..3089e18 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,30 @@
+2013-04-02  John J. Barton  <johnjbarton@chromium.org>
+
+        Web Inspector: Encapsulate SetEmbedderData/GetEmbedderData
+        https://bugs.webkit.org/show_bug.cgi?id=113331
+
+        Reviewed by Pavel Feldman.
+
+        Refactor to encapsulate uses of V8 Set/GetEmbedderData together
+        with uses of V8 Set/GetAlignedPointerInEmbedderData since these
+        functions operate on the same underyling array in V8.
+
+        * bindings/v8/ScriptController.cpp:
+        (WebCore::ScriptController::setContextDebugId):
+        (WebCore::ScriptController::contextDebugId):
+        * bindings/v8/V8DOMWindowShell.cpp:
+        (WebCore::setInjectedScriptContextDebugId):
+        * bindings/v8/V8PerContextData.cpp:
+        (WebCore::V8PerContextDebugData::setContextDebugData):
+        (WebCore):
+        (WebCore::V8PerContextDebugData::contextDebugId):
+        (WebCore::V8PerContextDebugData::createDebugData):
+        (WebCore::V8PerContextDebugData::getDebugData):
+        (WebCore::V8PerContextDebugData::setDebugData):
+        * bindings/v8/V8PerContextData.h:
+        (V8PerContextDebugData):
+        (WebCore):
+
 2013-04-02  Alexei Filippov  <alph@chromium.org>
 
         Web Inspector: Adjust toolbar labels position
diff --git a/Source/WebCore/bindings/v8/ScriptController.cpp b/Source/WebCore/bindings/v8/ScriptController.cpp
index bfb8341..4723d37 100644
--- a/Source/WebCore/bindings/v8/ScriptController.cpp
+++ b/Source/WebCore/bindings/v8/ScriptController.cpp
@@ -64,6 +64,7 @@
 #include "V8HiddenPropertyName.h"
 #include "V8HTMLEmbedElement.h"
 #include "V8NPObject.h"
+#include "V8PerContextData.h"
 #include "V8RecursionScope.h"
 #include "Widget.h"
 #include "npruntime_impl.h"
@@ -707,29 +708,12 @@
         return false;
     v8::HandleScope scope;
     v8::Handle<v8::Context> context = m_windowShell->context();
-    if (!context->GetEmbedderData(0)->IsUndefined())
-        return false;
-
-    v8::Context::Scope contextScope(context);
-
-    char buffer[32];
-    snprintf(buffer, sizeof(buffer), "page,%d", debugId);
-    buffer[31] = 0;
-    context->SetEmbedderData(0, v8::String::NewSymbol(buffer));
-
-    return true;
+    return V8PerContextDebugData::setContextDebugData(context, "page", debugId);
 }
 
 int ScriptController::contextDebugId(v8::Handle<v8::Context> context)
 {
-    v8::HandleScope scope;
-    if (!context->GetEmbedderData(0)->IsString())
-        return -1;
-    v8::String::AsciiValue ascii(context->GetEmbedderData(0));
-    char* comma = strnstr(*ascii, ",", ascii.length());
-    if (!comma)
-        return -1;
-    return atoi(comma + 1);
+    return V8PerContextDebugData::contextDebugId(context);
 }
 
 void ScriptController::attachDebugger(void*)
diff --git a/Source/WebCore/bindings/v8/V8DOMWindowShell.cpp b/Source/WebCore/bindings/v8/V8DOMWindowShell.cpp
index 6832d12..439adc3 100644
--- a/Source/WebCore/bindings/v8/V8DOMWindowShell.cpp
+++ b/Source/WebCore/bindings/v8/V8DOMWindowShell.cpp
@@ -78,12 +78,7 @@
 
 static void setInjectedScriptContextDebugId(v8::Handle<v8::Context> targetContext, int debugId)
 {
-    char buffer[32];
-    if (debugId == -1)
-        snprintf(buffer, sizeof(buffer), "injected");
-    else
-        snprintf(buffer, sizeof(buffer), "injected,%d", debugId);
-    targetContext->SetEmbedderData(0, v8::String::NewSymbol(buffer));
+    V8PerContextDebugData::setContextDebugData(targetContext, "injected", debugId);
 }
 
 PassOwnPtr<V8DOMWindowShell> V8DOMWindowShell::create(Frame* frame, PassRefPtr<DOMWrapperWorld> world, v8::Isolate* isolate)
diff --git a/Source/WebCore/bindings/v8/V8PerContextData.cpp b/Source/WebCore/bindings/v8/V8PerContextData.cpp
index ee8a0e6..95c725f 100644
--- a/Source/WebCore/bindings/v8/V8PerContextData.cpp
+++ b/Source/WebCore/bindings/v8/V8PerContextData.cpp
@@ -33,6 +33,7 @@
 
 #include "V8Binding.h"
 #include "V8ObjectConstructor.h"
+#include <wtf/StringExtras.h>
 
 namespace WebCore {
 
@@ -138,5 +139,55 @@
 
     return function;
 }
+static v8::Handle<v8::Value> createDebugData(const char* worldName, int debugId) 
+{
+    char buffer[32];
+    unsigned wanted;
+    if (debugId == -1)
+        wanted = snprintf(buffer, sizeof(buffer), "%s", worldName);
+    else 
+        wanted = snprintf(buffer, sizeof(buffer), "%s,%d", worldName, debugId);
+
+    if (wanted < sizeof(buffer))
+        return v8::String::NewSymbol(buffer);
+
+    return v8::Undefined();
+};
+
+static v8::Handle<v8::Value> debugData(v8::Handle<v8::Context> context)
+{
+    v8::Context::Scope contextScope(context);
+    return context->GetEmbedderData(v8ContextDebugIdIndex);
+}
+
+static void setDebugData(v8::Handle<v8::Context> context, v8::Handle<v8::Value> value)
+{
+    v8::Context::Scope contextScope(context);
+    context->SetEmbedderData(v8ContextDebugIdIndex, value);
+}
+
+bool V8PerContextDebugData::setContextDebugData(v8::Handle<v8::Context> context, const char* worldName, int debugId) 
+{
+    if (!debugData(context)->IsUndefined())
+        return false;
+    v8::HandleScope scope;
+    v8::Handle<v8::Value> debugData = createDebugData(worldName, debugId);
+    setDebugData(context, debugData);
+    return true;
+}
+
+int V8PerContextDebugData::contextDebugId(v8::Handle<v8::Context> context) 
+{
+    v8::HandleScope scope;
+    v8::Handle<v8::Value> data = debugData(context);
+
+    if (!data->IsString())
+        return -1;
+    v8::String::AsciiValue ascii(data);
+    char* comma = strnstr(*ascii, ",", ascii.length());
+    if (!comma)
+        return -1;
+    return atoi(comma + 1);
+}
 
 } // namespace WebCore
diff --git a/Source/WebCore/bindings/v8/V8PerContextData.h b/Source/WebCore/bindings/v8/V8PerContextData.h
index 0ac7395..1dd1de0 100644
--- a/Source/WebCore/bindings/v8/V8PerContextData.h
+++ b/Source/WebCore/bindings/v8/V8PerContextData.h
@@ -133,6 +133,12 @@
     ScopedPersistent<v8::Value> m_objectPrototype;
 };
 
+class V8PerContextDebugData {
+public:
+    static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
+    static int contextDebugId(v8::Handle<v8::Context>);
+};
+
 } // namespace WebCore
 
 #endif // V8PerContextData_h