/*
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "WebContext.h"

#include "ArgumentCoders.h"
#include "HandleMessage.h"
#include "MessageDecoder.h"
#if PLATFORM(MAC) || PLATFORM(IOS)
#include "SharedMemory.h"
#endif
#include "StatisticsData.h"
#include "WebContextMessages.h"
#if PLATFORM(MAC) || PLATFORM(IOS)
#include "WebCoreArgumentCoders.h"
#endif
#if PLATFORM(MAC)
#include <WebCore/Color.h>
#endif
#if PLATFORM(IOS)
#include <WebCore/Pasteboard.h>
#endif
#if PLATFORM(MAC)
#include <wtf/Vector.h>
#endif
#include <wtf/text/WTFString.h>

namespace WebKit {

void WebContext::didReceiveWebContextMessage(IPC::Connection*, IPC::MessageDecoder& decoder)
{
    if (decoder.messageName() == Messages::WebContext::AddVisitedLinkHash::name()) {
        IPC::handleMessage<Messages::WebContext::AddVisitedLinkHash>(decoder, this, &WebContext::addVisitedLinkHash);
        return;
    }
    if (decoder.messageName() == Messages::WebContext::DidGetStatistics::name()) {
        IPC::handleMessage<Messages::WebContext::DidGetStatistics>(decoder, this, &WebContext::didGetStatistics);
        return;
    }
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::WriteWebContentToPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::WriteWebContentToPasteboard>(decoder, this, &WebContext::writeWebContentToPasteboard);
        return;
    }
#endif
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::WriteImageToPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::WriteImageToPasteboard>(decoder, this, &WebContext::writeImageToPasteboard);
        return;
    }
#endif
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::WriteStringToPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::WriteStringToPasteboard>(decoder, this, &WebContext::writeStringToPasteboard);
        return;
    }
#endif
    if (decoder.messageName() == Messages::WebContext::AddPlugInAutoStartOriginHash::name()) {
        IPC::handleMessage<Messages::WebContext::AddPlugInAutoStartOriginHash>(decoder, this, &WebContext::addPlugInAutoStartOriginHash);
        return;
    }
    if (decoder.messageName() == Messages::WebContext::PlugInDidReceiveUserInteraction::name()) {
        IPC::handleMessage<Messages::WebContext::PlugInDidReceiveUserInteraction>(decoder, this, &WebContext::plugInDidReceiveUserInteraction);
        return;
    }
    ASSERT_NOT_REACHED();
}

void WebContext::didReceiveSyncWebContextMessage(IPC::Connection*, IPC::MessageDecoder& decoder, std::unique_ptr<IPC::MessageEncoder>& replyEncoder)
{
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::ReadStringFromPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::ReadStringFromPasteboard>(decoder, *replyEncoder, this, &WebContext::readStringFromPasteboard);
        return;
    }
#endif
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::ReadURLFromPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::ReadURLFromPasteboard>(decoder, *replyEncoder, this, &WebContext::readURLFromPasteboard);
        return;
    }
#endif
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::ReadBufferFromPasteboard::name()) {
        IPC::handleMessage<Messages::WebContext::ReadBufferFromPasteboard>(decoder, *replyEncoder, this, &WebContext::readBufferFromPasteboard);
        return;
    }
#endif
#if PLATFORM(IOS)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardItemsCount::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardItemsCount>(decoder, *replyEncoder, this, &WebContext::getPasteboardItemsCount);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardTypes::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardTypes>(decoder, *replyEncoder, this, &WebContext::getPasteboardTypes);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardPathnamesForType::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardPathnamesForType>(decoder, *replyEncoder, this, &WebContext::getPasteboardPathnamesForType);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardStringForType::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardStringForType>(decoder, *replyEncoder, this, &WebContext::getPasteboardStringForType);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardBufferForType::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardBufferForType>(decoder, *replyEncoder, this, &WebContext::getPasteboardBufferForType);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::PasteboardCopy::name()) {
        IPC::handleMessage<Messages::WebContext::PasteboardCopy>(decoder, *replyEncoder, this, &WebContext::pasteboardCopy);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardChangeCount::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardChangeCount>(decoder, *replyEncoder, this, &WebContext::getPasteboardChangeCount);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardUniqueName::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardUniqueName>(decoder, *replyEncoder, this, &WebContext::getPasteboardUniqueName);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardColor::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardColor>(decoder, *replyEncoder, this, &WebContext::getPasteboardColor);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::GetPasteboardURL::name()) {
        IPC::handleMessage<Messages::WebContext::GetPasteboardURL>(decoder, *replyEncoder, this, &WebContext::getPasteboardURL);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::AddPasteboardTypes::name()) {
        IPC::handleMessage<Messages::WebContext::AddPasteboardTypes>(decoder, *replyEncoder, this, &WebContext::addPasteboardTypes);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::SetPasteboardTypes::name()) {
        IPC::handleMessage<Messages::WebContext::SetPasteboardTypes>(decoder, *replyEncoder, this, &WebContext::setPasteboardTypes);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::SetPasteboardPathnamesForType::name()) {
        IPC::handleMessage<Messages::WebContext::SetPasteboardPathnamesForType>(decoder, *replyEncoder, this, &WebContext::setPasteboardPathnamesForType);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::SetPasteboardStringForType::name()) {
        IPC::handleMessage<Messages::WebContext::SetPasteboardStringForType>(decoder, *replyEncoder, this, &WebContext::setPasteboardStringForType);
        return;
    }
#endif
#if PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::SetPasteboardBufferForType::name()) {
        IPC::handleMessage<Messages::WebContext::SetPasteboardBufferForType>(decoder, *replyEncoder, this, &WebContext::setPasteboardBufferForType);
        return;
    }
#endif
#if !PLATFORM(MAC)
    if (decoder.messageName() == Messages::WebContext::Dummy::name()) {
        IPC::handleMessage<Messages::WebContext::Dummy>(decoder, *replyEncoder, this, &WebContext::dummy);
        return;
    }
#endif
    ASSERT_NOT_REACHED();
}

} // namespace WebKit
