atom_api_browser_view.cc 4.8 KB

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