Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Side by Side Diff: Source/web/SuspendableScriptExecutor.cpp

Issue 649183010: Added SuspendableScriptExecutor::createAndRun to replace ctor+run (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@a
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "web/SuspendableScriptExecutor.h" 6 #include "web/SuspendableScriptExecutor.h"
7 7
8 #include "bindings/core/v8/ScriptController.h" 8 #include "bindings/core/v8/ScriptController.h"
9 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
10 #include "core/frame/LocalFrame.h" 10 #include "core/frame/LocalFrame.h"
11 #include "platform/UserGestureIndicator.h" 11 #include "platform/UserGestureIndicator.h"
12 #include "public/platform/WebVector.h" 12 #include "public/platform/WebVector.h"
13 #include "public/web/WebScriptExecutionCallback.h" 13 #include "public/web/WebScriptExecutionCallback.h"
14 14
15 namespace blink { 15 namespace blink {
16 16
17 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con st Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebS criptExecutionCallback* callback)
18 {
19 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, w orldID, 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
17 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl dID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGestu re, WebScriptExecutionCallback* callback) 37 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl dID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGestu re, WebScriptExecutionCallback* callback)
18 : ActiveDOMObject(frame->document()) 38 : ActiveDOMObject(frame->document())
19 , m_frame(frame) 39 , m_frame(frame)
20 , m_worldID(worldID) 40 , m_worldID(worldID)
21 , m_sources(sources) 41 , m_sources(sources)
22 , m_extensionGroup(extensionGroup) 42 , m_extensionGroup(extensionGroup)
23 , m_userGesture(userGesture) 43 , m_userGesture(userGesture)
24 , m_callback(callback) 44 , m_callback(callback)
25 { 45 {
26 } 46 }
27 47
28 SuspendableScriptExecutor::~SuspendableScriptExecutor() 48 SuspendableScriptExecutor::~SuspendableScriptExecutor()
29 { 49 {
30 } 50 }
31 51
32 void SuspendableScriptExecutor::run() 52 void SuspendableScriptExecutor::run()
33 { 53 {
34 suspendIfNeeded(); 54 suspendIfNeeded();
35 ExecutionContext* context = executionContext(); 55 ExecutionContext* context = executionContext();
36 ASSERT(context); 56 ASSERT(context);
37 if (context && !context->activeDOMObjectsAreSuspended()) 57 if (context && !context->activeDOMObjectsAreSuspended())
38 executeAndDestroySelf(); 58 executeAndDestroySelf();
39 } 59 }
40 60
41 void SuspendableScriptExecutor::resume()
42 {
43 executeAndDestroySelf();
44 }
45
46 void SuspendableScriptExecutor::contextDestroyed()
47 {
48 // this method can only be called if the script was not called in run()
49 // and context remained suspend (method resume has never called)
50 ActiveDOMObject::contextDestroyed();
51 m_callback->completed(Vector<v8::Local<v8::Value> >());
52 delete this;
53 }
54
55 void SuspendableScriptExecutor::executeAndDestroySelf() 61 void SuspendableScriptExecutor::executeAndDestroySelf()
56 { 62 {
57 // after calling the destructor of object - object will be unsubscribed from 63 // after calling the destructor of object - object will be unsubscribed from
58 // resumed and contextDestroyed LifecycleObserver methods 64 // resumed and contextDestroyed LifecycleObserver methods
59 OwnPtr<UserGestureIndicator> indicator; 65 OwnPtr<UserGestureIndicator> indicator;
60 if (m_userGesture) 66 if (m_userGesture)
61 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse rGesture)); 67 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse rGesture));
62 68
63 v8::HandleScope scope(v8::Isolate::GetCurrent()); 69 v8::HandleScope scope(v8::Isolate::GetCurrent());
64 Vector<v8::Local<v8::Value> > results; 70 Vector<v8::Local<v8::Value> > results;
65 if (m_worldID) { 71 if (m_worldID) {
66 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e xtensionGroup, &results); 72 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e xtensionGroup, &results);
67 } else { 73 } else {
68 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain WorldAndReturnValue(m_sources.first()); 74 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain WorldAndReturnValue(m_sources.first());
69 results.append(scriptValue); 75 results.append(scriptValue);
70 } 76 }
71 m_callback->completed(results); 77 m_callback->completed(results);
72 delete this; 78 delete this;
73 } 79 }
74 80
75 81
76 } // namespace blink 82 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698