Merge pull request #7952 from the-ress/window-setappid
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / atom_api_window.cc
1 // Copyright (c) 2013 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_window.h"
6 #include "atom/common/native_mate_converters/value_converter.h"
7
8 #include "atom/browser/api/atom_api_menu.h"
9 #include "atom/browser/api/atom_api_web_contents.h"
10 #include "atom/browser/browser.h"
11 #include "atom/browser/native_window.h"
12 #include "atom/common/native_mate_converters/callback.h"
13 #include "atom/common/native_mate_converters/file_path_converter.h"
14 #include "atom/common/native_mate_converters/gfx_converter.h"
15 #include "atom/common/native_mate_converters/gurl_converter.h"
16 #include "atom/common/native_mate_converters/image_converter.h"
17 #include "atom/common/native_mate_converters/string16_converter.h"
18 #include "atom/common/options_switches.h"
19 #include "base/command_line.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/common/content_switches.h"
22 #include "native_mate/constructor.h"
23 #include "native_mate/dictionary.h"
24 #include "ui/gfx/geometry/rect.h"
25
26 #if defined(TOOLKIT_VIEWS)
27 #include "atom/browser/native_window_views.h"
28 #endif
29
30 #if defined(OS_WIN)
31 #include "atom/browser/ui/win/taskbar_host.h"
32 #include "ui/base/win/shell.h"
33 #endif
34
35 #include "atom/common/node_includes.h"
36
37 #if defined(OS_WIN)
38 namespace mate {
39
40 template<>
41 struct Converter<atom::TaskbarHost::ThumbarButton> {
42   static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
43                      atom::TaskbarHost::ThumbarButton* out) {
44     mate::Dictionary dict;
45     if (!ConvertFromV8(isolate, val, &dict))
46       return false;
47     dict.Get("click", &(out->clicked_callback));
48     dict.Get("tooltip", &(out->tooltip));
49     dict.Get("flags", &out->flags);
50     return dict.Get("icon", &(out->icon));
51   }
52 };
53
54 }  // namespace mate
55 #endif
56
57 namespace atom {
58
59 namespace api {
60
61 namespace {
62
63 // Converts binary data to Buffer.
64 v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
65   auto buffer = node::Buffer::Copy(isolate, static_cast<char*>(val), size);
66   if (buffer.IsEmpty())
67     return v8::Null(isolate);
68   else
69     return buffer.ToLocalChecked();
70 }
71
72 }  // namespace
73
74
75 Window::Window(v8::Isolate* isolate, v8::Local<v8::Object> wrapper,
76                const mate::Dictionary& options) {
77   mate::Handle<class WebContents> web_contents;
78   // If no WebContents was passed to the constructor, create it from options.
79   if (!options.Get("webContents", &web_contents)) {
80     // Use options.webPreferences to create WebContents.
81     mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate);
82     options.Get(options::kWebPreferences, &web_preferences);
83
84     // Copy the backgroundColor to webContents.
85     v8::Local<v8::Value> value;
86     if (options.Get(options::kBackgroundColor, &value))
87       web_preferences.Set(options::kBackgroundColor, value);
88
89     v8::Local<v8::Value> transparent;
90     if (options.Get("transparent", &transparent))
91       web_preferences.Set("transparent", transparent);
92
93     // Creates the WebContents used by BrowserWindow.
94     web_contents = WebContents::Create(isolate, web_preferences);
95   }
96
97   Init(isolate, wrapper, options, web_contents);
98 }
99
100 void Window::Init(v8::Isolate* isolate,
101                   v8::Local<v8::Object> wrapper,
102                   const mate::Dictionary& options,
103                   mate::Handle<class WebContents> web_contents) {
104   web_contents_.Reset(isolate, web_contents.ToV8());
105   api_web_contents_ = web_contents.get();
106
107   // Keep a copy of the options for later use.
108   mate::Dictionary(isolate, web_contents->GetWrapper()).Set(
109       "browserWindowOptions", options);
110
111   // The parent window.
112   mate::Handle<Window> parent;
113   if (options.Get("parent", &parent))
114     parent_window_.Reset(isolate, parent.ToV8());
115
116   // Creates BrowserWindow.
117   window_.reset(NativeWindow::Create(
118       web_contents->managed_web_contents(),
119       options,
120       parent.IsEmpty() ? nullptr : parent->window_.get()));
121   web_contents->SetOwnerWindow(window_.get());
122
123 #if defined(TOOLKIT_VIEWS)
124   // Sets the window icon.
125   mate::Handle<NativeImage> icon;
126   if (options.Get(options::kIcon, &icon))
127     SetIcon(icon);
128 #endif
129
130   window_->InitFromOptions(options);
131   window_->AddObserver(this);
132
133   InitWith(isolate, wrapper);
134   AttachAsUserData(window_.get());
135
136   // We can only append this window to parent window's child windows after this
137   // window's JS wrapper gets initialized.
138   if (!parent.IsEmpty())
139     parent->child_windows_.Set(isolate, ID(), wrapper);
140 }
141
142 Window::~Window() {
143   if (!window_->IsClosed())
144     window_->CloseContents(nullptr);
145
146   // Destroy the native window in next tick because the native code might be
147   // iterating all windows.
148   base::MessageLoop::current()->DeleteSoon(FROM_HERE, window_.release());
149 }
150
151 void Window::WillCloseWindow(bool* prevent_default) {
152   *prevent_default = Emit("close");
153 }
154
155 void Window::WillDestroyNativeObject() {
156   // Close all child windows before closing current window.
157   v8::Locker locker(isolate());
158   v8::HandleScope handle_scope(isolate());
159   for (v8::Local<v8::Value> value : child_windows_.Values(isolate())) {
160     mate::Handle<Window> child;
161     if (mate::ConvertFromV8(isolate(), value, &child))
162       child->window_->CloseImmediately();
163   }
164 }
165
166 void Window::OnWindowClosed() {
167   api_web_contents_->DestroyWebContents();
168
169   RemoveFromWeakMap();
170   window_->RemoveObserver(this);
171
172   // We can not call Destroy here because we need to call Emit first, but we
173   // also do not want any method to be used, so just mark as destroyed here.
174   MarkDestroyed();
175
176   Emit("closed");
177
178   RemoveFromParentChildWindows();
179
180   // Destroy the native class when window is closed.
181   base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure());
182 }
183
184 void Window::OnWindowBlur() {
185   Emit("blur");
186 }
187
188 void Window::OnWindowFocus() {
189   Emit("focus");
190 }
191
192 void Window::OnWindowShow() {
193   Emit("show");
194 }
195
196 void Window::OnWindowHide() {
197   Emit("hide");
198 }
199
200 void Window::OnReadyToShow() {
201   Emit("ready-to-show");
202 }
203
204 void Window::OnWindowMaximize() {
205   Emit("maximize");
206 }
207
208 void Window::OnWindowUnmaximize() {
209   Emit("unmaximize");
210 }
211
212 void Window::OnWindowMinimize() {
213   Emit("minimize");
214 }
215
216 void Window::OnWindowRestore() {
217   Emit("restore");
218 }
219
220 void Window::OnWindowResize() {
221   Emit("resize");
222 }
223
224 void Window::OnWindowMove() {
225   Emit("move");
226 }
227
228 void Window::OnWindowMoved() {
229   Emit("moved");
230 }
231
232 void Window::OnWindowEnterFullScreen() {
233   Emit("enter-full-screen");
234 }
235
236 void Window::OnWindowLeaveFullScreen() {
237   Emit("leave-full-screen");
238 }
239
240 void Window::OnWindowScrollTouchBegin() {
241   Emit("scroll-touch-begin");
242 }
243
244 void Window::OnWindowScrollTouchEnd() {
245   Emit("scroll-touch-end");
246 }
247
248 void Window::OnWindowScrollTouchEdge() {
249   Emit("scroll-touch-edge");
250 }
251
252 void Window::OnWindowSwipe(const std::string& direction) {
253   Emit("swipe", direction);
254 }
255
256 void Window::OnWindowEnterHtmlFullScreen() {
257   Emit("enter-html-full-screen");
258 }
259
260 void Window::OnWindowLeaveHtmlFullScreen() {
261   Emit("leave-html-full-screen");
262 }
263
264 void Window::OnRendererUnresponsive() {
265   Emit("unresponsive");
266 }
267
268 void Window::OnRendererResponsive() {
269   Emit("responsive");
270 }
271
272 void Window::OnExecuteWindowsCommand(const std::string& command_name) {
273   Emit("app-command", command_name);
274 }
275
276 #if defined(OS_WIN)
277 void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
278   if (IsWindowMessageHooked(message)) {
279     messages_callback_map_[message].Run(
280         ToBuffer(isolate(), static_cast<void*>(&w_param), sizeof(WPARAM)),
281         ToBuffer(isolate(), static_cast<void*>(&l_param), sizeof(LPARAM)));
282   }
283 }
284 #endif
285
286 // static
287 mate::WrappableBase* Window::New(mate::Arguments* args) {
288   if (!Browser::Get()->is_ready()) {
289     args->ThrowError("Cannot create BrowserWindow before app is ready");
290     return nullptr;
291   }
292
293   if (args->Length() > 1) {
294     args->ThrowError();
295     return nullptr;
296   }
297
298   mate::Dictionary options;
299   if (!(args->Length() == 1 && args->GetNext(&options))) {
300     options = mate::Dictionary::CreateEmpty(args->isolate());
301   }
302
303   return new Window(args->isolate(), args->GetThis(), options);
304 }
305
306 void Window::Close() {
307   window_->Close();
308 }
309
310 void Window::Focus() {
311   window_->Focus(true);
312 }
313
314 void Window::Blur() {
315   window_->Focus(false);
316 }
317
318 bool Window::IsFocused() {
319   return window_->IsFocused();
320 }
321
322 void Window::Show() {
323   window_->Show();
324 }
325
326 void Window::ShowInactive() {
327   // This method doesn't make sense for modal window..
328   if (IsModal())
329     return;
330
331   window_->ShowInactive();
332 }
333
334 void Window::Hide() {
335   window_->Hide();
336 }
337
338 bool Window::IsVisible() {
339   return window_->IsVisible();
340 }
341
342 bool Window::IsEnabled() {
343   return window_->IsEnabled();
344 }
345
346 void Window::Maximize() {
347   window_->Maximize();
348 }
349
350 void Window::Unmaximize() {
351   window_->Unmaximize();
352 }
353
354 bool Window::IsMaximized() {
355   return window_->IsMaximized();
356 }
357
358 void Window::Minimize() {
359   window_->Minimize();
360 }
361
362 void Window::Restore() {
363   window_->Restore();
364 }
365
366 bool Window::IsMinimized() {
367   return window_->IsMinimized();
368 }
369
370 void Window::SetFullScreen(bool fullscreen) {
371   window_->SetFullScreen(fullscreen);
372 }
373
374 bool Window::IsFullscreen() {
375   return window_->IsFullscreen();
376 }
377
378 void Window::SetBounds(const gfx::Rect& bounds, mate::Arguments* args) {
379   bool animate = false;
380   args->GetNext(&animate);
381   window_->SetBounds(bounds, animate);
382 }
383
384 gfx::Rect Window::GetBounds() {
385   return window_->GetBounds();
386 }
387
388 void Window::SetContentBounds(const gfx::Rect& bounds, mate::Arguments* args) {
389   bool animate = false;
390   args->GetNext(&animate);
391   window_->SetContentBounds(bounds, animate);
392 }
393
394 gfx::Rect Window::GetContentBounds() {
395   return window_->GetContentBounds();
396 }
397
398 void Window::SetSize(int width, int height, mate::Arguments* args) {
399   bool animate = false;
400   args->GetNext(&animate);
401   window_->SetSize(gfx::Size(width, height), animate);
402 }
403
404 std::vector<int> Window::GetSize() {
405   std::vector<int> result(2);
406   gfx::Size size = window_->GetSize();
407   result[0] = size.width();
408   result[1] = size.height();
409   return result;
410 }
411
412 void Window::SetContentSize(int width, int height, mate::Arguments* args) {
413   bool animate = false;
414   args->GetNext(&animate);
415   window_->SetContentSize(gfx::Size(width, height), animate);
416 }
417
418 std::vector<int> Window::GetContentSize() {
419   std::vector<int> result(2);
420   gfx::Size size = window_->GetContentSize();
421   result[0] = size.width();
422   result[1] = size.height();
423   return result;
424 }
425
426 void Window::SetMinimumSize(int width, int height) {
427   window_->SetMinimumSize(gfx::Size(width, height));
428 }
429
430 std::vector<int> Window::GetMinimumSize() {
431   std::vector<int> result(2);
432   gfx::Size size = window_->GetMinimumSize();
433   result[0] = size.width();
434   result[1] = size.height();
435   return result;
436 }
437
438 void Window::SetMaximumSize(int width, int height) {
439   window_->SetMaximumSize(gfx::Size(width, height));
440 }
441
442 std::vector<int> Window::GetMaximumSize() {
443   std::vector<int> result(2);
444   gfx::Size size = window_->GetMaximumSize();
445   result[0] = size.width();
446   result[1] = size.height();
447   return result;
448 }
449
450 void Window::SetSheetOffset(double offsetY, mate::Arguments* args) {
451   double offsetX = 0.0;
452   args->GetNext(&offsetX);
453   window_->SetSheetOffset(offsetX, offsetY);
454 }
455
456 void Window::SetResizable(bool resizable) {
457   window_->SetResizable(resizable);
458 }
459
460 bool Window::IsResizable() {
461   return window_->IsResizable();
462 }
463
464 void Window::SetMovable(bool movable) {
465   window_->SetMovable(movable);
466 }
467
468 bool Window::IsMovable() {
469   return window_->IsMovable();
470 }
471
472 void Window::SetMinimizable(bool minimizable) {
473   window_->SetMinimizable(minimizable);
474 }
475
476 bool Window::IsMinimizable() {
477   return window_->IsMinimizable();
478 }
479
480 void Window::SetMaximizable(bool maximizable) {
481   window_->SetMaximizable(maximizable);
482 }
483
484 bool Window::IsMaximizable() {
485   return window_->IsMaximizable();
486 }
487
488 void Window::SetFullScreenable(bool fullscreenable) {
489   window_->SetFullScreenable(fullscreenable);
490 }
491
492 bool Window::IsFullScreenable() {
493   return window_->IsFullScreenable();
494 }
495
496 void Window::SetClosable(bool closable) {
497   window_->SetClosable(closable);
498 }
499
500 bool Window::IsClosable() {
501   return window_->IsClosable();
502 }
503
504 void Window::SetAlwaysOnTop(bool top, mate::Arguments* args) {
505   std::string level = "floating";
506   args->GetNext(&level);
507   window_->SetAlwaysOnTop(top, level);
508 }
509
510 bool Window::IsAlwaysOnTop() {
511   return window_->IsAlwaysOnTop();
512 }
513
514 void Window::Center() {
515   window_->Center();
516 }
517
518 void Window::SetPosition(int x, int y, mate::Arguments* args) {
519   bool animate = false;
520   args->GetNext(&animate);
521   window_->SetPosition(gfx::Point(x, y), animate);
522 }
523
524 std::vector<int> Window::GetPosition() {
525   std::vector<int> result(2);
526   gfx::Point pos = window_->GetPosition();
527   result[0] = pos.x();
528   result[1] = pos.y();
529   return result;
530 }
531
532 void Window::SetTitle(const std::string& title) {
533   window_->SetTitle(title);
534 }
535
536 std::string Window::GetTitle() {
537   return window_->GetTitle();
538 }
539
540 void Window::FlashFrame(bool flash) {
541   window_->FlashFrame(flash);
542 }
543
544 void Window::SetSkipTaskbar(bool skip) {
545   window_->SetSkipTaskbar(skip);
546 }
547
548 void Window::SetKiosk(bool kiosk) {
549   window_->SetKiosk(kiosk);
550 }
551
552 bool Window::IsKiosk() {
553   return window_->IsKiosk();
554 }
555
556 void Window::SetBackgroundColor(const std::string& color_name) {
557   window_->SetBackgroundColor(color_name);
558 }
559
560 void Window::SetHasShadow(bool has_shadow) {
561   window_->SetHasShadow(has_shadow);
562 }
563
564 bool Window::HasShadow() {
565   return window_->HasShadow();
566 }
567
568 void Window::FocusOnWebView() {
569   window_->FocusOnWebView();
570 }
571
572 void Window::BlurWebView() {
573   window_->BlurWebView();
574 }
575
576 bool Window::IsWebViewFocused() {
577   return window_->IsWebViewFocused();
578 }
579
580 void Window::SetRepresentedFilename(const std::string& filename) {
581   window_->SetRepresentedFilename(filename);
582 }
583
584 std::string Window::GetRepresentedFilename() {
585   return window_->GetRepresentedFilename();
586 }
587
588 void Window::SetDocumentEdited(bool edited) {
589   window_->SetDocumentEdited(edited);
590 }
591
592 bool Window::IsDocumentEdited() {
593   return window_->IsDocumentEdited();
594 }
595
596 void Window::SetIgnoreMouseEvents(bool ignore) {
597   return window_->SetIgnoreMouseEvents(ignore);
598 }
599
600 void Window::SetContentProtection(bool enable) {
601   return window_->SetContentProtection(enable);
602 }
603
604 void Window::SetFocusable(bool focusable) {
605   return window_->SetFocusable(focusable);
606 }
607
608 void Window::SetProgressBar(double progress, mate::Arguments* args) {
609   mate::Dictionary options;
610   std::string mode;
611   NativeWindow::ProgressState state = NativeWindow::PROGRESS_NORMAL;
612
613   args->GetNext(&options) && options.Get("mode", &mode);
614
615   if (mode == "error") {
616     state = NativeWindow::PROGRESS_ERROR;
617   } else if (mode == "paused") {
618     state = NativeWindow::PROGRESS_PAUSED;
619   } else if (mode == "indeterminate") {
620     state = NativeWindow::PROGRESS_INDETERMINATE;
621   } else if (mode == "none") {
622     state = NativeWindow::PROGRESS_NONE;
623   }
624
625   window_->SetProgressBar(progress, state);
626 }
627
628 void Window::SetOverlayIcon(const gfx::Image& overlay,
629                             const std::string& description) {
630   window_->SetOverlayIcon(overlay, description);
631 }
632
633 bool Window::SetThumbarButtons(mate::Arguments* args) {
634 #if defined(OS_WIN)
635   std::vector<TaskbarHost::ThumbarButton> buttons;
636   if (!args->GetNext(&buttons)) {
637     args->ThrowError();
638     return false;
639   }
640   auto window = static_cast<NativeWindowViews*>(window_.get());
641   return window->taskbar_host().SetThumbarButtons(
642       window_->GetAcceleratedWidget(), buttons);
643 #else
644   return false;
645 #endif
646 }
647
648 void Window::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
649   mate::Handle<Menu> menu;
650   if (value->IsObject() &&
651       mate::V8ToString(value->ToObject()->GetConstructorName()) == "Menu" &&
652       mate::ConvertFromV8(isolate, value, &menu)) {
653     menu_.Reset(isolate, menu.ToV8());
654     window_->SetMenu(menu->model());
655   } else if (value->IsNull()) {
656     menu_.Reset();
657     window_->SetMenu(nullptr);
658   } else {
659     isolate->ThrowException(v8::Exception::TypeError(
660         mate::StringToV8(isolate, "Invalid Menu")));
661   }
662 }
663
664 void Window::SetAutoHideMenuBar(bool auto_hide) {
665   window_->SetAutoHideMenuBar(auto_hide);
666 }
667
668 bool Window::IsMenuBarAutoHide() {
669   return window_->IsMenuBarAutoHide();
670 }
671
672 void Window::SetMenuBarVisibility(bool visible) {
673   window_->SetMenuBarVisibility(visible);
674 }
675
676 bool Window::IsMenuBarVisible() {
677   return window_->IsMenuBarVisible();
678 }
679
680 #if defined(OS_WIN)
681 bool Window::HookWindowMessage(UINT message,
682                                const MessageCallback& callback) {
683   messages_callback_map_[message] = callback;
684   return true;
685 }
686
687 void Window::UnhookWindowMessage(UINT message) {
688   if (!ContainsKey(messages_callback_map_, message))
689     return;
690
691   messages_callback_map_.erase(message);
692 }
693
694 bool Window::IsWindowMessageHooked(UINT message) {
695   return ContainsKey(messages_callback_map_, message);
696 }
697
698 void Window::UnhookAllWindowMessages() {
699   messages_callback_map_.clear();
700 }
701
702 bool Window::SetThumbnailClip(const gfx::Rect& region) {
703   auto window = static_cast<NativeWindowViews*>(window_.get());
704   return window->taskbar_host().SetThumbnailClip(
705       window_->GetAcceleratedWidget(), region);
706 }
707
708 bool Window::SetThumbnailToolTip(const std::string& tooltip) {
709   auto window = static_cast<NativeWindowViews*>(window_.get());
710   return window->taskbar_host().SetThumbnailToolTip(
711       window_->GetAcceleratedWidget(), tooltip);
712 }
713
714 void Window::SetAppDetails(const mate::Dictionary& options) {
715   base::string16 app_id;
716   base::FilePath app_icon_path;
717   int app_icon_index = 0;
718   base::string16 relaunch_command;
719   base::string16 relaunch_display_name;
720
721   options.Get("appId", &app_id);
722   options.Get("appIconPath", &app_icon_path);
723   options.Get("appIconIndex", &app_icon_index);
724   options.Get("relaunchCommand", &relaunch_command);
725   options.Get("relaunchDisplayName", &relaunch_display_name);
726
727   ui::win::SetAppDetailsForWindow(
728       app_id, app_icon_path, app_icon_index,
729       relaunch_command, relaunch_display_name,
730       window_->GetAcceleratedWidget());
731 }
732 #endif
733
734 #if defined(TOOLKIT_VIEWS)
735 void Window::SetIcon(mate::Handle<NativeImage> icon) {
736 #if defined(OS_WIN)
737   static_cast<NativeWindowViews*>(window_.get())->SetIcon(
738       icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
739       icon->GetHICON(GetSystemMetrics(SM_CXICON)));
740 #elif defined(USE_X11)
741   static_cast<NativeWindowViews*>(window_.get())->SetIcon(
742       icon->image().AsImageSkia());
743 #endif
744 }
745 #endif
746
747 void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) {
748   gfx::Size extra_size;
749   args->GetNext(&extra_size);
750   window_->SetAspectRatio(aspect_ratio, extra_size);
751 }
752
753 void Window::PreviewFile(const std::string& path, mate::Arguments* args) {
754   std::string display_name;
755   if (!args->GetNext(&display_name))
756     display_name = path;
757   window_->PreviewFile(path, display_name);
758 }
759
760 void Window::SetParentWindow(v8::Local<v8::Value> value,
761                              mate::Arguments* args) {
762   if (IsModal()) {
763     args->ThrowError("Can not be called for modal window");
764     return;
765   }
766
767   mate::Handle<Window> parent;
768   if (value->IsNull()) {
769     RemoveFromParentChildWindows();
770     parent_window_.Reset();
771     window_->SetParentWindow(nullptr);
772   } else if (mate::ConvertFromV8(isolate(), value, &parent)) {
773     parent_window_.Reset(isolate(), value);
774     window_->SetParentWindow(parent->window_.get());
775     parent->child_windows_.Set(isolate(), ID(), GetWrapper());
776   } else {
777     args->ThrowError("Must pass BrowserWindow instance or null");
778   }
779 }
780
781 v8::Local<v8::Value> Window::GetParentWindow() const {
782   if (parent_window_.IsEmpty())
783     return v8::Null(isolate());
784   else
785     return v8::Local<v8::Value>::New(isolate(), parent_window_);
786 }
787
788 std::vector<v8::Local<v8::Object>> Window::GetChildWindows() const {
789   return child_windows_.Values(isolate());
790 }
791
792 bool Window::IsModal() const {
793   return window_->is_modal();
794 }
795
796 v8::Local<v8::Value> Window::GetNativeWindowHandle() {
797   gfx::AcceleratedWidget handle = window_->GetAcceleratedWidget();
798   return ToBuffer(
799       isolate(), static_cast<void*>(&handle), sizeof(gfx::AcceleratedWidget));
800 }
801
802 void Window::SetVisibleOnAllWorkspaces(bool visible) {
803   return window_->SetVisibleOnAllWorkspaces(visible);
804 }
805
806 bool Window::IsVisibleOnAllWorkspaces() {
807   return window_->IsVisibleOnAllWorkspaces();
808 }
809
810 void Window::SetVibrancy(mate::Arguments* args) {
811   std::string type;
812
813   args->GetNext(&type);
814   window_->SetVibrancy(type);
815 }
816
817 int32_t Window::ID() const {
818   return weak_map_id();
819 }
820
821 v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) {
822   if (web_contents_.IsEmpty())
823     return v8::Null(isolate);
824   else
825     return v8::Local<v8::Value>::New(isolate, web_contents_);
826 }
827
828 void Window::RemoveFromParentChildWindows() {
829   if (parent_window_.IsEmpty())
830     return;
831
832   mate::Handle<Window> parent;
833   if (!mate::ConvertFromV8(isolate(), GetParentWindow(), &parent))
834     return;
835
836   parent->child_windows_.Remove(ID());
837 }
838
839 // static
840 void Window::BuildPrototype(v8::Isolate* isolate,
841                             v8::Local<v8::FunctionTemplate> prototype) {
842   prototype->SetClassName(mate::StringToV8(isolate, "BrowserWindow"));
843   mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
844       .MakeDestroyable()
845       .SetMethod("close", &Window::Close)
846       .SetMethod("focus", &Window::Focus)
847       .SetMethod("blur", &Window::Blur)
848       .SetMethod("isFocused", &Window::IsFocused)
849       .SetMethod("show", &Window::Show)
850       .SetMethod("showInactive", &Window::ShowInactive)
851       .SetMethod("hide", &Window::Hide)
852       .SetMethod("isVisible", &Window::IsVisible)
853       .SetMethod("isEnabled", &Window::IsEnabled)
854       .SetMethod("maximize", &Window::Maximize)
855       .SetMethod("unmaximize", &Window::Unmaximize)
856       .SetMethod("isMaximized", &Window::IsMaximized)
857       .SetMethod("minimize", &Window::Minimize)
858       .SetMethod("restore", &Window::Restore)
859       .SetMethod("isMinimized", &Window::IsMinimized)
860       .SetMethod("setFullScreen", &Window::SetFullScreen)
861       .SetMethod("isFullScreen", &Window::IsFullscreen)
862       .SetMethod("setAspectRatio", &Window::SetAspectRatio)
863       .SetMethod("previewFile", &Window::PreviewFile)
864 #if !defined(OS_WIN)
865       .SetMethod("setParentWindow", &Window::SetParentWindow)
866 #endif
867       .SetMethod("getParentWindow", &Window::GetParentWindow)
868       .SetMethod("getChildWindows", &Window::GetChildWindows)
869       .SetMethod("isModal", &Window::IsModal)
870       .SetMethod("getNativeWindowHandle", &Window::GetNativeWindowHandle)
871       .SetMethod("getBounds", &Window::GetBounds)
872       .SetMethod("setBounds", &Window::SetBounds)
873       .SetMethod("getSize", &Window::GetSize)
874       .SetMethod("setSize", &Window::SetSize)
875       .SetMethod("getContentBounds", &Window::GetContentBounds)
876       .SetMethod("setContentBounds", &Window::SetContentBounds)
877       .SetMethod("getContentSize", &Window::GetContentSize)
878       .SetMethod("setContentSize", &Window::SetContentSize)
879       .SetMethod("setMinimumSize", &Window::SetMinimumSize)
880       .SetMethod("getMinimumSize", &Window::GetMinimumSize)
881       .SetMethod("setMaximumSize", &Window::SetMaximumSize)
882       .SetMethod("getMaximumSize", &Window::GetMaximumSize)
883       .SetMethod("setSheetOffset", &Window::SetSheetOffset)
884       .SetMethod("setResizable", &Window::SetResizable)
885       .SetMethod("isResizable", &Window::IsResizable)
886       .SetMethod("setMovable", &Window::SetMovable)
887       .SetMethod("isMovable", &Window::IsMovable)
888       .SetMethod("setMinimizable", &Window::SetMinimizable)
889       .SetMethod("isMinimizable", &Window::IsMinimizable)
890       .SetMethod("setMaximizable", &Window::SetMaximizable)
891       .SetMethod("isMaximizable", &Window::IsMaximizable)
892       .SetMethod("setFullScreenable", &Window::SetFullScreenable)
893       .SetMethod("isFullScreenable", &Window::IsFullScreenable)
894       .SetMethod("setClosable", &Window::SetClosable)
895       .SetMethod("isClosable", &Window::IsClosable)
896       .SetMethod("setAlwaysOnTop", &Window::SetAlwaysOnTop)
897       .SetMethod("isAlwaysOnTop", &Window::IsAlwaysOnTop)
898       .SetMethod("center", &Window::Center)
899       .SetMethod("setPosition", &Window::SetPosition)
900       .SetMethod("getPosition", &Window::GetPosition)
901       .SetMethod("setTitle", &Window::SetTitle)
902       .SetMethod("getTitle", &Window::GetTitle)
903       .SetMethod("flashFrame", &Window::FlashFrame)
904       .SetMethod("setSkipTaskbar", &Window::SetSkipTaskbar)
905       .SetMethod("setKiosk", &Window::SetKiosk)
906       .SetMethod("isKiosk", &Window::IsKiosk)
907       .SetMethod("setBackgroundColor", &Window::SetBackgroundColor)
908       .SetMethod("setHasShadow", &Window::SetHasShadow)
909       .SetMethod("hasShadow", &Window::HasShadow)
910       .SetMethod("setRepresentedFilename", &Window::SetRepresentedFilename)
911       .SetMethod("getRepresentedFilename", &Window::GetRepresentedFilename)
912       .SetMethod("setDocumentEdited", &Window::SetDocumentEdited)
913       .SetMethod("isDocumentEdited", &Window::IsDocumentEdited)
914       .SetMethod("setIgnoreMouseEvents", &Window::SetIgnoreMouseEvents)
915       .SetMethod("setContentProtection", &Window::SetContentProtection)
916       .SetMethod("setFocusable", &Window::SetFocusable)
917       .SetMethod("focusOnWebView", &Window::FocusOnWebView)
918       .SetMethod("blurWebView", &Window::BlurWebView)
919       .SetMethod("isWebViewFocused", &Window::IsWebViewFocused)
920       .SetMethod("setProgressBar", &Window::SetProgressBar)
921       .SetMethod("setOverlayIcon", &Window::SetOverlayIcon)
922       .SetMethod("setThumbarButtons", &Window::SetThumbarButtons)
923       .SetMethod("setMenu", &Window::SetMenu)
924       .SetMethod("setAutoHideMenuBar", &Window::SetAutoHideMenuBar)
925       .SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide)
926       .SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility)
927       .SetMethod("isMenuBarVisible", &Window::IsMenuBarVisible)
928       .SetMethod("setVisibleOnAllWorkspaces",
929                  &Window::SetVisibleOnAllWorkspaces)
930       .SetMethod("isVisibleOnAllWorkspaces",
931                  &Window::IsVisibleOnAllWorkspaces)
932       .SetMethod("setVibrancy", &Window::SetVibrancy)
933 #if defined(OS_WIN)
934       .SetMethod("hookWindowMessage", &Window::HookWindowMessage)
935       .SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)
936       .SetMethod("unhookWindowMessage", &Window::UnhookWindowMessage)
937       .SetMethod("unhookAllWindowMessages", &Window::UnhookAllWindowMessages)
938       .SetMethod("setThumbnailClip", &Window::SetThumbnailClip)
939       .SetMethod("setThumbnailToolTip", &Window::SetThumbnailToolTip)
940       .SetMethod("setAppDetails", &Window::SetAppDetails)
941 #endif
942 #if defined(TOOLKIT_VIEWS)
943       .SetMethod("setIcon", &Window::SetIcon)
944 #endif
945       .SetProperty("id", &Window::ID)
946       .SetProperty("webContents", &Window::WebContents);
947 }
948
949 // static
950 v8::Local<v8::Value> Window::From(v8::Isolate* isolate,
951                                   NativeWindow* native_window) {
952   auto existing = TrackableObject::FromWrappedClass(isolate, native_window);
953   if (existing)
954     return existing->GetWrapper();
955   else
956     return v8::Null(isolate);
957 }
958
959 }  // namespace api
960
961 }  // namespace atom
962
963
964 namespace {
965
966 using atom::api::Window;
967
968 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
969                 v8::Local<v8::Context> context, void* priv) {
970   v8::Isolate* isolate = context->GetIsolate();
971   Window::SetConstructor(isolate, base::Bind(&Window::New));
972
973   mate::Dictionary browser_window(
974       isolate, Window::GetConstructor(isolate)->GetFunction());
975   browser_window.SetMethod("fromId",
976                            &mate::TrackableObject<Window>::FromWeakMapID);
977   browser_window.SetMethod("getAllWindows",
978                            &mate::TrackableObject<Window>::GetAll);
979
980   mate::Dictionary dict(isolate, exports);
981   dict.Set("BrowserWindow", browser_window);
982 }
983
984 }  // namespace
985
986 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_window, Initialize)