1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "web/SuspendableScriptExecutor.h"
8 #include "bindings/core/v8/ScriptController.h"
9 #include "bindings/core/v8/ScriptSourceCode.h"
10 #include "core/dom/Document.h"
11 #include "core/frame/LocalFrame.h"
12 #include "platform/UserGestureIndicator.h"
13 #include "public/platform/WebVector.h"
14 #include "public/web/WebScriptExecutionCallback.h"
18 void SuspendableScriptExecutor::createAndRun(LocalFrame
* frame
, int worldID
, const WillBeHeapVector
<ScriptSourceCode
>& sources
, int extensionGroup
, bool userGesture
, WebScriptExecutionCallback
* callback
)
20 SuspendableScriptExecutor
* executor
= new SuspendableScriptExecutor(frame
, worldID
, sources
, extensionGroup
, userGesture
, callback
);
24 void SuspendableScriptExecutor::contextDestroyed()
26 SuspendableTimer::contextDestroyed();
27 m_callback
->completed(Vector
<v8::Local
<v8::Value
>>());
31 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame
* frame
, int worldID
, const WillBeHeapVector
<ScriptSourceCode
>& sources
, int extensionGroup
, bool userGesture
, WebScriptExecutionCallback
* callback
)
32 : SuspendableTimer(frame
->document())
35 , m_callback(callback
)
38 , m_extensionGroup(extensionGroup
)
39 , m_userGesture(userGesture
)
43 SuspendableScriptExecutor::~SuspendableScriptExecutor()
47 void SuspendableScriptExecutor::fired()
49 executeAndDestroySelf();
52 void SuspendableScriptExecutor::run()
54 ExecutionContext
* context
= executionContext();
56 if (!context
->activeDOMObjectsAreSuspended()) {
58 executeAndDestroySelf();
61 startOneShot(0, FROM_HERE
);
65 void SuspendableScriptExecutor::executeAndDestroySelf()
67 // after calling the destructor of object - object will be unsubscribed from
68 // resumed and contextDestroyed LifecycleObserver methods
69 OwnPtr
<UserGestureIndicator
> indicator
;
71 indicator
= adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUserGesture
));
73 v8::HandleScope
scope(v8::Isolate::GetCurrent());
74 Vector
<v8::Local
<v8::Value
>> results
;
76 m_frame
->script().executeScriptInIsolatedWorld(m_worldID
, m_sources
, m_extensionGroup
, &results
);
78 v8::Local
<v8::Value
> scriptValue
= m_frame
->script().executeScriptInMainWorldAndReturnValue(m_sources
.first());
79 results
.append(scriptValue
);
82 // The script may have removed the frame, in which case contextDestroyed()
83 // will have handled the disposal/callback.
84 if (!m_frame
->client())
87 m_callback
->completed(results
);
91 void SuspendableScriptExecutor::dispose()
93 // Remove object as a ContextLifecycleObserver.
94 ActiveDOMObject::clearContext();
99 DEFINE_TRACE(SuspendableScriptExecutor
)
101 visitor
->trace(m_frame
);
102 visitor
->trace(m_sources
);
103 SuspendableTimer::trace(visitor
);