summaryrefslogtreecommitdiffstats
path: root/remoting/host/continue_window_mac.mm
blob: 8793dd470f28a688b833722d5f057bd0aba37d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/continue_window.h"

#import <Cocoa/Cocoa.h>

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/memory/scoped_nsobject.h"
#include "remoting/host/chromoting_host.h"

// As this is a plugin, there needs to be a way to find its bundle
// so that resources are able to be found. This class exists solely so that
// there is a way to get the bundle that this code file is in using
// [NSBundle bundleForClass:[ContinueWindowMacClassToLocateMyBundle class]]
// It is really only a name.
@interface ContinueWindowMacClassToLocateMyBundle : NSObject
@end

@implementation ContinueWindowMacClassToLocateMyBundle
@end

namespace remoting {

class ContinueWindowMac : public remoting::ContinueWindow {
 public:
  ContinueWindowMac() : modal_session_(NULL) {}
  virtual ~ContinueWindowMac() {}

  virtual void Show(remoting::ChromotingHost* host) OVERRIDE;
  virtual void Hide() OVERRIDE;

 private:
  NSModalSession modal_session_;

  DISALLOW_COPY_AND_ASSIGN(ContinueWindowMac);
};

void ContinueWindowMac::Show(remoting::ChromotingHost* host) {
  base::mac::ScopedNSAutoreleasePool pool;

  // Generate window shade
  NSArray* screens = [NSScreen screens];
  NSMutableArray* windows = [NSMutableArray arrayWithCapacity:[screens count]];
  for (NSScreen *screen in screens) {
    NSWindow* window =
      [[[NSWindow alloc] initWithContentRect:[screen frame]
                                   styleMask:NSBorderlessWindowMask
                                   backing:NSBackingStoreBuffered
                                   defer:NO
                                   screen:screen] autorelease];
    [window setReleasedWhenClosed:NO];
    [window setAlphaValue:0.8];
    [window setOpaque:NO];
    [window setBackgroundColor:[NSColor blackColor]];
    // Raise the window shade above just about everything else.
    // Leave the dock and menu bar exposed so the user has some basic level
    // of control (like they can quit Chromium).
    [window setLevel:NSModalPanelWindowLevel - 1];
    [window orderFront:nil];
    [windows addObject:window];
  }

  // Put up alert
  NSString* message = [NSString stringWithUTF8String:kMessage];
  NSString* continue_button =
      [NSString stringWithUTF8String:kDefaultButtonText];
  NSString* cancel_button =
      [NSString stringWithUTF8String:kCancelButtonText];
  scoped_nsobject<NSAlert> continue_alert([[NSAlert alloc] init]);
  [continue_alert setMessageText:message];
  [continue_alert addButtonWithTitle:continue_button];
  [continue_alert addButtonWithTitle:cancel_button];

  // See ContinueWindowMacClassToLocateMyBundle class above for details
  // on this.
  NSBundle *bundle =
      [NSBundle bundleForClass:[ContinueWindowMacClassToLocateMyBundle class]];
  NSString *imagePath = [bundle pathForResource:@"chromoting128" ofType:@"png"];
  scoped_nsobject<NSImage> image(
      [[NSImage alloc] initByReferencingFile:imagePath]);
  [continue_alert setIcon:image];
  [continue_alert layout];

  NSWindow* continue_window = [continue_alert window];
  [continue_window center];
  [continue_window orderWindow:NSWindowAbove
                    relativeTo:[[windows lastObject] windowNumber]];
  [continue_window makeKeyWindow];
  NSApplication* application = [NSApplication sharedApplication];
  modal_session_ = [application beginModalSessionForWindow:continue_window];
  NSInteger answer = 0;
  do {
    answer = [application runModalSession:modal_session_];
  } while (answer == NSRunContinuesResponse);
  [application endModalSession:modal_session_];
  modal_session_ = NULL;

  [continue_window close];

  // Remove window shade.
  for (NSWindow* window in windows) {
    [window close];
  }

  if (answer == NSAlertFirstButtonReturn) {
    host->PauseSession(false);
  } else {
    host->Shutdown(NULL);
  }
}

void ContinueWindowMac::Hide() {
  if (modal_session_) {
    NSApplication* application = [NSApplication sharedApplication];
    [application stopModalWithCode:NSAlertFirstButtonReturn];
  }
}

ContinueWindow* ContinueWindow::Create() {
  return new ContinueWindowMac();
}

}  // namespace remoting