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

content / public / renderer / chrome_object_extensions_utils.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/renderer/chrome_object_extensions_utils.h"

#include "gin/converter.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-object.h"

namespace content {

v8::Local<v8::Object> GetOrCreateChromeObject(v8::Isolate* isolate,
                                              v8::Local<v8::Context> context) {
  return GetOrCreateObject(isolate, context, "chrome");
}

v8::Local<v8::Object> GetOrCreateObject(v8::Isolate* isolate,
                                        v8::Local<v8::Context> context,
                                        const std::string& object_name) {
  v8::Local<v8::Object> global = context->Global();
  return GetOrCreateObject(isolate, context, global, object_name);
}

v8::Local<v8::Object> GetOrCreateObject(v8::Isolate* isolate,
                                        v8::Local<v8::Context> context,
                                        v8::Local<v8::Object> parent,
                                        const std::string& object_name) {
  v8::Local<v8::Object> new_object;
  v8::Local<v8::Value> object_value;
  if (!parent->Get(context, gin::StringToV8(isolate, object_name))
           .ToLocal(&object_value) ||
      !object_value->IsObject()) {
    new_object = v8::Object::New(isolate);
    parent->Set(context, gin::StringToSymbol(isolate, object_name), new_object)
        .Check();
  } else {
    new_object = v8::Local<v8::Object>::Cast(object_value);
  }
  return new_object;
}

}  // namespace content