Web Inspector: Add command for selecting files for file input element
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 21 Feb 2013 09:18:19 +0000 (09:18 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 21 Feb 2013 09:18:19 +0000 (09:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=109308

Patch by Ken Kania <kkania@chromium.org> on 2013-02-21
Reviewed by Pavel Feldman.

Source/WebCore:

Test: inspector-protocol/dom/setFileInputFiles.html

* inspector/Inspector.json:
* inspector/InspectorClient.h:
(WebCore::InspectorClient::canSetFileInputFiles):
(InspectorClient):
* inspector/InspectorController.cpp:
(WebCore::InspectorController::InspectorController):
* inspector/InspectorDOMAgent.cpp:
(WebCore::InspectorDOMAgent::InspectorDOMAgent):
(WebCore::InspectorDOMAgent::setFileInputFiles):
(WebCore):
* inspector/InspectorDOMAgent.h:
(WebCore):
(WebCore::InspectorDOMAgent::create):
(InspectorDOMAgent):

Source/WebKit/chromium:

* src/InspectorClientImpl.cpp:
(WebKit::InspectorClientImpl::canSetFileInputFiles):
(WebKit):
* src/InspectorClientImpl.h:
(InspectorClientImpl):

LayoutTests:

* inspector-protocol/dom/setFileInputFiles-expected.txt: Added.
* inspector-protocol/dom/setFileInputFiles.html: Added.
* platform/chromium/inspector-protocol/dom/setFileInputFiles-expected.txt: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@143574 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
LayoutTests/ChangeLog
LayoutTests/inspector-protocol/dom/setFileInputFiles-expected.txt [new file with mode: 0644]
LayoutTests/inspector-protocol/dom/setFileInputFiles.html [new file with mode: 0644]
LayoutTests/platform/chromium/inspector-protocol/dom/setFileInputFiles-expected.txt [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/inspector/Inspector.json
Source/WebCore/inspector/InspectorClient.h
Source/WebCore/inspector/InspectorController.cpp
Source/WebCore/inspector/InspectorDOMAgent.cpp
Source/WebCore/inspector/InspectorDOMAgent.h
Source/WebKit/chromium/ChangeLog
Source/WebKit/chromium/src/InspectorClientImpl.cpp
Source/WebKit/chromium/src/InspectorClientImpl.h

index 578700965cea8e6701ea219e5a3d30c0d9e7dd2b..0194de35b4ca48ceb273968ba8bf4b4a2a387d67 100644 (file)
@@ -1,3 +1,14 @@
+2013-02-21  Ken Kania  <kkania@chromium.org>
+
+        Web Inspector: Add command for selecting files for file input element
+        https://bugs.webkit.org/show_bug.cgi?id=109308
+
+        Reviewed by Pavel Feldman.
+
+        * inspector-protocol/dom/setFileInputFiles-expected.txt: Added.
+        * inspector-protocol/dom/setFileInputFiles.html: Added.
+        * platform/chromium/inspector-protocol/dom/setFileInputFiles-expected.txt: Added.
+
 2013-02-21  Pan Deng  <pan.deng@intel.com>
 
         [Web Inspector]Add WebSocket networking events in WebInspector Timeline panel.
diff --git a/LayoutTests/inspector-protocol/dom/setFileInputFiles-expected.txt b/LayoutTests/inspector-protocol/dom/setFileInputFiles-expected.txt
new file mode 100644 (file)
index 0000000..61739f3
--- /dev/null
@@ -0,0 +1,4 @@
+Received error: Cannot set file input files
+Received error: Cannot set file input files
+
diff --git a/LayoutTests/inspector-protocol/dom/setFileInputFiles.html b/LayoutTests/inspector-protocol/dom/setFileInputFiles.html
new file mode 100644 (file)
index 0000000..a7ac046
--- /dev/null
@@ -0,0 +1,69 @@
+<html>
+<head>
+<script type="text/javascript" src="../../http/tests/inspector-protocol/resources/protocol-test.js"></script>
+<script>
+
+function onLoad()
+{
+    document.querySelector("input").addEventListener("change", onChange);
+
+    function onChange(event)
+    {
+        var files = event.target.files;
+        log("File count: " + files.length);
+        for (var i = 0; i < files.length; i++) {
+            log("Name: " + files[i].name);
+        }
+    }
+
+    runTest();
+}
+
+function test()
+{
+    function abortOnError(message)
+    {
+        if (message.error) {
+            InspectorTest.log(message.error.message);
+            InspectorTest.completeTest();
+        }
+    }
+
+    InspectorTest.sendCommand("DOM.getDocument", {}, onGotDocument);
+
+    function onGotDocument(message)
+    {
+        abortOnError(message);
+        var node = message.result.root;
+        InspectorTest.sendCommand("DOM.querySelectorAll", { "nodeId": node.nodeId, "selector": "input" }, onQuerySelectorAll);
+    }
+
+    function onQuerySelectorAll(message)
+    {
+        abortOnError(message);
+        var ids = message.result.nodeIds;
+        InspectorTest.sendCommand("DOM.setFileInputFiles", { "nodeId": ids[0], "files": ["file1", "file2"] }, onSetFiles);
+        InspectorTest.sendCommand("DOM.setFileInputFiles", { "nodeId": ids[1], "files": ["file1", "file2"] }, onBadSetFiles);
+    }
+
+    function onSetFiles(message)
+    {
+        if (message.error) {
+            InspectorTest.log("Received error: " + message.error.message);
+        }
+    }
+
+    function onBadSetFiles(message)
+    {
+        InspectorTest.log("Received error: " + message.error.message);
+        InspectorTest.completeTest();
+    }
+}
+
+</script>
+</head>
+<body onload="onLoad()">
+<input type="file"></input>
+<input type="text"></input>
+</body>
+</html>
diff --git a/LayoutTests/platform/chromium/inspector-protocol/dom/setFileInputFiles-expected.txt b/LayoutTests/platform/chromium/inspector-protocol/dom/setFileInputFiles-expected.txt
new file mode 100644 (file)
index 0000000..5bacd37
--- /dev/null
@@ -0,0 +1,6 @@
+File count: 2
+Name: file1
+Name: file2
+Received error: Node is not a file input element
+
index 392271f61a8db3c980d2d17670e4a36976c92aef..88994ed022cc0c1e42a8d0d2185b6dbcd8f8e42a 100644 (file)
@@ -1,3 +1,27 @@
+2013-02-21  Ken Kania  <kkania@chromium.org>
+
+        Web Inspector: Add command for selecting files for file input element
+        https://bugs.webkit.org/show_bug.cgi?id=109308
+
+        Reviewed by Pavel Feldman.
+
+        Test: inspector-protocol/dom/setFileInputFiles.html
+
+        * inspector/Inspector.json:
+        * inspector/InspectorClient.h:
+        (WebCore::InspectorClient::canSetFileInputFiles):
+        (InspectorClient):
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::InspectorController):
+        * inspector/InspectorDOMAgent.cpp:
+        (WebCore::InspectorDOMAgent::InspectorDOMAgent):
+        (WebCore::InspectorDOMAgent::setFileInputFiles):
+        (WebCore):
+        * inspector/InspectorDOMAgent.h:
+        (WebCore):
+        (WebCore::InspectorDOMAgent::create):
+        (InspectorDOMAgent):
+
 2013-02-21  Pan Deng  <pan.deng@intel.com>
 
         [Web Inspector]Add WebSocket networking events in Timeline panel.
index 3200ba9d10b31e4937a9db4326bb57e92b785732..70ab7ee427e1ce7fbf9082f7520546040b01124c 100644 (file)
             {
                 "name": "focus",
                 "parameters": [
-                    { "name": "nodeId", "$ref": "DOM.NodeId", "description": "Id of the node to focus." }
+                    { "name": "nodeId", "$ref": "NodeId", "description": "Id of the node to focus." }
                 ],
                 "description": "Focuses the given element.",
                 "hidden": true
+            },
+            {
+                "name": "setFileInputFiles",
+                "parameters": [
+                    { "name": "nodeId", "$ref": "NodeId", "description": "Id of the file input node to set files for." },
+                    { "name": "files", "type": "array", "items": { "type": "string" }, "description": "Array of file paths to set." }
+                ],
+                "description": "Sets files for the given file input element.",
+                "hidden": true
             }
         ],
         "events": [
index c86e70b3a09735ad241488fe49b520f9b7f5aeb6..94c2e947600f085eb702f3810318505289cf7223 100644 (file)
@@ -91,6 +91,8 @@ public:
 
     virtual bool handleJavaScriptDialog(bool) { return false; }
 
+    virtual bool canSetFileInputFiles() { return false; }
+
     static bool doDispatchMessageOnFrontendPage(Page* frontendPage, const String& message);
 };
 
index 85483623101ed7a58f8c2b4950e551c2831030dc..0b6588de768a89d14d85bcba1c0c21e92397c978 100644 (file)
@@ -98,7 +98,7 @@ InspectorController::InspectorController(Page* page, InspectorClient* inspectorC
     m_pageAgent = pageAgentPtr.get();
     m_agents.append(pageAgentPtr.release());
 
-    OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), m_injectedScriptManager.get(), m_overlay.get()));
+    OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), m_injectedScriptManager.get(), m_overlay.get(), inspectorClient));
     m_domAgent = domAgentPtr.get();
     m_agents.append(domAgentPtr.release());
 
index 2a2969244e1141116a77ab584b58247250e951ea..dfa70ca89ed42c796d282df3bf6bc41372d977f5 100644 (file)
 #include "EventListener.h"
 #include "EventNames.h"
 #include "EventTarget.h"
+#include "File.h"
+#include "FileList.h"
 #include "Frame.h"
 #include "FrameTree.h"
 #include "HTMLElement.h"
 #include "HTMLFrameOwnerElement.h"
+#include "HTMLInputElement.h"
 #include "HTMLNames.h"
 #include "HTMLTemplateElement.h"
 #include "HitTestResult.h"
 #include "IdentifiersFactory.h"
 #include "InjectedScriptManager.h"
+#include "InspectorClient.h"
 #include "InspectorFrontend.h"
 #include "InspectorHistory.h"
 #include "InspectorOverlay.h"
@@ -194,11 +198,12 @@ String InspectorDOMAgent::toErrorString(const ExceptionCode& ec)
     return "";
 }
 
-InspectorDOMAgent::InspectorDOMAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
+InspectorDOMAgent::InspectorDOMAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay, InspectorClient* client)
     : InspectorBaseAgent<InspectorDOMAgent>("DOM", instrumentingAgents, inspectorState)
     , m_pageAgent(pageAgent)
     , m_injectedScriptManager(injectedScriptManager)
     , m_overlay(overlay)
+    , m_client(client)
     , m_frontend(0)
     , m_domListener(0)
     , m_lastNodeId(1)
@@ -1200,6 +1205,34 @@ void InspectorDOMAgent::focus(ErrorString* errorString, int nodeId)
     element->focus();
 }
 
+void InspectorDOMAgent::setFileInputFiles(ErrorString* errorString, int nodeId, const RefPtr<InspectorArray>& files)
+{
+    if (!m_client->canSetFileInputFiles()) {
+        *errorString = "Cannot set file input files";
+        return;
+    }
+
+    Node* node = assertNode(errorString, nodeId);
+    if (!node)
+        return;
+    HTMLInputElement* element = node->toInputElement();
+    if (!element || !element->isFileUpload()) {
+        *errorString = "Node is not a file input element";
+        return;
+    }
+
+    RefPtr<FileList> fileList = FileList::create();
+    for (InspectorArray::const_iterator iter = files->begin(); iter != files->end(); ++iter) {
+        String path;
+        if (!(*iter)->asString(&path)) {
+            *errorString = "Files must be strings";
+            return;
+        }
+        fileList->append(File::create(path));
+    }
+    element->setFiles(fileList);
+}
+
 void InspectorDOMAgent::resolveNode(ErrorString* errorString, int nodeId, const String* const objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result)
 {
     String objectGroupName = objectGroup ? *objectGroup : "";
index cc02e6a5e4538259fece248b352c64d7c0e09f9b..0f98fab6cc0212512570bb657850abff8fd69817 100644 (file)
@@ -57,6 +57,7 @@ class DOMEditor;
 class Document;
 class Element;
 class Event;
+class InspectorClient;
 class InspectorFrontend;
 class InspectorHistory;
 class InspectorOverlay;
@@ -102,9 +103,9 @@ public:
         virtual void didModifyDOMAttr(Element*) = 0;
     };
 
-    static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
+    static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay, InspectorClient* client)
     {
-        return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, inspectorState, injectedScriptManager, overlay));
+        return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, inspectorState, injectedScriptManager, overlay, client));
     }
 
     static String toErrorString(const ExceptionCode&);
@@ -149,6 +150,7 @@ public:
     virtual void redo(ErrorString*);
     virtual void markUndoableState(ErrorString*);
     virtual void focus(ErrorString*, int nodeId);
+    virtual void setFileInputFiles(ErrorString*, int nodeId, const RefPtr<InspectorArray>& files);
 
     void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
 
@@ -204,7 +206,7 @@ public:
     int pushNodePathForRenderLayerToFrontend(const RenderLayer*);
 
 private:
-    InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*);
+    InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*, InspectorClient*);
 
     void setSearchingForNode(ErrorString*, bool enabled, InspectorObject* highlightConfig);
     PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, InspectorObject* highlightInspectorObject);
@@ -236,6 +238,7 @@ private:
     InspectorPageAgent* m_pageAgent;
     InjectedScriptManager* m_injectedScriptManager;
     InspectorOverlay* m_overlay;
+    InspectorClient* m_client;
     InspectorFrontend::DOM* m_frontend;
     DOMListener* m_domListener;
     NodeToIdMap m_documentNodeToIdMap;
index 08c93d3019ece2da3f75d584933591adf56e52a1..4972e59d0d317d4cc993b956441bc699172d5dff 100644 (file)
@@ -1,3 +1,16 @@
+2013-02-21  Ken Kania  <kkania@chromium.org>
+
+        Web Inspector: Add command for selecting files for file input element
+        https://bugs.webkit.org/show_bug.cgi?id=109308
+
+        Reviewed by Pavel Feldman.
+
+        * src/InspectorClientImpl.cpp:
+        (WebKit::InspectorClientImpl::canSetFileInputFiles):
+        (WebKit):
+        * src/InspectorClientImpl.h:
+        (InspectorClientImpl):
+
 2013-02-20  PaweĊ‚ Hajdan, Jr.  <phajdan.jr@chromium.org>
 
         Add gyp option to switch ENABLE(SQL_DATABASE)
index 06fcda9641c8563bb203726680b181b7c37be247..89763bdce6d7e1e38a06ea97454fdc8faf68c9b8 100644 (file)
@@ -228,6 +228,11 @@ bool InspectorClientImpl::handleJavaScriptDialog(bool accept)
     return false;
 }
 
+bool InspectorClientImpl::canSetFileInputFiles()
+{
+    return true;
+}
+
 WebDevToolsAgentImpl* InspectorClientImpl::devToolsAgent()
 {
     return static_cast<WebDevToolsAgentImpl*>(m_inspectedWebView->devToolsAgent());
index d256d20fb4b204915f6483ace4e4d202e2e515c7..794c66eb879a30625a2bfe97325235930ad179d2 100644 (file)
@@ -94,6 +94,8 @@ public:
 
     virtual bool handleJavaScriptDialog(bool accept);
 
+    virtual bool canSetFileInputFiles();
+
 private:
     WebDevToolsAgentImpl* devToolsAgent();