From 58b1172025ad754939aeefe7af2f2fe4b3e225ea Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 5 Aug 2015 12:46:32 +0800 Subject: [PATCH] Avoid exposing data members to subclass --- atom/browser/native_window.cc | 6 +++++ atom/browser/native_window.h | 41 ++++++++++++++++------------- atom/browser/native_window_mac.mm | 18 ++++++------- atom/browser/native_window_views.cc | 34 +++++++++++------------- 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 0461b7d74eb8..96085846bca8 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -522,6 +522,12 @@ void NativeWindow::NotifyWindowLeaveHtmlFullScreen() { OnWindowLeaveHtmlFullScreen()); } +void NativeWindow::NotifyWindowExecuteWindowsCommand( + const std::string& command) { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, + OnExecuteWindowsCommand(command)); +} + void NativeWindow::DevToolsFocused() { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnDevToolsFocus()); } diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 611ff82011c5..8919d529df03 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -205,11 +205,11 @@ class NativeWindow : public content::WebContentsObserver, void NotifyWindowLeaveFullScreen(); void NotifyWindowEnterHtmlFullScreen(); void NotifyWindowLeaveHtmlFullScreen(); + void NotifyWindowExecuteWindowsCommand(const std::string& command); void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); } - void RemoveObserver(NativeWindowObserver* obs) { observers_.RemoveObserver(obs); } @@ -219,7 +219,10 @@ class NativeWindow : public content::WebContentsObserver, } bool has_frame() const { return has_frame_; } + bool transparent() const { return transparent_; } SkRegion* draggable_region() const { return draggable_region_.get(); } + bool enable_larger_than_screen() const { return enable_larger_than_screen_; } + gfx::ImageSkia icon() const { return icon_; } void set_has_dialog_attached(bool has_dialog_attached) { has_dialog_attached_ = has_dialog_attached; @@ -240,21 +243,6 @@ class NativeWindow : public content::WebContentsObserver, void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; bool OnMessageReceived(const IPC::Message& message) override; - // Whether window has standard frame. - bool has_frame_; - - // Whether window is transparent. - bool transparent_; - - // Whether window can be resized larger than screen. - bool enable_larger_than_screen_; - - // Window icon. - gfx::ImageSkia icon_; - - // Observers of this window. - ObserverList observers_; - private: // Called when the window needs to update its draggable region. void UpdateDraggableRegions( @@ -271,6 +259,22 @@ class NativeWindow : public content::WebContentsObserver, const SkBitmap& bitmap, content::ReadbackResponse response); + // Whether window has standard frame. + bool has_frame_; + + // Whether window is transparent. + bool transparent_; + + // For custom drag, the whole window is non-draggable and the draggable region + // has to been explicitly provided. + scoped_ptr draggable_region_; // used in custom drag. + + // Whether window can be resized larger than screen. + bool enable_larger_than_screen_; + + // Window icon. + gfx::ImageSkia icon_; + // The windows has been closed. bool is_closed_; @@ -301,9 +305,8 @@ class NativeWindow : public content::WebContentsObserver, // The page this window is viewing. brightray::InspectableWebContents* inspectable_web_contents_; - // For custom drag, the whole window is non-draggable and the draggable region - // has to been explicitly provided. - scoped_ptr draggable_region_; // used in custom drag. + // Observers of this window. + ObserverList observers_; base::WeakPtrFactory weak_factory_; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 8a0955b2903c..d9ac903590f2 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -340,7 +340,7 @@ NativeWindowMac::NativeWindowMac( NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask; - if (!useStandardWindow || transparent_ || !has_frame_) { + if (!useStandardWindow || transparent() || !has_frame()) { styleMask |= NSTexturedBackgroundWindowMask; } @@ -350,12 +350,12 @@ NativeWindowMac::NativeWindowMac( backing:NSBackingStoreBuffered defer:YES]); [window_ setShell:this]; - [window_ setEnableLargerThanScreen:enable_larger_than_screen_]; + [window_ setEnableLargerThanScreen:enable_larger_than_screen()]; window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]); [window_ setDelegate:window_delegate_]; - if (transparent_) { + if (transparent()) { // Make window has transparent background. [window_ setOpaque:NO]; [window_ setHasShadow:NO]; @@ -363,7 +363,7 @@ NativeWindowMac::NativeWindowMac( } // Remove non-transparent corners, see http://git.io/vfonD. - if (!has_frame_) + if (!has_frame()) [window_ setOpaque:NO]; // We will manage window's lifetime ourselves. @@ -372,7 +372,7 @@ NativeWindowMac::NativeWindowMac( // On OS X the initial window size doesn't include window frame. bool use_content_size = false; options.Get(switches::kUseContentSize, &use_content_size); - if (!has_frame_ || !use_content_size) + if (!has_frame() || !use_content_size) SetSize(gfx::Size(width, height)); // Enable the NSView to accept first mouse event. @@ -509,7 +509,7 @@ gfx::Rect NativeWindowMac::GetBounds() { } void NativeWindowMac::SetContentSize(const gfx::Size& size) { - if (!has_frame_) { + if (!has_frame()) { SetSize(size); return; } @@ -527,7 +527,7 @@ void NativeWindowMac::SetContentSize(const gfx::Size& size) { } gfx::Size NativeWindowMac::GetContentSize() { - if (!has_frame_) + if (!has_frame()) return GetSize(); NSRect bounds = [[window_ contentView] bounds]; @@ -588,7 +588,7 @@ void NativeWindowMac::Center() { void NativeWindowMac::SetTitle(const std::string& title) { // We don't want the title to show in transparent window. - if (transparent_) + if (transparent()) return; [window_ setTitle:base::SysUTF8ToNSString(title)]; @@ -779,7 +779,7 @@ void NativeWindowMac::HandleKeyboardEvent( void NativeWindowMac::InstallView() { NSView* view = inspectable_web_contents()->GetView()->GetNativeView(); - if (has_frame_) { + if (has_frame()) { // Add layer with white background for the contents view. base::scoped_nsobject layer([[CALayer alloc] init]); [layer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 35aea41eb549..cb28f2dd9871 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -232,7 +232,7 @@ NativeWindowViews::NativeWindowViews( options.Get(switches::kResizable, &resizable_); #endif - if (enable_larger_than_screen_) + if (enable_larger_than_screen()) // We need to set a default maximum window size here otherwise Windows // will not allow us to resize the window larger than scree. // Setting directly to INT_MAX somehow doesn't work, so we just devide @@ -252,9 +252,9 @@ NativeWindowViews::NativeWindowViews( params.bounds = bounds; params.delegate = this; params.type = views::Widget::InitParams::TYPE_WINDOW; - params.remove_standard_frame = !has_frame_; + params.remove_standard_frame = !has_frame(); - if (transparent_) + if (transparent()) params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; #if defined(USE_X11) @@ -312,24 +312,24 @@ NativeWindowViews::NativeWindowViews( set_background(views::Background::CreateStandardPanelBackground()); AddChildView(web_view_); - if (has_frame_ && + if (has_frame() && options.Get(switches::kUseContentSize, &use_content_size_) && use_content_size_) bounds = ContentBoundsToWindowBounds(bounds); #if defined(OS_WIN) - if (!has_frame_) { + if (!has_frame()) { // Set Window style so that we get a minimize and maximize animation when // frameless. DWORD frame_style = WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION; // We should not show a frame for transparent window. - if (transparent_) + if (transparent()) frame_style &= ~(WS_THICKFRAME | WS_CAPTION); ::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, frame_style); } - if (transparent_) { + if (transparent()) { // Transparent window on Windows has to have WS_EX_COMPOSITED style. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE); ex_style |= WS_EX_COMPOSITED; @@ -339,14 +339,14 @@ NativeWindowViews::NativeWindowViews( // TODO(zcbenz): This was used to force using native frame on Windows 2003, we // should check whether setting it in InitParams can work. - if (has_frame_) { + if (has_frame()) { window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE); window_->FrameTypeChanged(); } // The given window is most likely not rectangular since it uses // transparency and has no standard frame, don't show a shadow for it. - if (transparent_ && !has_frame_) + if (transparent() && !has_frame()) wm::SetShadowType(GetNativeWindow(), wm::SHADOW_TYPE_NONE); window_->UpdateWindowIcon(); @@ -469,7 +469,7 @@ gfx::Rect NativeWindowViews::GetBounds() { } void NativeWindowViews::SetContentSize(const gfx::Size& size) { - if (!has_frame_) { + if (!has_frame()) { NativeWindow::SetSize(size); return; } @@ -480,7 +480,7 @@ void NativeWindowViews::SetContentSize(const gfx::Size& size) { } gfx::Size NativeWindowViews::GetContentSize() { - if (!has_frame_) + if (!has_frame()) return GetSize(); gfx::Size content_size = @@ -628,7 +628,7 @@ void NativeWindowViews::SetMenu(ui::MenuModel* menu_model) { #endif // Do not show menu bar in frameless window. - if (!has_frame_) + if (!has_frame()) return; if (!menu_bar_) { @@ -812,7 +812,7 @@ bool NativeWindowViews::ShouldHandleSystemCommands() const { } gfx::ImageSkia NativeWindowViews::GetWindowAppIcon() { - return icon_; + return icon(); } gfx::ImageSkia NativeWindowViews::GetWindowIcon() { @@ -840,7 +840,7 @@ bool NativeWindowViews::ShouldDescendIntoChildForEventHandling( return false; // And the events on border for dragging resizable frameless window. - if (!has_frame_ && CanResize()) { + if (!has_frame() && CanResize()) { FramelessView* frame = static_cast( window_->non_client_view()->frame_view()); return frame->ResizingBorderHitTest(location) == HTNOWHERE; @@ -860,7 +860,7 @@ views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView( frame_view->Init(this, widget); return frame_view; #else - if (has_frame_) { + if (has_frame()) { return new NativeFrameView(this, widget); } else { FramelessView* frame_view = new FramelessView; @@ -892,9 +892,7 @@ bool NativeWindowViews::ExecuteWindowsCommand(int command_id) { NotifyWindowMaximize(); } else { std::string command = AppCommandToString(command_id); - FOR_EACH_OBSERVER(NativeWindowObserver, - observers_, - OnExecuteWindowsCommand(command)); + NotifyWindowExecuteWindowsCommand(command); } return false; }