#include "config.h"
#include "WebWorkerBase.h"
#include "CrossThreadTask.h"
#include "DatabaseTask.h"
#include "Document.h"
#include "MessagePortChannel.h"
#include "PlatformMessagePortChannel.h"
#include "SecurityOrigin.h"
#include "WebDataSourceImpl.h"
#include "WebFileError.h"
#include "WebFrameClient.h"
#include "WebFrameImpl.h"
#include "WebMessagePortChannel.h"
#include "WebRuntimeFeatures.h"
#include "WebSettings.h"
#include "WebView.h"
#include "WebWorkerClient.h"
#include "WorkerContext.h"
#include "WorkerFileSystemCallbacksBridge.h"
#include "WorkerScriptController.h"
#include "WorkerThread.h"
#include <wtf/MainThread.h>
using namespace WebCore;
namespace WebKit {
#if ENABLE(WORKERS)
static const char allowDatabaseMode[] = "allowDatabaseMode";
static const char allowFileSystemMode[] = "allowFileSystemMode";
static const char openFileSystemMode[] = "openFileSystemMode";
namespace {
class AllowDatabaseMainThreadBridge : public ThreadSafeRefCounted<AllowDatabaseMainThreadBridge> {
public:
static PassRefPtr<AllowDatabaseMainThreadBridge> create(WebWorkerBase* worker, const WTF::String& mode, WebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& name, const WTF::String& displayName, unsigned long estimatedSize)
{
return adoptRef(new AllowDatabaseMainThreadBridge(worker, mode, commonClient, frame, name, displayName, estimatedSize));
}
void cancel()
{
MutexLocker locker(m_mutex);
m_worker = 0;
}
bool result()
{
return m_result;
}
void signalCompleted(bool result)
{
MutexLocker locker(m_mutex);
if (m_worker)
m_worker->postTaskForModeToWorkerContext(
createCallbackTask(&didComplete, AllowCrossThreadAccess(this), result), m_mode);
}
private:
AllowDatabaseMainThreadBridge(WebWorkerBase* worker, const WTF::String& mode, WebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& name, const WTF::String& displayName, unsigned long estimatedSize)
: m_worker(worker)
, m_mode(mode)
{
worker->dispatchTaskToMainThread(
createCallbackTask(&allowDatabaseTask, AllowCrossThreadAccess(commonClient),
AllowCrossThreadAccess(frame),
String(name), String(displayName), estimatedSize,
AllowCrossThreadAccess(this)));
}
static void allowDatabaseTask(WebCore::ScriptExecutionContext* context, WebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String name, const WTF::String displayName, unsigned long estimatedSize, PassRefPtr<AllowDatabaseMainThreadBridge> bridge)
{
if (!commonClient)
bridge->signalCompleted(false);
else
bridge->signalCompleted(commonClient->allowDatabase(frame, name, displayName, estimatedSize));
}
static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowDatabaseMainThreadBridge> bridge, bool result)
{
bridge->m_result = result;
}
bool m_result;
Mutex m_mutex;
WebWorkerBase* m_worker;
WTF::String m_mode;
};
class AllowFileSystemMainThreadBridge : public ThreadSafeRefCounted<AllowFileSystemMainThreadBridge> {
public:
static PassRefPtr<AllowFileSystemMainThreadBridge> create(WebWorkerBase* worker, const WTF::String& mode, WebCommonWorkerClient* commonClient)
{
return adoptRef(new AllowFileSystemMainThreadBridge(worker, mode, commonClient));
}
void cancel()
{
MutexLocker locker(m_mutex);
m_worker = 0;
}
bool result()
{
return m_result;
}
void signalCompleted(bool result)
{
MutexLocker locker(m_mutex);
if (m_worker)
m_worker->postTaskForModeToWorkerContext(
createCallbackTask(&didComplete, AllowCrossThreadAccess(this), result), m_mode);
}
private:
AllowFileSystemMainThreadBridge(WebWorkerBase* worker, const WTF::String& mode, WebCommonWorkerClient* commonClient)
: m_worker(worker)
, m_mode(mode)
{
worker->dispatchTaskToMainThread(
createCallbackTask(&allowFileSystemTask, AllowCrossThreadAccess(commonClient),
AllowCrossThreadAccess(this)));
}
static void allowFileSystemTask(WebCore::ScriptExecutionContext* context, WebCommonWorkerClient* commonClient, PassRefPtr<AllowFileSystemMainThreadBridge> bridge)
{
if (!commonClient)
bridge->signalCompleted(false);
else
bridge->signalCompleted(commonClient->allowFileSystem());
}
static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowFileSystemMainThreadBridge> bridge, bool result)
{
bridge->m_result = result;
}
bool m_result;
Mutex m_mutex;
WebWorkerBase* m_worker;
WTF::String m_mode;
};
}
static void initializeWebKitStaticValues()
{
static bool initialized = false;
if (!initialized) {
initialized = true;
RefPtr<SecurityOrigin> origin =
SecurityOrigin::create(KURL(ParsedURLString, "http://localhost"));
origin.release();
}
}
WebWorkerBase::WebWorkerBase()
: m_webView(0)
, m_askedToTerminate(false)
{
initializeWebKitStaticValues();
}
WebWorkerBase::~WebWorkerBase()
{
ASSERT(m_webView);
WebFrameImpl* webFrame = static_cast<WebFrameImpl*>(m_webView->mainFrame());
if (webFrame)
webFrame->setClient(0);
m_webView->close();
}
void WebWorkerBase::stopWorkerThread()
{
if (m_askedToTerminate)
return;
m_askedToTerminate = true;
if (m_workerThread)
m_workerThread->stop();
}
void WebWorkerBase::initializeLoader(const WebURL& url)
{
ASSERT(!m_webView);
m_webView = WebView::create(0);
m_webView->settings()->setOfflineWebApplicationCacheEnabled(WebRuntimeFeatures::isApplicationCacheEnabled());
m_webView->initializeMainFrame(this);
WebFrameImpl* webFrame = static_cast<WebFrameImpl*>(m_webView->mainFrame());
CString content("");
int len = static_cast<int>(content.length());
RefPtr<SharedBuffer> buf(SharedBuffer::create(content.data(), len));
SubstituteData substData(buf, String("text/html"), String("UTF-8"), KURL());
webFrame->frame()->loader()->load(ResourceRequest(url), substData, false);
m_loadingDocument = webFrame->frame()->document();
}
void WebWorkerBase::dispatchTaskToMainThread(PassOwnPtr<ScriptExecutionContext::Task> task)
{
callOnMainThread(invokeTaskMethod, task.leakPtr());
}
void WebWorkerBase::invokeTaskMethod(void* param)
{
ScriptExecutionContext::Task* task =
static_cast<ScriptExecutionContext::Task*>(param);
task->performTask(0);
delete task;
}
void WebWorkerBase::didCreateDataSource(WebFrame*, WebDataSource* ds)
{
static_cast<WebDataSourceImpl*>(ds)->setDeferMainResourceDataLoad(false);
}
WebApplicationCacheHost* WebWorkerBase::createApplicationCacheHost(WebFrame*, WebApplicationCacheHostClient* appcacheHostClient)
{
if (commonClient())
return commonClient()->createApplicationCacheHost(appcacheHostClient);
return 0;
}
bool WebWorkerBase::allowDatabase(WebFrame*, const WebString& name, const WebString& displayName, unsigned long estimatedSize)
{
WorkerRunLoop& runLoop = m_workerThread->runLoop();
WorkerScriptController* controller = WorkerScriptController::controllerForContext();
WorkerContext* workerContext = controller->workerContext();
String mode = allowDatabaseMode;
mode.append(String::number(runLoop.createUniqueId()));
RefPtr<AllowDatabaseMainThreadBridge> bridge = AllowDatabaseMainThreadBridge::create(this, mode, commonClient(), m_webView->mainFrame(), String(name), String(displayName), estimatedSize);
if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
bridge->cancel();
return false;
}
return bridge->result();
}
#if ENABLE(FILE_SYSTEM)
bool WebWorkerBase::allowFileSystem()
{
WorkerRunLoop& runLoop = m_workerThread->runLoop();
WorkerScriptController* controller = WorkerScriptController::controllerForContext();
WorkerContext* workerContext = controller->workerContext();
String mode = allowFileSystemMode;
mode.append(String::number(runLoop.createUniqueId()));
RefPtr<AllowFileSystemMainThreadBridge> bridge = AllowFileSystemMainThreadBridge::create(this, mode, commonClient());
if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
bridge->cancel();
return false;
}
return bridge->result();
}
void WebWorkerBase::openFileSystemForWorker(WebFileSystem::Type type, long long size, bool create, WebFileSystemCallbacks* callbacks, bool synchronous)
{
WorkerRunLoop& runLoop = m_workerThread->runLoop();
WorkerScriptController* controller = WorkerScriptController::controllerForContext();
WorkerContext* workerContext = controller->workerContext();
String mode = openFileSystemMode;
mode.append(String::number(runLoop.createUniqueId()));
RefPtr<WorkerFileSystemCallbacksBridge> bridge = WorkerFileSystemCallbacksBridge::create(this, workerContext, callbacks);
bridge->postOpenFileSystemToMainThread(commonClient(), type, size, create, mode);
if (synchronous) {
if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated)
bridge->stop();
}
}
#endif
void WebWorkerBase::postMessageToWorkerObject(PassRefPtr<SerializedScriptValue> message,
PassOwnPtr<MessagePortChannelArray> channels)
{
dispatchTaskToMainThread(createCallbackTask(&postMessageTask, AllowCrossThreadAccess(this),
message->toWireString(), channels));
}
void WebWorkerBase::postMessageTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr,
String message,
PassOwnPtr<MessagePortChannelArray> channels)
{
if (!thisPtr->client())
return;
WebMessagePortChannelArray webChannels(channels.get() ? channels->size() : 0);
for (size_t i = 0; i < webChannels.size(); ++i) {
webChannels[i] = (*channels)[i]->channel()->webChannelRelease();
webChannels[i]->setClient(0);
}
thisPtr->client()->postMessageToWorkerObject(message, webChannels);
}
void WebWorkerBase::postExceptionToWorkerObject(const String& errorMessage,
int lineNumber,
const String& sourceURL)
{
dispatchTaskToMainThread(
createCallbackTask(&postExceptionTask, AllowCrossThreadAccess(this),
errorMessage, lineNumber,
sourceURL));
}
void WebWorkerBase::postExceptionTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr,
const String& errorMessage,
int lineNumber, const String& sourceURL)
{
if (!thisPtr->commonClient())
return;
thisPtr->commonClient()->postExceptionToWorkerObject(errorMessage,
lineNumber,
sourceURL);
}
void WebWorkerBase::postConsoleMessageToWorkerObject(MessageSource source,
MessageType type,
MessageLevel level,
const String& message,
int lineNumber,
const String& sourceURL)
{
dispatchTaskToMainThread(createCallbackTask(&postConsoleMessageTask, AllowCrossThreadAccess(this),
source, type, level,
message, lineNumber, sourceURL));
}
void WebWorkerBase::postConsoleMessageTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr,
int source,
int type, int level,
const String& message,
int lineNumber,
const String& sourceURL)
{
if (!thisPtr->commonClient())
return;
thisPtr->commonClient()->postConsoleMessageToWorkerObject(source,
type, level, message,
lineNumber, sourceURL);
}
void WebWorkerBase::confirmMessageFromWorkerObject(bool hasPendingActivity)
{
dispatchTaskToMainThread(createCallbackTask(&confirmMessageTask, AllowCrossThreadAccess(this),
hasPendingActivity));
}
void WebWorkerBase::confirmMessageTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr,
bool hasPendingActivity)
{
if (!thisPtr->client())
return;
thisPtr->client()->confirmMessageFromWorkerObject(hasPendingActivity);
}
void WebWorkerBase::reportPendingActivity(bool hasPendingActivity)
{
dispatchTaskToMainThread(createCallbackTask(&reportPendingActivityTask,
AllowCrossThreadAccess(this),
hasPendingActivity));
}
void WebWorkerBase::reportPendingActivityTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr,
bool hasPendingActivity)
{
if (!thisPtr->client())
return;
thisPtr->client()->reportPendingActivity(hasPendingActivity);
}
void WebWorkerBase::workerContextClosed()
{
dispatchTaskToMainThread(createCallbackTask(&workerContextClosedTask,
AllowCrossThreadAccess(this)));
}
void WebWorkerBase::workerContextClosedTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr)
{
if (thisPtr->commonClient())
thisPtr->commonClient()->workerContextClosed();
thisPtr->stopWorkerThread();
}
void WebWorkerBase::workerContextDestroyed()
{
dispatchTaskToMainThread(createCallbackTask(&workerContextDestroyedTask,
AllowCrossThreadAccess(this)));
}
void WebWorkerBase::workerContextDestroyedTask(ScriptExecutionContext* context,
WebWorkerBase* thisPtr)
{
if (thisPtr->commonClient())
thisPtr->commonClient()->workerContextDestroyed();
delete thisPtr;
}
void WebWorkerBase::postTaskToLoader(PassOwnPtr<ScriptExecutionContext::Task> task)
{
ASSERT(m_loadingDocument->isDocument());
m_loadingDocument->postTask(task);
}
void WebWorkerBase::postTaskForModeToWorkerContext(
PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode)
{
m_workerThread->runLoop().postTaskForMode(task, mode);
}
#endif // ENABLE(WORKERS)
}