Shaka Player Embedded
byte_string.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <algorithm>
18 #include <memory>
19 #include <utility>
20 
22 
23 namespace shaka {
24 
26  : vector(source, source + strlen(source)) {}
27 
28 ByteString::ByteString(const std::string& source)
29  : vector(source.begin(), source.end()) {}
30 
31 
32 bool ByteString::TryConvert(Handle<JsValue> value) {
33 #if defined(USING_V8)
34  if (value.IsEmpty() || !value->IsString())
35  return false;
36 
37  v8::String::Value value_raw(GetIsolate(), value);
38  const uint16_t* data = *value_raw;
39  const size_t length = value_raw.length();
40  if (length == 0 && value.As<v8::String>()->Length() != 0)
41  return false;
42 #elif defined(USING_JSC)
43  JSContextRef cx = GetContext();
44  if (!value || !JSValueIsString(cx, value))
45  return false;
46 
47  // TODO: Avoid this copy if possible.
48  LocalVar<JsString> str(JSValueToStringCopy(cx, value, nullptr));
49  if (!str)
50  return false;
51  const uint16_t* data = JSStringGetCharactersPtr(str);
52  const size_t length = JSStringGetLength(str);
53 #endif
54 
55  std::vector<uint8_t> results(length);
56  DCHECK_EQ(results.size(), length);
57  for (size_t i = 0; i < length; i++) {
58  if (data[i] > 0xFF) {
59  LOG(WARNING) << "The string to be encoded contains characters outside "
60  "the Latin1 range.";
61  return false;
62  }
63  results[i] = static_cast<uint8_t>(data[i]);
64  }
65  swap(results);
66  return true;
67 }
68 
69 ReturnVal<JsValue> ByteString::ToJsValue() const {
70 #if defined(USING_V8)
71  return v8::String::NewFromOneByte(GetIsolate(), data(),
72  v8::NewStringType::kNormal, size())
73  .ToLocalChecked();
74 #elif defined(USING_JSC)
75  std::unique_ptr<uint16_t[]> temp_data(new uint16_t[size()]);
76  // Copy the data by writing each source byte to its own 16-bit element.
77  std::copy(data(), data() + size(), temp_data.get());
78  LocalVar<JsString> str(JSStringCreateWithCharacters(temp_data.get(), size()));
79  CHECK(str);
80  return JSValueMakeString(GetContext(), str);
81 #endif
82 }
83 
84 } // namespace shaka
ReturnVal< JsValue > ToJsValue() const override
Definition: byte_string.cc:69
bool TryConvert(Handle< JsValue > value) override
Definition: byte_string.cc:32
const char * source
Definition: media_utils.cc:30
void swap(shared_lock< Mutex > &a, shared_lock< Mutex > &b)
Definition: shared_lock.h:161
ByteString(const char *source)
Definition: byte_string.cc:25
JSContextRef GetContext()
Definition: jsc_utils.cc:24
v8::Isolate * GetIsolate()
Definition: v8_utils.cc:27