Changeset 121676 in webkit


Ignore:
Timestamp:
Jul 2, 2012 7:12:04 AM (12 years ago)
Author:
commit-queue@webkit.org
Message:

Web Inspector: Add requestFileContent command and fileContentReceived event
https://bugs.webkit.org/show_bug.cgi?id=89642

Patch by Taiju Tsuiki <tzik@chromium.org> on 2012-07-02
Reviewed by Vsevolod Vlasov.

Source/WebCore:

Test: http/tests/inspector/filesystem/read-file.html

  • inspector/Inspector.json:
  • inspector/InspectorFileSystemAgent.cpp:

(WebCore):
(WebCore::InspectorFileSystemAgent::requestFileContent):

  • inspector/InspectorFileSystemAgent.h:

(InspectorFileSystemAgent):

  • inspector/front-end/FileSystemModel.js:

(WebInspector.FileSystemModel.prototype.requestMetadata):
(WebInspector.FileSystemModel.prototype.requestFileContent):
(WebInspector.FileSystemModel.File.prototype.get resourceType):
(WebInspector.FileSystemModel.File.prototype.requestFileContent):
(WebInspector.FileSystemRequestManager):
(WebInspector.FileSystemRequestManager.prototype._metadataReceived):
(WebInspector.FileSystemRequestManager.prototype.requestFileContent.requestAccepted):
(WebInspector.FileSystemRequestManager.prototype.requestFileContent):
(WebInspector.FileSystemRequestManager.prototype._fileContentReceived):
(WebInspector.FileSystemDispatcher.prototype.metadataReceived):
(WebInspector.FileSystemDispatcher.prototype.fileContentReceived):

LayoutTests:

  • http/tests/inspector/filesystem/read-file-expected.txt: Added.
  • http/tests/inspector/filesystem/read-file.html: Added.
Location:
trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r121675 r121676  
     12012-07-02  Taiju Tsuiki  <tzik@chromium.org>
     2
     3        Web Inspector: Add requestFileContent command and fileContentReceived event
     4        https://bugs.webkit.org/show_bug.cgi?id=89642
     5
     6        Reviewed by Vsevolod Vlasov.
     7
     8        * http/tests/inspector/filesystem/read-file-expected.txt: Added.
     9        * http/tests/inspector/filesystem/read-file.html: Added.
     10
    1112012-07-02  Taiju Tsuiki  <tzik@chromium.org>
    212
  • trunk/Source/WebCore/ChangeLog

    r121675 r121676  
     12012-07-02  Taiju Tsuiki  <tzik@chromium.org>
     2
     3        Web Inspector: Add requestFileContent command and fileContentReceived event
     4        https://bugs.webkit.org/show_bug.cgi?id=89642
     5
     6        Reviewed by Vsevolod Vlasov.
     7
     8        Test: http/tests/inspector/filesystem/read-file.html
     9
     10        * inspector/Inspector.json:
     11        * inspector/InspectorFileSystemAgent.cpp:
     12        (WebCore):
     13        (WebCore::InspectorFileSystemAgent::requestFileContent):
     14        * inspector/InspectorFileSystemAgent.h:
     15        (InspectorFileSystemAgent):
     16        * inspector/front-end/FileSystemModel.js:
     17        (WebInspector.FileSystemModel.prototype.requestMetadata):
     18        (WebInspector.FileSystemModel.prototype.requestFileContent):
     19        (WebInspector.FileSystemModel.File.prototype.get resourceType):
     20        (WebInspector.FileSystemModel.File.prototype.requestFileContent):
     21        (WebInspector.FileSystemRequestManager):
     22        (WebInspector.FileSystemRequestManager.prototype._metadataReceived):
     23        (WebInspector.FileSystemRequestManager.prototype.requestFileContent.requestAccepted):
     24        (WebInspector.FileSystemRequestManager.prototype.requestFileContent):
     25        (WebInspector.FileSystemRequestManager.prototype._fileContentReceived):
     26        (WebInspector.FileSystemDispatcher.prototype.metadataReceived):
     27        (WebInspector.FileSystemDispatcher.prototype.fileContentReceived):
     28
    1292012-07-02  Taiju Tsuiki  <tzik@chromium.org>
    230
  • trunk/Source/WebCore/inspector/Inspector.json

    r121549 r121676  
    14721472                ],
    14731473                "description": "Returns metadata of the entry as metadataReceived event."
     1474            },
     1475            {
     1476                "name": "requestFileContent",
     1477                "parameters": [
     1478                    { "name": "url", "type": "string", "description": "URL of the file that the frontend is requesting to read from." },
     1479                    { "name": "start", "type": "integer", "optional": true, "description": "Specifies the start of range to read." },
     1480                    { "name": "end", "type": "integer", "optional": true, "description": "Specifies the end of range to read exclusively." }
     1481                ],
     1482                "returns": [
     1483                    { "name": "requestId", "$ref": "RequestId", "description": "Request identifier. Corresponding fileContentReceived event should have same requestId with this." }
     1484                ],
     1485                "description": "Returns content of the file as fileContentReceived event. Result should be sliced into [start, end)."
    14741486            }
    14751487        ],
     
    14961508                "name": "metadataReceived",
    14971509                "parameters": [
    1498                     { "name": "requestId", "type": "integer", "description": "Request Identifier that was returned in response to the corresponding getMetadata request." },
     1510                    { "name": "requestId", "type": "integer", "description": "Request Identifier that was returned in response to the corresponding requestMetadata command." },
    14991511                    { "name": "errorCode", "type": "integer", "description": "0, if no error. Otherwise, errorCode is set to FileError::ErrorCode value." },
    15001512                    { "name": "metadata", "$ref": "FileSystem.Metadata", "optional": true, "description": "Contains metadata of the entry if the command completed successfully." }
    15011513                ],
    1502                 "description": "Completion event of getMetadata command."
     1514                "description": "Completion event of requestMetadata command."
     1515            },
     1516            {
     1517                "name": "fileContentReceived",
     1518                "parameters": [
     1519                    { "name": "requestId", "type": "integer", "description": "Request Identifier that was returned in response to the corresponding requestFileContent command." },
     1520                    { "name": "errorCode", "type": "integer", "description": "0, if no error. Otherwise, errorCode is set to FileError::ErrorCode value." },
     1521                    { "name": "content", "type": "string", "optional": true, "description": "Contains content of the file as base64 encoded string." }
     1522                ],
     1523                "description": "Completion event of requestFileContent command."
    15031524            }
    15041525        ]
  • trunk/Source/WebCore/inspector/InspectorFileSystemAgent.cpp

    r121251 r121676  
    3535#include "InspectorFileSystemAgent.h"
    3636
     37#include "Base64.h"
    3738#include "DOMFileSystem.h"
    3839#include "DirectoryEntry.h"
     
    4041#include "Document.h"
    4142#include "EntriesCallback.h"
     43#include "Entry.h"
    4244#include "EntryArray.h"
    4345#include "EntryCallback.h"
    4446#include "ErrorCallback.h"
     47#include "FileCallback.h"
     48#include "FileEntry.h"
    4549#include "FileError.h"
     50#include "FileReader.h"
    4651#include "FileSystemCallback.h"
    4752#include "FileSystemCallbacks.h"
     
    5560#include "Metadata.h"
    5661#include "MetadataCallback.h"
     62#include "ScriptExecutionContext.h"
    5763#include "SecurityOrigin.h"
    5864
     
    401407}
    402408
     409class ReadFileTask : public EventListener {
     410    WTF_MAKE_NONCOPYABLE(ReadFileTask);
     411public:
     412    static PassRefPtr<ReadFileTask> create(PassRefPtr<FrontendProvider> frontendProvider, int requestId, const String& url, long long start, long long end)
     413    {
     414        return adoptRef(new ReadFileTask(frontendProvider, requestId, url, start, end));
     415    }
     416
     417    virtual ~ReadFileTask()
     418    {
     419        reportResult(FileError::ABORT_ERR, 0);
     420    }
     421
     422    void start(ScriptExecutionContext*);
     423
     424
     425    virtual bool operator==(const EventListener& other) OVERRIDE
     426    {
     427        return this == &other;
     428    }
     429
     430    virtual void handleEvent(ScriptExecutionContext*, Event* event) OVERRIDE
     431    {
     432        if (event->type() == eventNames().loadEvent)
     433            didRead();
     434        else if (event->type() == eventNames().errorEvent)
     435            didHitError(m_reader->error().get());
     436    }
     437
     438private:
     439    bool didHitError(FileError* error)
     440    {
     441        reportResult(error->code(), 0);
     442        return true;
     443    }
     444
     445    bool didGetEntry(Entry*);
     446    bool didGetFile(File*);
     447    void didRead();
     448
     449    void reportResult(FileError::ErrorCode errorCode, const String* result)
     450    {
     451        if (!m_frontendProvider || !m_frontendProvider->frontend())
     452            return;
     453        m_frontendProvider->frontend()->fileContentReceived(m_requestId, static_cast<int>(errorCode), result);
     454        m_frontendProvider = 0;
     455    }
     456
     457    ReadFileTask(PassRefPtr<FrontendProvider> frontendProvider, int requestId, const String& url, long long start, long long end)
     458        : EventListener(EventListener::CPPEventListenerType)
     459        , m_frontendProvider(frontendProvider)
     460        , m_requestId(requestId)
     461        , m_url(ParsedURLString, url)
     462        , m_start(start)
     463        , m_end(end)
     464        , m_current(start) { }
     465
     466    RefPtr<FrontendProvider> m_frontendProvider;
     467    int m_requestId;
     468    KURL m_url;
     469    int m_start;
     470    long long m_end;
     471    long long m_current;
     472
     473    RefPtr<FileReader> m_reader;
     474};
     475
     476void ReadFileTask::start(ScriptExecutionContext* scriptExecutionContext)
     477{
     478    ASSERT(scriptExecutionContext);
     479
     480    FileSystemType type;
     481    String path;
     482    if (!DOMFileSystemBase::crackFileSystemURL(m_url, type, path)) {
     483        reportResult(FileError::SYNTAX_ERR, 0);
     484        return;
     485    }
     486
     487    RefPtr<EntryCallback> successCallback = CallbackDispatcherFactory<EntryCallback>::create(this, &ReadFileTask::didGetEntry);
     488    RefPtr<ErrorCallback> errorCallback = CallbackDispatcherFactory<ErrorCallback>::create(this, &ReadFileTask::didHitError);
     489    OwnPtr<ResolveURICallbacks> fileSystemCallbacks = ResolveURICallbacks::create(successCallback, errorCallback, scriptExecutionContext, type, path);
     490
     491    LocalFileSystem::localFileSystem().readFileSystem(scriptExecutionContext, type, fileSystemCallbacks.release());
     492}
     493
     494bool ReadFileTask::didGetEntry(Entry* entry)
     495{
     496    if (entry->isDirectory()) {
     497        reportResult(FileError::TYPE_MISMATCH_ERR, 0);
     498        return true;
     499    }
     500
     501    if (!entry->filesystem()->scriptExecutionContext()) {
     502        reportResult(FileError::ABORT_ERR, 0);
     503        return true;
     504    }
     505
     506    RefPtr<FileCallback> successCallback = CallbackDispatcherFactory<FileCallback>::create(this, &ReadFileTask::didGetFile);
     507    RefPtr<ErrorCallback> errorCallback = CallbackDispatcherFactory<ErrorCallback>::create(this, &ReadFileTask::didHitError);
     508    static_cast<FileEntry*>(entry)->file(successCallback, errorCallback);
     509
     510    m_reader = FileReader::create(entry->filesystem()->scriptExecutionContext());
     511    return true;
     512}
     513
     514bool ReadFileTask::didGetFile(File* file)
     515{
     516    RefPtr<Blob> blob = file->slice(m_start, m_end);
     517    m_reader->setOnload(this);
     518    m_reader->setOnerror(this);
     519
     520    ExceptionCode ec = 0;
     521    m_reader->readAsArrayBuffer(blob.get(), ec);
     522    return true;
     523}
     524
     525void ReadFileTask::didRead()
     526{
     527    RefPtr<ArrayBuffer> result = m_reader->arrayBufferResult();
     528    String encodedResult = base64Encode(static_cast<char*>(result->data()), result->byteLength());
     529    reportResult(static_cast<FileError::ErrorCode>(0), &encodedResult);
     530}
     531
    403532}
    404533
     
    479608}
    480609
     610void InspectorFileSystemAgent::requestFileContent(ErrorString* error, const String& url, const int* start, const int* end, int* requestId)
     611{
     612    if (!m_enabled || !m_frontendProvider) {
     613        *error = "FileSystem agent is not enabled";
     614        return;
     615    }
     616    ASSERT(m_frontendProvider->frontend());
     617
     618    *requestId = m_nextRequestId++;
     619
     620    if (ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextForOrigin(SecurityOrigin::createFromString(url).get()))
     621        ReadFileTask::create(m_frontendProvider, *requestId, url, start ? *start : 0, end ? *end : std::numeric_limits<long long>::max())->start(scriptExecutionContext);
     622    else
     623        m_frontendProvider->frontend()->fileContentReceived(*requestId, static_cast<int>(FileError::ABORT_ERR), 0);
     624}
     625
    481626void InspectorFileSystemAgent::setFrontend(InspectorFrontend* frontend)
    482627{
  • trunk/Source/WebCore/inspector/InspectorFileSystemAgent.h

    r121251 r121676  
    6363    virtual void requestDirectoryContent(ErrorString*, const String& url, int* requestId) OVERRIDE;
    6464    virtual void requestMetadata(ErrorString*, const String& url, int* requestId) OVERRIDE;
     65    virtual void requestFileContent(ErrorString*, const String& url, const int* start, const int* end, int* requestId) OVERRIDE;
    6566
    6667    virtual void setFrontend(InspectorFrontend*) OVERRIDE;
  • trunk/Source/WebCore/inspector/front-end/FileSystemModel.js

    r121542 r121676  
    238238    },
    239239
     240    /**
     241     * @param {WebInspector.FileSystemModel.Entry} entry
     242     * @param {function(number, FileSystemAgent.Metadata=)} callback
     243     */
    240244    requestMetadata: function(entry, callback)
    241245    {
    242246        this._agentWrapper.requestMetadata(entry.url, callback);
     247    },
     248
     249    /**
     250     * @param {WebInspector.FileSystemModel.File} file
     251     * @param {number} start
     252     * @param {number} end
     253     * @param {function(number, string=)} callback
     254     */
     255    requestFileContent: function(file, start, end, callback)
     256    {
     257        this._agentWrapper.requestFileContent(file.url, start, end, callback);
    243258    }
    244259}
     
    408423    {
    409424        return this._resourceType;
     425    },
     426
     427    /**
     428     * @param {number} start
     429     * @param {number} end
     430     * @param {function(number, string=)} callback
     431     */
     432    requestFileContent: function(start, end, callback)
     433    {
     434        this.fileSystemModel.requestFileContent(this, start, end, callback);
    410435    }
    411436}
     
    421446    this._pendingDirectoryContentRequests = {};
    422447    this._pendingMetadataRequests = {};
     448    this._pendingFileContentRequests = {};
    423449
    424450    InspectorBackend.registerFileSystemDispatcher(new WebInspector.FileSystemDispatcher(this));
     
    511537        delete this._pendingMetadataRequests[requestId];
    512538        callback(errorCode, metadata);
     539    },
     540
     541    /**
     542     * @param {string} url
     543     * @param {number} start
     544     * @param {number} end
     545     * @param {function(number, string)} callback
     546     */
     547    requestFileContent: function(url, start, end, callback)
     548    {
     549        var store = this._pendingFileContentRequests;
     550        FileSystemAgent.requestFileContent(url, start, end, requestAccepted);
     551
     552        function requestAccepted(error, requestId)
     553        {
     554            if (!error)
     555                store[requestId] = callback;
     556        }
     557    },
     558
     559    /**
     560     * @param {number} requestId
     561     * @param {number} errorCode
     562     * @param {string=} content
     563     */
     564    _fileContentReceived: function(requestId, errorCode, content)
     565    {
     566        var callback = /** @type {function(number, string=)} */ this._pendingFileContentRequests[requestId];
     567        if (!callback)
     568            return;
     569        delete this._pendingFileContentRequests[requestId];
     570        callback(errorCode, content);
    513571    }
    514572}
     
    553611    {
    554612        this._agentWrapper._metadataReceived(requestId, errorCode, metadata);
     613    },
     614
     615    /**
     616     * @param {number} requestId
     617     * @param {number} errorCode
     618     * @param {string=} content
     619     */
     620    fileContentReceived: function(requestId, errorCode, content)
     621    {
     622        this._agentWrapper._fileContentReceived(requestId, errorCode, content);
    555623    }
    556624}
Note: See TracChangeset for help on using the changeset viewer.