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