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_con
troller.h" |
| 12 |
| 13 @interface SSLCertificateViewerCocoa () |
| 14 - (instancetype)initWithCertificate:(net::X509Certificate*)certificate |
| 15 forWebContents:(content::WebContents*)webContents; |
| 16 - (void)onConstrainedWindowClosed; |
| 17 @end |
| 18 |
| 19 namespace { |
| 20 |
| 21 class SSLCertificateViewerCocoaBridge : public ConstrainedWindowMacDelegate { |
| 22 public: |
| 23 explicit SSLCertificateViewerCocoaBridge( |
| 24 SSLCertificateViewerCocoa* controller) |
| 25 : controller_(controller) {} |
| 26 |
| 27 // ConstrainedWindowMacDelegate: |
| 28 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { |
| 29 // |onConstrainedWindowClosed| will delete the sheet which might still be |
| 30 // in use higher up the call stack. Wait for the next cycle of the event |
| 31 // loop to call this function. |
| 32 [controller_ performSelector:@selector(onConstrainedWindowClosed) |
| 33 withObject:nil |
| 34 afterDelay:0]; |
| 35 } |
| 36 |
| 37 private: |
| 38 SSLCertificateViewerCocoa* controller_; // Weak. Owns this. |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(SSLCertificateViewerCocoaBridge); |
| 41 }; |
| 42 |
| 43 } // namespace |
| 44 |
| 45 @implementation SSLCertificateViewerCocoa { |
| 46 std::unique_ptr<SSLCertificateViewerCocoaBridge> observer_; |
| 47 std::unique_ptr<ConstrainedWindowMac> constrainedWindow_; |
| 48 base::scoped_nsobject<NSWindow> overlayWindow_; |
| 49 // A copy of the sheet's frame. Used to restore on show. |
| 50 NSRect oldSheetFrame_; |
| 51 BOOL closePending_; |
| 52 // A copy of the sheet's |autoresizesSubviews| flag to restore on show. |
| 53 BOOL oldResizesSubviews_; |
| 54 } |
| 55 |
| 56 - (instancetype)initWithCertificate:(net::X509Certificate*)certificate |
| 57 forWebContents:(content::WebContents*)webContents { |
| 58 if ((self = [super initWithCertificate:certificate |
| 59 forWebContents:webContents])) { |
| 60 observer_.reset(new SSLCertificateViewerCocoaBridge(self)); |
| 61 constrainedWindow_ = |
| 62 CreateAndShowWebModalDialogMac(observer_.get(), webContents, self); |
| 63 } |
| 64 return self; |
| 65 } |
| 66 |
| 67 - (void)onConstrainedWindowClosed { |
| 68 [self releaseSheetWindow]; |
| 69 constrainedWindow_.reset(); |
| 70 [self release]; |
| 71 } |
| 72 |
| 73 // SSLCertificateViewerMac overrides: |
| 74 |
| 75 - (void)sheetDidEnd:(NSWindow*)parent |
| 76 returnCode:(NSInteger)returnCode |
| 77 context:(void*)context { |
| 78 if (!closePending_) |
| 79 constrainedWindow_->CloseWebContentsModalDialog(); |
| 80 } |
| 81 |
| 82 // ConstrainedWindowSheet protocol implementation. |
| 83 |
| 84 - (void)showSheetForWindow:(NSWindow*)window { |
| 85 overlayWindow_.reset([window retain]); |
| 86 [self showCertificateSheet:window]; |
| 87 } |
| 88 |
| 89 - (void)closeSheetWithAnimation:(BOOL)withAnimation { |
| 90 closePending_ = YES; |
| 91 overlayWindow_.reset(); |
| 92 [self closeCertificateSheet]; |
| 93 } |
| 94 |
| 95 - (void)hideSheet { |
| 96 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; |
| 97 [sheetWindow setAlphaValue:0.0]; |
| 98 [sheetWindow setIgnoresMouseEvents:YES]; |
| 99 |
| 100 oldResizesSubviews_ = [[sheetWindow contentView] autoresizesSubviews]; |
| 101 [[sheetWindow contentView] setAutoresizesSubviews:NO]; |
| 102 } |
| 103 |
| 104 - (void)unhideSheet { |
| 105 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; |
| 106 [sheetWindow setIgnoresMouseEvents:NO]; |
| 107 |
| 108 [[sheetWindow contentView] setAutoresizesSubviews:oldResizesSubviews_]; |
| 109 [[overlayWindow_ attachedSheet] setAlphaValue:1.0]; |
| 110 } |
| 111 |
| 112 - (void)pulseSheet { |
| 113 // NOOP |
| 114 } |
| 115 |
| 116 - (void)makeSheetKeyAndOrderFront { |
| 117 [[overlayWindow_ attachedSheet] makeKeyAndOrderFront:nil]; |
| 118 } |
| 119 |
| 120 - (void)updateSheetPosition { |
| 121 // NOOP |
| 122 } |
| 123 |
| 124 - (void)resizeWithNewSize:(NSSize)preferredSize { |
| 125 // NOOP |
| 126 } |
| 127 |
| 128 - (NSWindow*)sheetWindow { |
| 129 return [self certificatePanel]; |
| 130 } |
| 131 |
| 132 @end |
| 133 |
| 134 void ShowCertificateViewer(content::WebContents* web_contents, |
| 135 gfx::NativeWindow parent, |
| 136 net::X509Certificate* cert) { |
| 137 // SSLCertificateViewerCocoa will manage its own lifetime and will release |
| 138 // itself when the dialog is closed. |
| 139 // See -[SSLCertificateViewerCocoa onConstrainedWindowClosed]. |
| 140 [[SSLCertificateViewerCocoa alloc] initWithCertificate:cert |
| 141 forWebContents:web_contents]; |
| 142 } |
OLD | NEW |