Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "public/platform/WebStreams.h" | |
| 7 | |
| 8 #include "wtf/Assertions.h" | |
| 9 | |
| 10 namespace blink { | |
| 11 | |
| 12 const char kStreamsExtensionName[] = "blink/streams"; | |
| 13 | |
| 14 // TODO use js2c.py in a gyp file somehow to externalize this into a .js file | |
| 15 // Apparently the presubmit checks hate multiline strings because they hate free dom | |
| 16 const char kStreamsExtensionCode[] = R"JSCODE( | |
| 17 'use strict'; | |
| 18 | |
| 19 native function SET_PRIVATE(); | |
| 20 native function GET_PRIVATE(); | |
| 21 | |
| 22 this.ReadableStream = ReadableStream; | |
| 23 | |
| 24 function ReadableStream(underlyingSource) { | |
| 25 if (underlyingSource === undefined) { | |
| 26 underlyingSource = {}; | |
| 27 } | |
| 28 | |
| 29 SET_PRIVATE(this, 'ReadableStream#underlyingSource', underlyingSource); | |
| 30 } | |
| 31 | |
| 32 ReadableStream.prototype.logUnderlyingSource = function () { | |
| 33 console.log(GET_PRIVATE(this, 'ReadableStream#underlyingSource')); | |
| 34 }; | |
| 35 | |
| 36 )JSCODE"; | |
| 37 | |
| 38 class StreamsExtensionImpl : public v8::Extension { | |
| 39 public: | |
| 40 StreamsExtensionImpl() : v8::Extension(kStreamsExtensionName, kStreamsEx tensionCode) { } | |
| 41 | |
| 42 private: | |
| 43 v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( | |
| 44 v8::Isolate* isolate, v8::Handle<v8::String> name) override | |
| 45 { | |
| 46 | |
| 47 if (name->Equals(v8::String::NewFromUtf8(isolate, "SET_PRIVATE"))) { | |
| 48 return v8::FunctionTemplate::New(isolate, SetPrivate); | |
| 49 } | |
| 50 if (name->Equals(v8::String::NewFromUtf8(isolate, "GET_PRIVATE"))) { | |
| 51 return v8::FunctionTemplate::New(isolate, GetPrivate); | |
| 52 } | |
| 53 | |
| 54 return v8::Handle<v8::FunctionTemplate>(); | |
| 55 } | |
| 56 | |
| 57 static void SetPrivate(const v8::FunctionCallbackInfo<v8::Value>& info) | |
| 58 { | |
| 59 ASSERT(info.Length() == 3 && info[0]->IsObject() && info[1]->IsStrin g()); | |
| 60 | |
| 61 auto isolate = info.GetIsolate(); | |
| 62 v8::HandleScope scope(isolate); | |
| 63 auto object = info[0].As<v8::Object>(); | |
| 64 auto name = info[1].As<v8::String>(); | |
| 65 auto value = info[2]; | |
| 66 | |
| 67 auto privateSymbol = v8::Private::ForApi(isolate, name); | |
| 68 object->SetPrivate(privateSymbol, value); | |
| 69 } | |
| 70 | |
| 71 static void GetPrivate(const v8::FunctionCallbackInfo<v8::Value>& info) | |
| 72 { | |
| 73 ASSERT(info.Length() == 2 && info[0]->IsObject() && info[1]->IsStrin g()); | |
| 74 | |
| 75 auto isolate = info.GetIsolate(); | |
| 76 v8::HandleScope scope(isolate); | |
| 77 auto object = info[0].As<v8::Object>(); | |
| 78 auto name = info[1].As<v8::String>(); | |
| 79 | |
| 80 auto privateSymbol = v8::Private::ForApi(isolate, name); | |
|
Dmitry Lomov (no reviews)
2015/02/13 16:39:56
Hmm I do not get it. Seems like you are creating a
| |
| 81 auto value = object->GetPrivate(privateSymbol); | |
| 82 | |
| 83 info.GetReturnValue().Set(value); | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 v8::Extension* WebStreams::CreateV8Extension() | |
| 88 { | |
| 89 return new StreamsExtensionImpl(); | |
| 90 } | |
| 91 | |
| 92 } // namespace blink | |
| OLD | NEW |