Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / SuspendableScriptExecutor.cpp
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.
4
5 #include "config.h"
6 #include "web/SuspendableScriptExecutor.h"
7
8 #include "bindings/core/v8/ScriptController.h"
9 #include "core/dom/Document.h"
10 #include "core/frame/LocalFrame.h"
11 #include "platform/UserGestureIndicator.h"
12 #include "public/platform/WebVector.h"
13 #include "public/web/WebScriptExecutionCallback.h"
14
15 namespace blink {
16
17 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
18 {
19     SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, worldID, sources, extensionGroup, userGesture, callback);
20     executor->run();
21 }
22
23 void SuspendableScriptExecutor::resume()
24 {
25     executeAndDestroySelf();
26 }
27
28 void SuspendableScriptExecutor::contextDestroyed()
29 {
30     // this method can only be called if the script was not called in run()
31     // and context remained suspend (method resume has never called)
32     ActiveDOMObject::contextDestroyed();
33     m_callback->completed(Vector<v8::Local<v8::Value> >());
34     delete this;
35 }
36
37 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
38     : ActiveDOMObject(frame->document())
39     , m_frame(frame)
40     , m_worldID(worldID)
41     , m_sources(sources)
42     , m_extensionGroup(extensionGroup)
43     , m_userGesture(userGesture)
44     , m_callback(callback)
45 {
46 }
47
48 SuspendableScriptExecutor::~SuspendableScriptExecutor()
49 {
50 }
51
52 void SuspendableScriptExecutor::run()
53 {
54     suspendIfNeeded();
55     ExecutionContext* context = executionContext();
56     ASSERT(context);
57     if (context && !context->activeDOMObjectsAreSuspended())
58         executeAndDestroySelf();
59 }
60
61 void SuspendableScriptExecutor::executeAndDestroySelf()
62 {
63     // after calling the destructor of object - object will be unsubscribed from
64     // resumed and contextDestroyed LifecycleObserver methods
65     OwnPtr<UserGestureIndicator> indicator;
66     if (m_userGesture)
67         indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
68
69     v8::HandleScope scope(v8::Isolate::GetCurrent());
70     Vector<v8::Local<v8::Value> > results;
71     if (m_worldID) {
72         m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_extensionGroup, &results);
73     } else {
74         v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMainWorldAndReturnValue(m_sources.first());
75         results.append(scriptValue);
76     }
77     m_callback->completed(results);
78     delete this;
79 }
80
81
82 } // namespace blink