Changeset 69532 in webkit


Ignore:
Timestamp:
Oct 11, 2010 3:15:21 PM (14 years ago)
Author:
Adam Roben
Message:

Implement SharedMemory on Windows

This makes visited links be colored correctly.

Fixes <http://webkit.org/b/47499> <rdar://problem/8422725>.

Reviewed by Anders Carlsson.

  • Platform/SharedMemory.h: Add Windows-specific members to

SharedMemory and SharedMemory::Handle.

  • Platform/win/SharedMemoryWin.cpp:

(WebKit::SharedMemory::Handle::Handle): Initialize our members.
(WebKit::SharedMemory::Handle::~Handle): Close our HANDLE if we have
one.
(WebKit::SharedMemory::Handle::encode): Encode our size, HANDLE, and
PID. Null out our HANDLE member, as it is now the receiving process's
responsibility to close the HANDLE.
(WebKit::SharedMemory::Handle::decode): Copy the handle from the
sending process into the receiving process and close the HANDLE the
sending process gave us.
(WebKit::SharedMemory::create): Map some memory and store it in a new
SharedMemory object.
(WebKit::accessRights): Helper function to convert a
SharedMemory::Protection to a file-mapping access right.
(WebKit::SharedMemory::create): Map the memory represented by the
Handle, and adopt the HANDLE from it.
(WebKit::SharedMemory::~SharedMemory): Clean up our memory mapping.
(WebKit::SharedMemory::createHandle): Give the Handle a copy of our
HANDLE with the specified protection.

Location:
trunk/WebKit2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKit2/ChangeLog

    r69531 r69532  
     12010-10-11  Adam Roben  <aroben@apple.com>
     2
     3        Implement SharedMemory on Windows
     4
     5        This makes visited links be colored correctly.
     6
     7        Fixes <http://webkit.org/b/47499> <rdar://problem/8422725>.
     8
     9        Reviewed by Anders Carlsson.
     10
     11        * Platform/SharedMemory.h: Add Windows-specific members to
     12        SharedMemory and SharedMemory::Handle.
     13
     14        * Platform/win/SharedMemoryWin.cpp:
     15        (WebKit::SharedMemory::Handle::Handle): Initialize our members.
     16        (WebKit::SharedMemory::Handle::~Handle): Close our HANDLE if we have
     17        one.
     18        (WebKit::SharedMemory::Handle::encode): Encode our size, HANDLE, and
     19        PID. Null out our HANDLE member, as it is now the receiving process's
     20        responsibility to close the HANDLE.
     21        (WebKit::SharedMemory::Handle::decode): Copy the handle from the
     22        sending process into the receiving process and close the HANDLE the
     23        sending process gave us.
     24        (WebKit::SharedMemory::create): Map some memory and store it in a new
     25        SharedMemory object.
     26        (WebKit::accessRights): Helper function to convert a
     27        SharedMemory::Protection to a file-mapping access right.
     28        (WebKit::SharedMemory::create): Map the memory represented by the
     29        Handle, and adopt the HANDLE from it.
     30        (WebKit::SharedMemory::~SharedMemory): Clean up our memory mapping.
     31        (WebKit::SharedMemory::createHandle): Give the Handle a copy of our
     32        HANDLE with the specified protection.
     33
    1342010-10-11  Adam Roben  <aroben@apple.com>
    235
  • trunk/WebKit2/Platform/SharedMemory.h

    r68353 r69532  
    5959#if PLATFORM(MAC)
    6060        mutable mach_port_t m_port;
     61#elif PLATFORM(WIN)
     62        mutable HANDLE m_handle;
    6163#endif
    6264        size_t m_size;
     
    8284    size_t m_size;
    8385    void* m_data;
     86#if PLATFORM(WIN)
     87    HANDLE m_handle;
     88#endif
    8489};
    8590
  • trunk/WebKit2/Platform/win/SharedMemoryWin.cpp

    r65484 r69532  
    2626#include "SharedMemory.h"
    2727
    28 #define DISABLE_NOT_IMPLEMENTED_WARNINGS 1
    29 #include "NotImplemented.h"
     28#include "ArgumentDecoder.h"
     29#include "ArgumentEncoder.h"
     30#include <wtf/RefPtr.h>
    3031
    3132namespace WebKit {
    3233
    3334SharedMemory::Handle::Handle()
     35    : m_handle(0)
     36    , m_size(0)
    3437{
    35     notImplemented();
    3638}
    3739
    3840SharedMemory::Handle::~Handle()
    3941{
    40     notImplemented();
     42    if (!m_handle)
     43        return;
     44
     45    ::CloseHandle(m_handle);
    4146}
    4247
    4348void SharedMemory::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
    4449{
    45     notImplemented();
     50    encoder->encodeUInt64(m_size);
     51
     52    // Hand off ownership of our HANDLE to the receiving process. It will close it for us.
     53    // FIXME: If the receiving process crashes before it receives the memory, the memory will be
     54    // leaked. See <http://webkit.org/b/47502>.
     55    encoder->encodeUInt64(reinterpret_cast<uint64_t>(m_handle));
     56    m_handle = 0;
     57
     58    // Send along our PID so that the receiving process can duplicate the HANDLE for its own use.
     59    encoder->encodeUInt32(::GetCurrentProcessId());
    4660}
    4761
    4862bool SharedMemory::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
    4963{
    50     notImplemented();
    51     return false;
     64    ASSERT_ARG(handle, !handle.m_handle);
     65    ASSERT_ARG(handle, !handle.m_size);
     66
     67    uint64_t size;
     68    if (!decoder->decodeUInt64(size))
     69        return false;
     70
     71    uint64_t sourceHandle;
     72    if (!decoder->decodeUInt64(sourceHandle))
     73        return false;
     74
     75    uint32_t sourcePID;
     76    if (!decoder->decodeUInt32(sourcePID))
     77        return false;
     78
     79    HANDLE sourceProcess = ::OpenProcess(PROCESS_DUP_HANDLE, FALSE, sourcePID);
     80    if (!sourceProcess)
     81        return false;
     82
     83    // Copy the handle into our process and close the handle that the sending process created for us.
     84    HANDLE duplicatedHandle;
     85    BOOL success = ::DuplicateHandle(sourceProcess, reinterpret_cast<HANDLE>(sourceHandle), ::GetCurrentProcess(), &duplicatedHandle, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
     86
     87    ::CloseHandle(sourceProcess);
     88
     89    if (!success)
     90        return false;
     91
     92    handle.m_handle = duplicatedHandle;
     93    handle.m_size = size;
     94    return true;
    5295}
    5396
    5497PassRefPtr<SharedMemory> SharedMemory::create(size_t size)
    5598{
    56     notImplemented();
     99    HANDLE handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, 0);
     100    if (!handle)
     101        return 0;
     102
     103    void* baseAddress = ::MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
     104    if (!baseAddress) {
     105        ::CloseHandle(handle);
     106        return 0;
     107    }
     108
     109    RefPtr<SharedMemory> memory = adoptRef(new SharedMemory);
     110    memory->m_size = size;
     111    memory->m_data = baseAddress;
     112    memory->m_handle = handle;
     113
     114    return memory.release();
     115}
     116
     117static DWORD accessRights(SharedMemory::Protection protection)
     118{
     119    switch (protection) {
     120    case SharedMemory::ReadOnly:
     121        return FILE_MAP_READ;
     122    case SharedMemory::ReadWrite:
     123        // FILE_MAP_WRITE implies read access, too.
     124        return FILE_MAP_WRITE;
     125    }
     126
     127    ASSERT_NOT_REACHED();
    57128    return 0;
    58129}
     
    60131PassRefPtr<SharedMemory> SharedMemory::create(const Handle& handle, Protection protection)
    61132{
    62     notImplemented();
    63     return 0;   
     133    DWORD desiredAccess = accessRights(protection);
     134
     135    void* baseAddress = ::MapViewOfFile(handle.m_handle, desiredAccess, 0, 0, handle.m_size);
     136    if (!baseAddress)
     137        return 0;
     138
     139    RefPtr<SharedMemory> memory = adoptRef(new SharedMemory);
     140    memory->m_size = handle.m_size;
     141    memory->m_data = baseAddress;
     142
     143    // Adopt the HANDLE.
     144    memory->m_handle = handle.m_handle;
     145    handle.m_handle = 0;
     146
     147    return memory.release();
    64148}
    65149
    66150SharedMemory::~SharedMemory()
    67151{
    68     notImplemented();
     152    ASSERT(m_data);
     153    ASSERT(m_handle);
     154
     155    ::UnmapViewOfFile(m_data);
     156    ::CloseHandle(m_handle);
    69157}
    70158   
    71159bool SharedMemory::createHandle(Handle& handle, Protection protection)
    72160{
    73     notImplemented();
    74     return false;
     161    ASSERT_ARG(handle, !handle.m_handle);
     162    ASSERT_ARG(handle, !handle.m_size);
     163
     164    HANDLE processHandle = ::GetCurrentProcess();
     165
     166    HANDLE duplicatedHandle;
     167    if (!::DuplicateHandle(processHandle, m_handle, processHandle, &duplicatedHandle, accessRights(protection), FALSE, 0))
     168        return false;
     169
     170    handle.m_handle = duplicatedHandle;
     171    handle.m_size = m_size;
     172    return true;
    75173}
    76174
Note: See TracChangeset for help on using the changeset viewer.