OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #import "remoting/host/disconnect_window_mac.h" | 7 #import "remoting/host/disconnect_window_mac.h" |
8 | 8 |
9 #include "base/bind.h" | |
9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/location.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/single_thread_task_runner.h" | |
10 #include "base/string_util.h" | 14 #include "base/string_util.h" |
11 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
12 #include "remoting/host/disconnect_window.h" | 16 #include "remoting/host/host_window.h" |
13 #include "remoting/host/ui_strings.h" | 17 #include "remoting/host/ui_strings.h" |
14 | 18 |
15 @interface DisconnectWindowController() | 19 @interface DisconnectWindowController() |
16 - (BOOL)isRToL; | 20 - (BOOL)isRToL; |
17 - (void)Hide; | 21 - (void)Hide; |
18 @end | 22 @end |
19 | 23 |
24 const int kMaximumConnectedNameWidthInPixels = 400; | |
25 | |
20 namespace remoting { | 26 namespace remoting { |
21 | 27 |
22 class DisconnectWindowMac : public remoting::DisconnectWindow { | 28 class DisconnectWindowMac : public HostWindow::Core { |
23 public: | 29 public: |
24 explicit DisconnectWindowMac(const UiStrings* ui_strings); | 30 DisconnectWindowMac( |
31 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
32 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
33 const base::Closure& disconnect_callback, | |
34 const std::string& username, | |
35 const UiStrings& ui_strings); | |
36 | |
37 private: | |
38 friend class base::RefCountedThreadSafe<Core>; | |
25 virtual ~DisconnectWindowMac(); | 39 virtual ~DisconnectWindowMac(); |
26 | 40 |
27 virtual bool Show(const base::Closure& disconnect_callback, | 41 // HostWindow::Core overrides. |
28 const std::string& username) OVERRIDE; | 42 virtual void StartOnUiThread() OVERRIDE; |
29 virtual void Hide() OVERRIDE; | 43 virtual void StopOnUiThread() OVERRIDE; |
30 | 44 |
31 private: | 45 // Posts |disconnect_callback_| to the |caller_task_runner_| thread. |
46 void DisconnectSession(); | |
47 | |
48 // Invoked in the |caller_task_runner_| thread to disconnect the client | |
49 // session. | |
50 base::Closure disconnect_callback_; | |
51 | |
52 // Specifies the remote user name. | |
53 std::string username_; | |
54 | |
32 DisconnectWindowController* window_controller_; | 55 DisconnectWindowController* window_controller_; |
33 | 56 |
34 // Points to the localized strings. | |
35 const UiStrings* ui_strings_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowMac); | 57 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowMac); |
38 }; | 58 }; |
39 | 59 |
40 DisconnectWindowMac::DisconnectWindowMac(const UiStrings* ui_strings) | 60 DisconnectWindowMac::DisconnectWindowMac( |
41 : window_controller_(nil), | 61 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
42 ui_strings_(ui_strings) { | 62 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
63 const base::Closure& disconnect_callback, | |
64 const std::string& username, | |
65 const UiStrings& ui_strings) | |
66 : HostWindow::Core(caller_task_runner, | |
67 ui_task_runner, | |
68 ui_strings), | |
69 disconnect_callback_(disconnect_callback), | |
70 username_(username), | |
71 window_controller_(NULL) { | |
43 } | 72 } |
44 | 73 |
45 DisconnectWindowMac::~DisconnectWindowMac() { | 74 DisconnectWindowMac::~DisconnectWindowMac() { |
46 Hide(); | |
47 } | 75 } |
48 | 76 |
49 bool DisconnectWindowMac::Show(const base::Closure& disconnect_callback, | 77 void DisconnectWindowMac::StartOnUiThread() { |
50 const std::string& username) { | 78 DCHECK(ui_task_runner()->BelongsToCurrentThread()); |
51 DCHECK(!disconnect_callback.is_null()); | |
52 DCHECK(window_controller_ == nil); | 79 DCHECK(window_controller_ == nil); |
53 | 80 |
81 base::Closure disconnect_callback = | |
82 base::Bind(&DisconnectWindowMac::DisconnectSession, this); | |
54 window_controller_ = | 83 window_controller_ = |
55 [[DisconnectWindowController alloc] initWithUiStrings:ui_strings_ | 84 [[DisconnectWindowController alloc] initWithUiStrings:&ui_strings() |
56 callback:disconnect_callback | 85 callback:disconnect_callback |
57 username:username]; | 86 username:username_]; |
58 [window_controller_ showWindow:nil]; | 87 [window_controller_ showWindow:nil]; |
59 return true; | |
60 } | 88 } |
61 | 89 |
62 void DisconnectWindowMac::Hide() { | 90 void DisconnectWindowMac::StopOnUiThread() { |
91 DCHECK(ui_task_runner()->BelongsToCurrentThread()); | |
92 | |
63 // DisconnectWindowController is responsible for releasing itself in its | 93 // DisconnectWindowController is responsible for releasing itself in its |
64 // windowWillClose: method. | 94 // windowWillClose: method. |
65 [window_controller_ Hide]; | 95 [window_controller_ Hide]; |
66 window_controller_ = nil; | 96 window_controller_ = nil; |
67 } | 97 } |
68 | 98 |
69 scoped_ptr<DisconnectWindow> DisconnectWindow::Create( | 99 scoped_ptr<HostWindow> HostWindow::CreateDisconnectWindow( |
70 const UiStrings* ui_strings) { | 100 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
71 return scoped_ptr<DisconnectWindow>(new DisconnectWindowMac(ui_strings)); | 101 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
102 const base::Closure& disconnect_callback, | |
103 const std::string& username, | |
104 const UiStrings& ui_strings) { | |
105 scoped_refptr<Core> core = new DisconnectWindowMac(caller_task_runner, | |
106 ui_task_runner, | |
107 disconnect_callback, | |
108 username, | |
109 ui_strings); | |
110 return scoped_ptr<HostWindow>(new HostWindow(core)); | |
111 } | |
112 | |
113 void DisconnectWindowMac::DisconnectSession() { | |
114 DCHECK(ui_task_runner()->BelongsToCurrentThread()); | |
115 | |
116 caller_task_runner()->PostTask(FROM_HERE, disconnect_callback_); | |
Sergey Ulanov
2013/04/02 19:04:22
This may not be safe. disconnect_callback_ may be
alexeypa (please no reviews)
2013/04/03 20:26:43
This code uses a weak pointer to an interface so i
| |
72 } | 117 } |
73 | 118 |
74 } // namespace remoting | 119 } // namespace remoting |
75 | 120 |
76 @implementation DisconnectWindowController | 121 @implementation DisconnectWindowController |
77 - (id)initWithUiStrings:(const remoting::UiStrings*)ui_strings | 122 - (id)initWithUiStrings:(const remoting::UiStrings*)ui_strings |
78 callback:(const base::Closure&)disconnect_callback | 123 callback:(const base::Closure&)disconnect_callback |
79 username:(const std::string&)username { | 124 username:(const std::string&)username { |
80 self = [super initWithWindowNibName:@"disconnect_window"]; | 125 self = [super initWithWindowNibName:@"disconnect_window"]; |
81 if (self) { | 126 if (self) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 [disconnectButton_ setTitle:base::SysUTF16ToNSString( | 158 [disconnectButton_ setTitle:base::SysUTF16ToNSString( |
114 ui_strings_->disconnect_button_text)]; | 159 ui_strings_->disconnect_button_text)]; |
115 | 160 |
116 // Resize the window dynamically based on the content. | 161 // Resize the window dynamically based on the content. |
117 CGFloat oldConnectedWidth = NSWidth([connectedToField_ bounds]); | 162 CGFloat oldConnectedWidth = NSWidth([connectedToField_ bounds]); |
118 [connectedToField_ sizeToFit]; | 163 [connectedToField_ sizeToFit]; |
119 NSRect connectedToFrame = [connectedToField_ frame]; | 164 NSRect connectedToFrame = [connectedToField_ frame]; |
120 CGFloat newConnectedWidth = NSWidth(connectedToFrame); | 165 CGFloat newConnectedWidth = NSWidth(connectedToFrame); |
121 | 166 |
122 // Set a max width for the connected to text field. | 167 // Set a max width for the connected to text field. |
123 if (newConnectedWidth > | 168 if (newConnectedWidth > kMaximumConnectedNameWidthInPixels) { |
124 remoting::DisconnectWindow::kMaximumConnectedNameWidthInPixels) { | 169 newConnectedWidth = kMaximumConnectedNameWidthInPixels; |
125 newConnectedWidth | |
126 = remoting::DisconnectWindow::kMaximumConnectedNameWidthInPixels; | |
127 connectedToFrame.size.width = newConnectedWidth; | 170 connectedToFrame.size.width = newConnectedWidth; |
128 [connectedToField_ setFrame:connectedToFrame]; | 171 [connectedToField_ setFrame:connectedToFrame]; |
129 } | 172 } |
130 | 173 |
131 CGFloat oldDisconnectWidth = NSWidth([disconnectButton_ bounds]); | 174 CGFloat oldDisconnectWidth = NSWidth([disconnectButton_ bounds]); |
132 [disconnectButton_ sizeToFit]; | 175 [disconnectButton_ sizeToFit]; |
133 NSRect disconnectFrame = [disconnectButton_ frame]; | 176 NSRect disconnectFrame = [disconnectButton_ frame]; |
134 CGFloat newDisconnectWidth = NSWidth(disconnectFrame); | 177 CGFloat newDisconnectWidth = NSWidth(disconnectFrame); |
135 | 178 |
136 // Move the disconnect button appropriately. | 179 // Move the disconnect button appropriately. |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 path = [NSBezierPath bezierPath]; | 327 path = [NSBezierPath bezierPath]; |
285 [path moveToPoint:top]; | 328 [path moveToPoint:top]; |
286 [path lineToPoint:bottom]; | 329 [path lineToPoint:bottom]; |
287 [light setStroke]; | 330 [light setStroke]; |
288 [path stroke]; | 331 [path stroke]; |
289 | 332 |
290 [context setShouldAntialias:alias]; | 333 [context setShouldAntialias:alias]; |
291 } | 334 } |
292 | 335 |
293 @end | 336 @end |
OLD | NEW |