|
| 1 | +// Copyright (c) 2017 GitHub, Inc. |
| 2 | +// Use of this source code is governed by the MIT license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "atom/browser/api/atom_api_browser_view.h" |
| 6 | + |
| 7 | +#include "atom/browser/api/atom_api_web_contents.h" |
| 8 | +#include "atom/browser/browser.h" |
| 9 | +#include "atom/browser/native_browser_view.h" |
| 10 | +#include "atom/common/color_util.h" |
| 11 | +#include "atom/common/native_mate_converters/gfx_converter.h" |
| 12 | +#include "atom/common/native_mate_converters/value_converter.h" |
| 13 | +#include "atom/common/node_includes.h" |
| 14 | +#include "atom/common/options_switches.h" |
| 15 | +#include "native_mate/constructor.h" |
| 16 | +#include "native_mate/dictionary.h" |
| 17 | +#include "ui/gfx/geometry/rect.h" |
| 18 | + |
| 19 | +namespace atom { |
| 20 | + |
| 21 | +namespace api { |
| 22 | + |
| 23 | +BrowserView::BrowserView(v8::Isolate* isolate, |
| 24 | + v8::Local<v8::Object> wrapper, |
| 25 | + const mate::Dictionary& options) |
| 26 | + : api_web_contents_(nullptr) { |
| 27 | + Init(isolate, wrapper, options); |
| 28 | +} |
| 29 | + |
| 30 | +void BrowserView::Init(v8::Isolate* isolate, |
| 31 | + v8::Local<v8::Object> wrapper, |
| 32 | + const mate::Dictionary& options) { |
| 33 | + mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate); |
| 34 | + options.Get(options::kWebPreferences, &web_preferences); |
| 35 | + web_preferences.Set("isBrowserView", true); |
| 36 | + mate::Handle<class WebContents> web_contents = |
| 37 | + WebContents::Create(isolate, web_preferences); |
| 38 | + |
| 39 | + web_contents_.Reset(isolate, web_contents.ToV8()); |
| 40 | + api_web_contents_ = web_contents.get(); |
| 41 | + |
| 42 | + view_.reset(NativeBrowserView::Create( |
| 43 | + api_web_contents_->managed_web_contents()->GetView())); |
| 44 | + |
| 45 | + InitWith(isolate, wrapper); |
| 46 | +} |
| 47 | + |
| 48 | +BrowserView::~BrowserView() { |
| 49 | + api_web_contents_->DestroyWebContents(); |
| 50 | +} |
| 51 | + |
| 52 | +// static |
| 53 | +mate::WrappableBase* BrowserView::New(mate::Arguments* args) { |
| 54 | + if (!Browser::Get()->is_ready()) { |
| 55 | + args->ThrowError("Cannot create BrowserView before app is ready"); |
| 56 | + return nullptr; |
| 57 | + } |
| 58 | + |
| 59 | + if (args->Length() > 1) { |
| 60 | + args->ThrowError("Too many arguments"); |
| 61 | + return nullptr; |
| 62 | + } |
| 63 | + |
| 64 | + mate::Dictionary options; |
| 65 | + if (!(args->Length() == 1 && args->GetNext(&options))) { |
| 66 | + options = mate::Dictionary::CreateEmpty(args->isolate()); |
| 67 | + } |
| 68 | + |
| 69 | + return new BrowserView(args->isolate(), args->GetThis(), options); |
| 70 | +} |
| 71 | + |
| 72 | +int32_t BrowserView::ID() const { |
| 73 | + return weak_map_id(); |
| 74 | +} |
| 75 | + |
| 76 | +void BrowserView::SetBounds(const gfx::Rect& bounds) { |
| 77 | + view_->SetBounds(bounds); |
| 78 | +} |
| 79 | + |
| 80 | +void BrowserView::SetBackgroundColor(const std::string& color_name) { |
| 81 | + view_->SetBackgroundColor(ParseHexColor(color_name)); |
| 82 | +} |
| 83 | + |
| 84 | +v8::Local<v8::Value> BrowserView::WebContents() { |
| 85 | + if (web_contents_.IsEmpty()) { |
| 86 | + return v8::Null(isolate()); |
| 87 | + } |
| 88 | + |
| 89 | + return v8::Local<v8::Value>::New(isolate(), web_contents_); |
| 90 | +} |
| 91 | + |
| 92 | +// static |
| 93 | +void BrowserView::BuildPrototype(v8::Isolate* isolate, |
| 94 | + v8::Local<v8::FunctionTemplate> prototype) { |
| 95 | + prototype->SetClassName(mate::StringToV8(isolate, "BrowserView")); |
| 96 | + mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) |
| 97 | + .MakeDestroyable() |
| 98 | + .SetMethod("setBounds", &BrowserView::SetBounds) |
| 99 | + .SetMethod("setBackgroundColor", &BrowserView::SetBackgroundColor) |
| 100 | + .SetProperty("webContents", &BrowserView::WebContents) |
| 101 | + .SetProperty("id", &BrowserView::ID); |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace api |
| 105 | + |
| 106 | +} // namespace atom |
| 107 | + |
| 108 | +namespace { |
| 109 | + |
| 110 | +using atom::api::BrowserView; |
| 111 | + |
| 112 | +void Initialize(v8::Local<v8::Object> exports, |
| 113 | + v8::Local<v8::Value> unused, |
| 114 | + v8::Local<v8::Context> context, |
| 115 | + void* priv) { |
| 116 | + v8::Isolate* isolate = context->GetIsolate(); |
| 117 | + BrowserView::SetConstructor(isolate, base::Bind(&BrowserView::New)); |
| 118 | + |
| 119 | + mate::Dictionary browser_view( |
| 120 | + isolate, BrowserView::GetConstructor(isolate)->GetFunction()); |
| 121 | + |
| 122 | + mate::Dictionary dict(isolate, exports); |
| 123 | + dict.Set("BrowserView", browser_view); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace |
| 127 | + |
| 128 | +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_browser_view, Initialize) |
0 commit comments