OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/certificate_viewer_mac_cocoa.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/foundation_util.h" |
| 9 #include "chrome/browser/certificate_viewer.h" |
| 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" |
| 11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h" |
| 12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" |
| 13 |
| 14 class SSLCertificateViewerCocoaBridge : public ConstrainedWindowMacDelegate { |
| 15 public: |
| 16 explicit SSLCertificateViewerCocoaBridge( |
| 17 SSLCertificateViewerCocoa* controller) |
| 18 : controller_(controller) {} |
| 19 |
| 20 virtual ~SSLCertificateViewerCocoaBridge() {} |
| 21 |
| 22 // ConstrainedWindowMacDelegate implementation: |
| 23 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { |
| 24 // |onConstrainedWindowClosed| will delete the sheet which might still be |
| 25 // in use higher up the call stack. Wait for the next cycle of the event |
| 26 // loop to call this function. |
| 27 [controller_ performSelector:@selector(onConstrainedWindowClosed) |
| 28 withObject:nil |
| 29 afterDelay:0]; |
| 30 } |
| 31 |
| 32 private: |
| 33 SSLCertificateViewerCocoa* controller_; // Weak. Owns this. |
| 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(SSLCertificateViewerCocoaBridge); |
| 36 }; |
| 37 |
| 38 @implementation SSLCertificateViewerCocoa { |
| 39 std::unique_ptr<SSLCertificateViewerCocoaBridge> observer_; |
| 40 std::unique_ptr<ConstrainedWindowMac> constrainedWindow_; |
| 41 base::scoped_nsobject<NSWindow> overlayWindow_; |
| 42 // A copy of the sheet's frame. Used to restore on show. |
| 43 NSRect oldSheetFrame_; |
| 44 BOOL closePending_; |
| 45 // A copy of the sheet's |autoresizesSubviews| flag to restore on show. |
| 46 BOOL oldResizesSubviews_; |
| 47 } |
| 48 |
| 49 - (void)sheetDidEnd:(NSWindow*)parent |
| 50 returnCode:(NSInteger)returnCode |
| 51 context:(void*)context { |
| 52 if (!closePending_) |
| 53 constrainedWindow_->CloseWebContentsModalDialog(); |
| 54 } |
| 55 |
| 56 - (void)displayForWebContents:(content::WebContents*)webContents { |
| 57 [super displayForWebContents:webContents]; |
| 58 |
| 59 observer_.reset(new SSLCertificateViewerCocoaBridge(self)); |
| 60 constrainedWindow_ = |
| 61 CreateAndShowWebModalDialogMac(observer_.get(), webContents, self); |
| 62 } |
| 63 |
| 64 - (NSWindow*)overlayWindow { |
| 65 return overlayWindow_; |
| 66 } |
| 67 |
| 68 - (void)showSheetForWindow:(NSWindow*)window { |
| 69 overlayWindow_.reset([window retain]); |
| 70 [super showSheetForWindow:window]; |
| 71 } |
| 72 |
| 73 - (void)closeSheetWithAnimation:(BOOL)withAnimation { |
| 74 closePending_ = YES; |
| 75 overlayWindow_.reset(); |
| 76 [super closeSheetWithAnimation:withAnimation]; |
| 77 } |
| 78 |
| 79 - (void)hideSheet { |
| 80 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; |
| 81 [sheetWindow setAlphaValue:0.0]; |
| 82 [sheetWindow setIgnoresMouseEvents:YES]; |
| 83 |
| 84 oldResizesSubviews_ = [[sheetWindow contentView] autoresizesSubviews]; |
| 85 [[sheetWindow contentView] setAutoresizesSubviews:NO]; |
| 86 } |
| 87 |
| 88 - (void)unhideSheet { |
| 89 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; |
| 90 [sheetWindow setIgnoresMouseEvents:NO]; |
| 91 |
| 92 [[sheetWindow contentView] setAutoresizesSubviews:oldResizesSubviews_]; |
| 93 [[overlayWindow_ attachedSheet] setAlphaValue:1.0]; |
| 94 } |
| 95 |
| 96 - (void)makeSheetKeyAndOrderFront { |
| 97 [[overlayWindow_ attachedSheet] makeKeyAndOrderFront:nil]; |
| 98 } |
| 99 |
| 100 - (void)onConstrainedWindowClosed { |
| 101 [self releaseSheetWindow]; |
| 102 constrainedWindow_.reset(); |
| 103 [self release]; |
| 104 } |
| 105 |
| 106 @end |
| 107 |
| 108 void ShowCertificateViewer(content::WebContents* web_contents, |
| 109 gfx::NativeWindow parent, |
| 110 net::X509Certificate* cert) { |
| 111 // SSLCertificateViewerCocoa will manage its own lifetime and will release |
| 112 // itself when the dialog is closed. |
| 113 // See -[SSLCertificateViewerCocoa onConstrainedWindowClosed]. |
| 114 SSLCertificateViewerCocoa* viewer = |
| 115 [[SSLCertificateViewerCocoa alloc] initWithCertificate:cert]; |
| 116 [viewer displayForWebContents:web_contents]; |
| 117 } |
OLD | NEW |