|
| 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 mate { |
| 20 | + |
| 21 | +template <> |
| 22 | +struct Converter<atom::AutoResizeFlags> { |
| 23 | + static bool FromV8(v8::Isolate* isolate, |
| 24 | + v8::Local<v8::Value> val, |
| 25 | + atom::AutoResizeFlags* auto_resize_flags) { |
| 26 | + mate::Dictionary params; |
| 27 | + if (!ConvertFromV8(isolate, val, ¶ms)) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + uint8_t flags = 0; |
| 32 | + bool width = false; |
| 33 | + if (params.Get("width", &width) && width) { |
| 34 | + flags |= atom::kAutoResizeWidth; |
| 35 | + } |
| 36 | + bool height = false; |
| 37 | + if (params.Get("height", &height) && height) { |
| 38 | + flags |= atom::kAutoResizeHeight; |
| 39 | + } |
| 40 | + |
| 41 | + *auto_resize_flags = static_cast<atom::AutoResizeFlags>(flags); |
| 42 | + return true; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +} // namespace mate |
| 47 | + |
| 48 | +namespace atom { |
| 49 | + |
| 50 | +namespace api { |
| 51 | + |
| 52 | +BrowserView::BrowserView(v8::Isolate* isolate, |
| 53 | + v8::Local<v8::Object> wrapper, |
| 54 | + const mate::Dictionary& options) |
| 55 | + : api_web_contents_(nullptr) { |
| 56 | + Init(isolate, wrapper, options); |
| 57 | +} |
| 58 | + |
| 59 | +void BrowserView::Init(v8::Isolate* isolate, |
| 60 | + v8::Local<v8::Object> wrapper, |
| 61 | + const mate::Dictionary& options) { |
| 62 | + mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate); |
| 63 | + options.Get(options::kWebPreferences, &web_preferences); |
| 64 | + web_preferences.Set("isBrowserView", true); |
| 65 | + mate::Handle<class WebContents> web_contents = |
| 66 | + WebContents::Create(isolate, web_preferences); |
| 67 | + |
| 68 | + web_contents_.Reset(isolate, web_contents.ToV8()); |
| 69 | + api_web_contents_ = web_contents.get(); |
| 70 | + |
| 71 | + view_.reset(NativeBrowserView::Create( |
| 72 | + api_web_contents_->managed_web_contents()->GetView())); |
| 73 | + |
| 74 | + InitWith(isolate, wrapper); |
| 75 | +} |
| 76 | + |
| 77 | +BrowserView::~BrowserView() { |
| 78 | + api_web_contents_->DestroyWebContents(true /* async */); |
| 79 | +} |
| 80 | + |
| 81 | +// static |
| 82 | +mate::WrappableBase* BrowserView::New(mate::Arguments* args) { |
| 83 | + if (!Browser::Get()->is_ready()) { |
| 84 | + args->ThrowError("Cannot create BrowserView before app is ready"); |
| 85 | + return nullptr; |
| 86 | + } |
| 87 | + |
| 88 | + if (args->Length() > 1) { |
| 89 | + args->ThrowError("Too many arguments"); |
| 90 | + return nullptr; |
| 91 | + } |
| 92 | + |
| 93 | + mate::Dictionary options; |
| 94 | + if (!(args->Length() == 1 && args->GetNext(&options))) { |
| 95 | + options = mate::Dictionary::CreateEmpty(args->isolate()); |
| 96 | + } |
| 97 | + |
| 98 | + return new BrowserView(args->isolate(), args->GetThis(), options); |
| 99 | +} |
| 100 | + |
| 101 | +int32_t BrowserView::ID() const { |
| 102 | + return weak_map_id(); |
| 103 | +} |
| 104 | + |
| 105 | +void BrowserView::SetAutoResize(AutoResizeFlags flags) { |
| 106 | + view_->SetAutoResizeFlags(flags); |
| 107 | +} |
| 108 | + |
| 109 | +void BrowserView::SetBounds(const gfx::Rect& bounds) { |
| 110 | + view_->SetBounds(bounds); |
| 111 | +} |
| 112 | + |
| 113 | +void BrowserView::SetBackgroundColor(const std::string& color_name) { |
| 114 | + view_->SetBackgroundColor(ParseHexColor(color_name)); |
| 115 | +} |
| 116 | + |
| 117 | +v8::Local<v8::Value> BrowserView::WebContents() { |
| 118 | + if (web_contents_.IsEmpty()) { |
| 119 | + return v8::Null(isolate()); |
| 120 | + } |
| 121 | + |
| 122 | + return v8::Local<v8::Value>::New(isolate(), web_contents_); |
| 123 | +} |
| 124 | + |
| 125 | +// static |
| 126 | +void BrowserView::BuildPrototype(v8::Isolate* isolate, |
| 127 | + v8::Local<v8::FunctionTemplate> prototype) { |
| 128 | + prototype->SetClassName(mate::StringToV8(isolate, "BrowserView")); |
| 129 | + mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) |
| 130 | + .MakeDestroyable() |
| 131 | + .SetMethod("setAutoResize", &BrowserView::SetAutoResize) |
| 132 | + .SetMethod("setBounds", &BrowserView::SetBounds) |
| 133 | + .SetMethod("setBackgroundColor", &BrowserView::SetBackgroundColor) |
| 134 | + .SetProperty("webContents", &BrowserView::WebContents) |
| 135 | + .SetProperty("id", &BrowserView::ID); |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace api |
| 139 | + |
| 140 | +} // namespace atom |
| 141 | + |
| 142 | +namespace { |
| 143 | + |
| 144 | +using atom::api::BrowserView; |
| 145 | + |
| 146 | +void Initialize(v8::Local<v8::Object> exports, |
| 147 | + v8::Local<v8::Value> unused, |
| 148 | + v8::Local<v8::Context> context, |
| 149 | + void* priv) { |
| 150 | + v8::Isolate* isolate = context->GetIsolate(); |
| 151 | + BrowserView::SetConstructor(isolate, base::Bind(&BrowserView::New)); |
| 152 | + |
| 153 | + mate::Dictionary browser_view( |
| 154 | + isolate, BrowserView::GetConstructor(isolate)->GetFunction()); |
| 155 | + |
| 156 | + mate::Dictionary dict(isolate, exports); |
| 157 | + dict.Set("BrowserView", browser_view); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace |
| 161 | + |
| 162 | +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_browser_view, Initialize) |
0 commit comments