Changes between Version 5 and Version 6 of ThreadCommunication


Ignore:
Timestamp:
Apr 19, 2011 3:43:59 PM (13 years ago)
Author:
levin@chromium.org
Comment:

Added "case study" of converting code to using the new api.

Legend:

Unmodified
Added
Removed
Modified
  • ThreadCommunication

    v5 v6  
    5858'''Case study: File API from Workers'''
    5959
    60 TBD: In progress
     60Here's an example of converting the move method from WebKit/chromium/src/WorkerFileSystemCallbacksBridge.cpp (http://trac.webkit.org/browser/trunk/Source/WebKit/chromium/src/WorkerFileSystemCallbacksBridge.cpp) to using this.
     61
     62The main changes are less code at the calling sites and less mechanics around carefully tracking the lifetime of each side.
     63
     64{{{
     65class MainThreadFileSystemCallbacks : public WebFileSystemCallbacks {
     66public:
     67    // Callbacks are self-destructed and we always return leaked pointer here.
     68    static MainThreadFileSystemCallbacks* createLeakedPtr(PassOwnPtr<ClassProxy> callbackProxy)
     69    {
     70        return adoptPtr(new MainThreadFileSystemCallbacks(callbackProxy)).leakPtr();
     71    }
     72
     73    virtual void didSucceed()
     74    {
     75        m_callbackProxy->invoke(&didSucceedOnMainThread);
     76        m_callbackProxy->done();
     77        delete this;
     78    }
     79
     80private:
     81    MainThreadFileSystemCallbacks(PassOwnPtr<ClassProxy> callbackProxy)
     82        : m_callbackProxy(callbackProxy)
     83    {
     84    }
     85
     86    OwnPtr<ClassProxy> m_callbackProxy;
     87};
     88
     89void moveOnMainThread(PassOwnPtr<ClassProxy> callbackProxy, WebFileSystem* fileSystem, const String& sourcePath, const String& destinationPath, const String& mode)
     90{
     91    fileSystem->move(sourcePath, destinationPath, MainThreadFileSystemCallbacks::createLeakedPtr(callbackProxy));
     92}
     93
     94void WorkerFileSystemCallbacks::stop()
     95{
     96    if (m_callbacksOnWorkerThread) {
     97        m_callbacksOnWorkerThread->didFail(WebFileErrorAbort);
     98        m_callbacksOnWorkerThread = 0;
     99    }
     100}
     101
     102void WorkerFileSystemCallbacks::postMoveToMainThread(WebFileSystem* fileSystem, const String& sourcePath, const String& destinationPath, const String& mode)
     103{
     104    OwnPtr<SyncWaiter> syncCall = mainThreadMessageLoop->invoke(&moveOnMainThread, getProxy(), fileSystem, sourcePath, destinationPath);
     105    syncCall.wait();
     106    stop();
     107}
     108}}}