Skip to content

Commit

Permalink
Add postTaskToMainThread to ScriptExecutionContext.
Browse files Browse the repository at this point in the history
Move the code to post task to the main thread into a new method on ScriptExecutionContext,
to use as a helper implementation of the virtual ScriptExecutionContext::postTask(Task) in
contexts that live on the main thread.
https://bugs.webkit.org/show_bug.cgi?id=31427

Reviewed by Alexey Proskuryakov.

No new tests - simply moving the code.

* dom/Document.cpp:
(WebCore::Document::postTask):
* dom/ScriptExecutionContext.cpp:
(WebCore::ScriptExecutionContextTaskTimer::ScriptExecutionContextTaskTimer):
(WebCore::ScriptExecutionContextTaskTimer::fired):
(WebCore::PerformTaskData::PerformTaskData):
(WebCore::PerformTaskData::performTask):
(WebCore::ScriptExecutionContext::postTaskToMainThread):
* dom/ScriptExecutionContext.h:

Canonical link: https://commits.webkit.org/42348@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@50919 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Dmitry Titov committed Nov 13, 2009
1 parent 7bf8863 commit c4c60b4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 43 deletions.
22 changes: 22 additions & 0 deletions WebCore/ChangeLog
@@ -1,3 +1,25 @@
2009-11-12 Dmitry Titov <dimich@chromium.org>

Reviewed by Alexey Proskuryakov.

Add postTaskToMainThread to ScriptExecutionContext.
Move the code to post task to the main thread into a new method on ScriptExecutionContext,
to use as a helper implementation of the virtual ScriptExecutionContext::postTask(Task) in
contexts that live on the main thread.
https://bugs.webkit.org/show_bug.cgi?id=31427

No new tests - simply moving the code.

* dom/Document.cpp:
(WebCore::Document::postTask):
* dom/ScriptExecutionContext.cpp:
(WebCore::ScriptExecutionContextTaskTimer::ScriptExecutionContextTaskTimer):
(WebCore::ScriptExecutionContextTaskTimer::fired):
(WebCore::PerformTaskData::PerformTaskData):
(WebCore::PerformTaskData::performTask):
(WebCore::ScriptExecutionContext::postTaskToMainThread):
* dom/ScriptExecutionContext.h:

2009-11-12 Simon Fraser <simon.fraser@apple.com>

Reviewed by Darin Adler.
Expand Down
44 changes: 1 addition & 43 deletions WebCore/dom/Document.cpp
Expand Up @@ -4536,51 +4536,9 @@ void Document::scriptImported(unsigned long identifier, const String& sourceStri
#endif
}

class ScriptExecutionContextTaskTimer : public TimerBase {
public:
ScriptExecutionContextTaskTimer(PassRefPtr<Document> context, PassOwnPtr<ScriptExecutionContext::Task> task)
: m_context(context)
, m_task(task)
{
}

private:
virtual void fired()
{
m_task->performTask(m_context.get());
delete this;
}

RefPtr<Document> m_context;
OwnPtr<ScriptExecutionContext::Task> m_task;
};

struct PerformTaskContext : Noncopyable {
PerformTaskContext(ScriptExecutionContext* scriptExecutionContext, PassOwnPtr<ScriptExecutionContext::Task> task)
: scriptExecutionContext(scriptExecutionContext)
, task(task)
{
}

ScriptExecutionContext* scriptExecutionContext; // The context should exist until task execution.
OwnPtr<ScriptExecutionContext::Task> task;
};

static void performTask(void* ctx)
{
PerformTaskContext* ptctx = reinterpret_cast<PerformTaskContext*>(ctx);
ptctx->task->performTask(ptctx->scriptExecutionContext);
delete ptctx;
}

void Document::postTask(PassOwnPtr<Task> task)
{
if (isMainThread()) {
ScriptExecutionContextTaskTimer* timer = new ScriptExecutionContextTaskTimer(static_cast<Document*>(this), task);
timer->startOneShot(0);
} else {
callOnMainThread(performTask, new PerformTaskContext(this, task));
}
postTaskToMainThread(task);
}

Element* Document::findAnchor(const String& name)
Expand Down
49 changes: 49 additions & 0 deletions WebCore/dom/ScriptExecutionContext.cpp
Expand Up @@ -215,4 +215,53 @@ JSC::JSGlobalData* ScriptExecutionContext::globalData()
}
#endif

class ScriptExecutionContextTaskTimer : public TimerBase {
public:
ScriptExecutionContextTaskTimer(PassRefPtr<ScriptExecutionContext> context, PassOwnPtr<ScriptExecutionContext::Task> task)
: m_context(context)
, m_task(task)
{
}

private:
virtual void fired()
{
m_task->performTask(m_context.get());
delete this;
}

RefPtr<ScriptExecutionContext> m_context;
OwnPtr<ScriptExecutionContext::Task> m_task;
};

class PerformTaskData {
public:
PerformTaskData(PassRefPtr<ScriptExecutionContext> context, PassOwnPtr<ScriptExecutionContext::Task> task)
: m_context(context)
, m_task(task)
{
}

static void performTask(void* data)
{
PerformTaskData* taskData = static_cast<PerformTaskData*>(data);
taskData->m_task->performTask(taskData->m_context.get());
delete taskData;
}

private:
RefPtr<ScriptExecutionContext> m_context;
OwnPtr<ScriptExecutionContext::Task> m_task;
};

void ScriptExecutionContext::postTaskToMainThread(PassOwnPtr<Task> task)
{
if (isMainThread()) {
ScriptExecutionContextTaskTimer* timer = new ScriptExecutionContextTaskTimer(this, task);
timer->startOneShot(0);
} else {
callOnMainThread(PerformTaskData::performTask, new PerformTaskData(this, task));
}
}

} // namespace WebCore
3 changes: 3 additions & 0 deletions WebCore/dom/ScriptExecutionContext.h
Expand Up @@ -114,6 +114,9 @@ namespace WebCore {
// that already contains content.
void setSecurityOrigin(PassRefPtr<SecurityOrigin>);

// Helper for contexts that live on the main thread.
void postTaskToMainThread(PassOwnPtr<Task>);

private:
virtual const KURL& virtualURL() const = 0;
virtual KURL virtualCompleteURL(const String&) const = 0;
Expand Down

0 comments on commit c4c60b4

Please sign in to comment.