1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "bindings/core/v8/ScriptStreamer.h"
#include "bindings/core/v8/ScriptSourceCode.h"
#include "bindings/core/v8/ScriptStreamerThread.h"
#include "bindings/core/v8/ScriptStreamingMode.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8ScriptRunner.h"
#include "core/dom/PendingScript.h"
#include "core/frame/Settings.h"
#include "platform/Task.h"
#include "platform/heap/Handle.h"
#include "public/platform/Platform.h"
#include <gtest/gtest.h>
#include <v8.h>
namespace blink {
namespace {
// For the benefit of Oilpan, put the part object PendingScript inside
// a wrapper that's on the Oilpan heap and hold a reference to that wrapper
// from ScriptStreamingTest.
class PendingScriptWrapper : public NoBaseWillBeGarbageCollectedFinalized<PendingScriptWrapper> {
public:
static PassOwnPtrWillBeRawPtr<PendingScriptWrapper> create()
{
return adoptPtrWillBeNoop(new PendingScriptWrapper());
}
static PassOwnPtrWillBeRawPtr<PendingScriptWrapper> create(Element* element, ScriptResource* resource)
{
return adoptPtrWillBeNoop(new PendingScriptWrapper(element, resource));
}
PendingScript& get() { return m_pendingScript; }
void trace(Visitor* visitor)
{
visitor->trace(m_pendingScript);
}
private:
PendingScriptWrapper()
{
}
PendingScriptWrapper(Element* element, ScriptResource* resource)
: m_pendingScript(PendingScript(element, resource))
{
}
PendingScript m_pendingScript;
};
// The bool param for ScriptStreamingTest controls whether to make the main
// thread block and wait for parsing.
class ScriptStreamingTest : public testing::TestWithParam<bool> {
public:
ScriptStreamingTest()
: m_scope(v8::Isolate::GetCurrent())
, m_settings(Settings::create())
, m_resourceRequest("http://www.streaming-test.com/")
, m_resource(ScriptResource::create(m_resourceRequest, "UTF-8").leakPtr())
, m_pendingScript(PendingScriptWrapper::create(0, m_resource)) // Takes ownership of m_resource.
{
m_settings->setV8ScriptStreamingEnabled(true);
if (GetParam())
m_settings->setV8ScriptStreamingMode(ScriptStreamingModeAllPlusBlockParsingBlocking);
m_resource->setLoading(true);
ScriptStreamer::setSmallScriptThresholdForTesting(0);
}
ScriptState* scriptState() const { return m_scope.scriptState(); }
v8::Isolate* isolate() const { return m_scope.isolate(); }
PendingScript& pendingScript() const { return m_pendingScript->get(); }
protected:
void appendData(const char* data)
{
m_resource->appendData(data, strlen(data));
// Yield control to the background thread, so that V8 gets a chance to
// process the data before the main thread adds more. Note that we
// cannot fully control in what kind of chunks the data is passed to V8
// (if V8 is not requesting more data between two appendData calls, it
// will get both chunks together).
Platform::current()->yieldCurrentThread();
}
void appendPadding()
{
for (int i = 0; i < 10; ++i) {
appendData(" /* this is padding to make the script long enough, so "
"that V8's buffer gets filled and it starts processing "
"the data */ ");
}
}
void finish()
{
m_resource->finish();
m_resource->setLoading(false);
}
void processTasksUntilStreamingComplete()
{
WebThread* currentThread = blink::Platform::current()->currentThread();
while (ScriptStreamerThread::shared()->isRunningTask()) {
currentThread->postTask(new Task(WTF::bind(&WebThread::exitRunLoop, currentThread)));
currentThread->enterRunLoop();
}
// Once more, because the "streaming complete" notification might only
// now be in the task queue.
currentThread->postTask(new Task(WTF::bind(&WebThread::exitRunLoop, currentThread)));
currentThread->enterRunLoop();
}
V8TestingScope m_scope;
OwnPtr<Settings> m_settings;
// The Resource and PendingScript where we stream from. These don't really
// fetch any data outside the test; the test controls the data by calling
// ScriptResource::appendData.
ResourceRequest m_resourceRequest;
ScriptResource* m_resource;
OwnPtrWillBePersistent<PendingScriptWrapper> m_pendingScript;
};
class TestScriptResourceClient : public ScriptResourceClient {
public:
TestScriptResourceClient()
: m_finished(false) { }
virtual void notifyFinished(Resource*) override { m_finished = true; }
bool finished() const { return m_finished; }
private:
bool m_finished;
};
TEST_P(ScriptStreamingTest, CompilingStreamedScript)
{
// Test that we can successfully compile a streamed script.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
appendData("function foo() {");
appendPadding();
appendData("return 5; }");
appendPadding();
appendData("foo();");
EXPECT_FALSE(client.finished());
finish();
// Process tasks on the main thread until the streaming background thread
// has completed its tasks.
processTasksUntilStreamingComplete();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_TRUE(sourceCode.streamer());
v8::TryCatch tryCatch;
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(sourceCode, isolate());
EXPECT_FALSE(script.IsEmpty());
EXPECT_FALSE(tryCatch.HasCaught());
}
TEST_P(ScriptStreamingTest, CompilingStreamedScriptWithParseError)
{
// Test that scripts with parse errors are handled properly. In those cases,
// the V8 side typically finished before loading finishes: make sure we
// handle it gracefully.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
appendData("function foo() {");
appendData("this is the part which will be a parse error");
// V8 won't realize the parse error until it actually starts parsing the
// script, and this happens only when its buffer is filled.
appendPadding();
EXPECT_FALSE(client.finished());
// Force the V8 side to finish before the loading.
processTasksUntilStreamingComplete();
EXPECT_FALSE(client.finished());
finish();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_TRUE(sourceCode.streamer());
v8::TryCatch tryCatch;
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(sourceCode, isolate());
EXPECT_TRUE(script.IsEmpty());
EXPECT_TRUE(tryCatch.HasCaught());
}
TEST_P(ScriptStreamingTest, CancellingStreaming)
{
// Test that the upper layers (PendingScript and up) can be ramped down
// while streaming is ongoing, and ScriptStreamer handles it gracefully.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
appendData("function foo() {");
// In general, we cannot control what the background thread is doing
// (whether it's parsing or waiting for more data). In this test, we have
// given it so little data that it's surely waiting for more.
// Simulate cancelling the network load (e.g., because the user navigated
// away).
EXPECT_FALSE(client.finished());
pendingScript().stopWatchingForLoad(&client);
pendingScript().releaseElementAndClear();
m_pendingScript = PendingScriptWrapper::create(); // This will destroy m_resource.
m_resource = 0;
// The V8 side will complete too. This should not crash. We don't receive
// any results from the streaming and the client doesn't get notified.
processTasksUntilStreamingComplete();
EXPECT_FALSE(client.finished());
}
TEST_P(ScriptStreamingTest, SuppressingStreaming)
{
// If we notice during streaming that there is a code cache, streaming
// is suppressed (V8 doesn't parse while the script is loading), and the
// upper layer (ScriptResourceClient) should get a notification when the
// script is loaded.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
appendData("function foo() {");
appendPadding();
m_resource->setCachedMetadata(V8ScriptRunner::tagForCodeCache(m_resource), "X", 1, Resource::CacheLocally);
appendPadding();
finish();
processTasksUntilStreamingComplete();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
// ScriptSourceCode doesn't refer to the streamer, since we have suppressed
// the streaming and resumed the non-streaming code path for script
// compilation.
EXPECT_FALSE(sourceCode.streamer());
}
TEST_P(ScriptStreamingTest, EmptyScripts)
{
// Empty scripts should also be streamed properly, that is, the upper layer
// (ScriptResourceClient) should be notified when an empty script has been
// loaded.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
// Finish the script without sending any data.
finish();
// The finished notification should arrive immediately and not be cycled
// through a background thread.
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_FALSE(sourceCode.streamer());
}
TEST_P(ScriptStreamingTest, SmallScripts)
{
// Small scripts shouldn't be streamed.
ScriptStreamer::setSmallScriptThresholdForTesting(100);
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
appendData("function foo() { }");
finish();
// The finished notification should arrive immediately and not be cycled
// through a background thread.
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_FALSE(sourceCode.streamer());
}
TEST_P(ScriptStreamingTest, ScriptsWithSmallFirstChunk)
{
// If a script is long enough, if should be streamed, even if the first data
// chunk is small.
ScriptStreamer::setSmallScriptThresholdForTesting(100);
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
// This is the first data chunk which is small.
appendData("function foo() { }");
appendPadding();
appendPadding();
appendPadding();
finish();
processTasksUntilStreamingComplete();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_TRUE(sourceCode.streamer());
v8::TryCatch tryCatch;
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(sourceCode, isolate());
EXPECT_FALSE(script.IsEmpty());
EXPECT_FALSE(tryCatch.HasCaught());
}
TEST_P(ScriptStreamingTest, EncodingChanges)
{
// It's possible that the encoding of the Resource changes after we start
// loading it.
m_resource->setEncoding("windows-1252");
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
m_resource->setEncoding("UTF-8");
// \xec\x92\x81 are the raw bytes for \uc481.
appendData("function foo() { var foob\xec\x92\x81r = 13; return foob\xec\x92\x81r; } foo();");
finish();
processTasksUntilStreamingComplete();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_TRUE(sourceCode.streamer());
v8::TryCatch tryCatch;
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(sourceCode, isolate());
EXPECT_FALSE(script.IsEmpty());
EXPECT_FALSE(tryCatch.HasCaught());
}
TEST_P(ScriptStreamingTest, EncodingFromBOM)
{
// Byte order marks should be removed before giving the data to V8. They
// will also affect encoding detection.
m_resource->setEncoding("windows-1252"); // This encoding is wrong on purpose.
ScriptStreamer::startStreaming(pendingScript(), m_settings.get(), m_scope.scriptState(), PendingScript::ParsingBlocking);
TestScriptResourceClient client;
pendingScript().watchForLoad(&client);
// \xef\xbb\xbf is the UTF-8 byte order mark. \xec\x92\x81 are the raw bytes
// for \uc481.
appendData("\xef\xbb\xbf function foo() { var foob\xec\x92\x81r = 13; return foob\xec\x92\x81r; } foo();");
finish();
processTasksUntilStreamingComplete();
EXPECT_TRUE(client.finished());
bool errorOccurred = false;
ScriptSourceCode sourceCode = pendingScript().getSource(KURL(), errorOccurred);
EXPECT_FALSE(errorOccurred);
EXPECT_TRUE(sourceCode.streamer());
v8::TryCatch tryCatch;
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(sourceCode, isolate());
EXPECT_FALSE(script.IsEmpty());
EXPECT_FALSE(tryCatch.HasCaught());
}
INSTANTIATE_TEST_CASE_P(ScriptStreamingInstantiation, ScriptStreamingTest, ::testing::Values(false, true));
} // namespace
} // namespace blink
|