OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/ui/cocoa/profiles/user_manager_mac.h" | 5 #include "chrome/browser/ui/cocoa/profiles/user_manager_mac.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #import "chrome/browser/app_controller_mac.h" | 9 #import "chrome/browser/app_controller_mac.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 11 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
13 #include "chrome/browser/profiles/profile_metrics.h" | 13 #include "chrome/browser/profiles/profile_metrics.h" |
14 #include "chrome/browser/profiles/profiles_state.h" | 14 #include "chrome/browser/profiles/profiles_state.h" |
15 #include "chrome/browser/signin/signin_promo.h" | |
15 #include "chrome/browser/ui/browser_dialogs.h" | 16 #include "chrome/browser/ui/browser_dialogs.h" |
16 #import "chrome/browser/ui/cocoa/browser_window_utils.h" | 17 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
17 #include "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | 18 #include "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
18 #include "chrome/browser/ui/user_manager.h" | 19 #include "chrome/browser/ui/user_manager.h" |
19 #include "chrome/grit/chromium_strings.h" | 20 #include "chrome/grit/chromium_strings.h" |
20 #include "content/public/browser/native_web_keyboard_event.h" | 21 #include "content/public/browser/native_web_keyboard_event.h" |
21 #include "content/public/browser/render_widget_host_view.h" | 22 #include "content/public/browser/render_widget_host_view.h" |
22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_contents_delegate.h" | 24 #include "content/public/browser/web_contents_delegate.h" |
24 #include "ui/base/l10n/l10n_util_mac.h" | 25 #include "ui/base/l10n/l10n_util_mac.h" |
25 #include "ui/events/keycodes/keyboard_codes.h" | 26 #include "ui/events/keycodes/keyboard_codes.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 | |
groby-ooo-7-16
2015/08/05 21:16:38
Either keep this blank line or remove the one at t
anthonyvd
2015/08/06 19:08:04
Done.
| |
29 // Update the App Controller with a new Profile. Used when a Profile is locked | 29 // Update the App Controller with a new Profile. Used when a Profile is locked |
30 // to set the Controller to the Guest profile so the old Profile's bookmarks, | 30 // to set the Controller to the Guest profile so the old Profile's bookmarks, |
31 // etc... cannot be accessed. | 31 // etc... cannot be accessed. |
32 void ChangeAppControllerForProfile(Profile* profile, | 32 void ChangeAppControllerForProfile(Profile* profile, |
33 Profile::CreateStatus status) { | 33 Profile::CreateStatus status) { |
34 if (status == Profile::CREATE_STATUS_INITIALIZED) { | 34 if (status == Profile::CREATE_STATUS_INITIALIZED) { |
35 AppController* controller = | 35 AppController* controller = |
36 base::mac::ObjCCast<AppController>([NSApp delegate]); | 36 base::mac::ObjCCast<AppController>([NSApp delegate]); |
37 [controller windowChangedToProfile:profile]; | 37 [controller windowChangedToProfile:profile]; |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 // An open User Manager window. There can only be one open at a time. This | 43 // An open User Manager window. There can only be one open at a time. This |
44 // is reset to NULL when the window is closed. | 44 // is reset to NULL when the window is closed. |
45 UserManagerMac* instance_ = NULL; // Weak. | 45 UserManagerMac* instance_ = NULL; // Weak. |
46 BOOL instance_under_construction_ = NO; | 46 BOOL instance_under_construction_ = NO; |
47 | 47 |
48 void CloseInstanceReauthDialog() { | |
49 DCHECK(instance_); | |
50 instance_->CloseReauthDialog(); | |
51 } | |
52 | |
48 // Custom WebContentsDelegate that allows handling of hotkeys. | 53 // Custom WebContentsDelegate that allows handling of hotkeys. |
49 class UserManagerWebContentsDelegate : public content::WebContentsDelegate { | 54 class UserManagerWebContentsDelegate : public content::WebContentsDelegate { |
50 public: | 55 public: |
51 UserManagerWebContentsDelegate() {} | 56 UserManagerWebContentsDelegate() {} |
52 | 57 |
53 // WebContentsDelegate implementation. Forwards all unhandled keyboard events | 58 // WebContentsDelegate implementation. Forwards all unhandled keyboard events |
54 // to the current window. | 59 // to the current window. |
55 void HandleKeyboardEvent( | 60 void HandleKeyboardEvent( |
56 content::WebContents* source, | 61 content::WebContents* source, |
57 const content::NativeWebKeyboardEvent& event) override { | 62 const content::NativeWebKeyboardEvent& event) override { |
(...skipping 10 matching lines...) Expand all Loading... | |
68 event.windowsKeyCode == ui::VKEY_V); | 73 event.windowsKeyCode == ui::VKEY_V); |
69 | 74 |
70 // Only handle close window Chrome accelerators and text editing ones. | 75 // Only handle close window Chrome accelerators and text editing ones. |
71 if (chromeCommandId == IDC_CLOSE_WINDOW || chromeCommandId == IDC_EXIT || | 76 if (chromeCommandId == IDC_CLOSE_WINDOW || chromeCommandId == IDC_EXIT || |
72 isTextEditingCommand) { | 77 isTextEditingCommand) { |
73 [[NSApp mainMenu] performKeyEquivalent:event.os_event]; | 78 [[NSApp mainMenu] performKeyEquivalent:event.os_event]; |
74 } | 79 } |
75 } | 80 } |
76 }; | 81 }; |
77 | 82 |
83 class ReauthDialogDelegate : public profiles::ReauthDialogObserver, | |
84 public UserManagerWebContentsDelegate { | |
85 public: | |
86 ReauthDialogDelegate(content::WebContents* web_contents, std::string email) | |
87 : profiles::ReauthDialogObserver(web_contents, email) {} | |
88 | |
89 // profiles::ReauthDialogObserver: | |
90 void CloseReauthDialog() override { | |
91 CloseInstanceReauthDialog(); | |
92 } | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(ReauthDialogDelegate); | |
95 }; | |
96 | |
97 @interface ReauthDialogWindowController | |
98 : NSWindowController <NSWindowDelegate> { | |
99 @private | |
100 std::string emailAddress_; | |
101 scoped_ptr<content::WebContents> webContents_; | |
102 scoped_ptr<ReauthDialogDelegate> webContentsDelegate_; | |
103 NSModalSession modalSession_; | |
104 } | |
105 - (void)windowWillClose:(NSNotification*)notification; | |
106 - (id)initWithProfile:(Profile*)profile email:(std::string)email; | |
107 @end | |
108 | |
109 @implementation ReauthDialogWindowController | |
110 - (void)windowWillClose:(NSNotification*)notification { | |
111 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
groby-ooo-7-16
2015/08/05 21:16:38
removeObserver should probably be handled in -deal
anthonyvd
2015/08/06 19:08:04
Done.
| |
112 [NSApp endModalSession:modalSession_]; | |
113 } | |
114 | |
115 - (id)initWithProfile:(Profile*)profile email:(std::string)email { | |
116 emailAddress_ = email; | |
117 // Center the window on the screen that currently has focus. | |
118 NSScreen* mainScreen = [NSScreen mainScreen]; | |
119 CGFloat screenHeight = [mainScreen frame].size.height; | |
120 CGFloat screenWidth = [mainScreen frame].size.width; | |
121 NSRect contentRect = | |
122 NSMakeRect((screenWidth - UserManager::kReauthDialogWidth) / 2, | |
123 (screenHeight - UserManager::kReauthDialogHeight) / 2, | |
124 UserManager::kReauthDialogWidth, | |
125 UserManager::kReauthDialogHeight); | |
groby-ooo-7-16
2015/08/05 21:16:38
You can save yourself quite a few of these lines b
anthonyvd
2015/08/06 19:08:04
Done.
| |
126 ChromeEventProcessingWindow* window = [[ChromeEventProcessingWindow alloc] | |
127 initWithContentRect:contentRect | |
128 styleMask:NSTitledWindowMask | | |
129 NSClosableWindowMask | | |
130 NSResizableWindowMask | |
131 backing:NSBackingStoreBuffered | |
132 defer:NO | |
133 screen:mainScreen]; | |
134 [window setTitle:l10n_util::GetNSString(IDS_PRODUCT_NAME)]; | |
135 [window setMinSize:NSMakeSize(UserManager::kReauthDialogWidth, | |
groby-ooo-7-16
2015/08/05 21:16:38
That's the minimum size for the frame, including w
anthonyvd
2015/08/06 19:08:04
Done.
| |
136 UserManager::kReauthDialogHeight)]; | |
137 | |
138 if ((self = [super initWithWindow:window])) { | |
139 // Initialize the web view. | |
140 webContents_.reset(content::WebContents::Create( | |
141 content::WebContents::CreateParams(profile))); | |
142 window.contentView = webContents_->GetNativeView(); | |
143 webContentsDelegate_.reset( | |
144 new ReauthDialogDelegate(webContents_.get(), emailAddress_)); | |
145 webContents_->SetDelegate(webContentsDelegate_.get()); | |
146 DCHECK(window.contentView); | |
147 | |
148 [[NSNotificationCenter defaultCenter] | |
149 addObserver:self | |
150 selector:@selector(windowWillClose:) | |
151 name:NSWindowWillCloseNotification | |
152 object:self.window]; | |
153 [self show]; | |
154 } | |
155 return self; | |
156 } | |
157 | |
158 - (void)show { | |
159 GURL url = signin::GetReauthURLWithEmail(emailAddress_); | |
160 webContents_->GetController().LoadURL(url, content::Referrer(), | |
161 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
162 std::string()); | |
163 modalSession_ = [NSApp beginModalSessionForWindow:[self window]]; | |
groby-ooo-7-16
2015/08/05 21:16:38
IIRC, we usually run modal things as sheets, not a
anthonyvd
2015/08/06 19:08:04
To be honest it's only because I'm very unfamiliar
groby-ooo-7-16
2015/08/06 19:49:25
First, thank you for willing to make this change,
groby-ooo-7-16
2015/08/06 19:49:25
That seems to indicate a NULL WebContentsDialogMan
anthonyvd
2015/08/06 20:07:19
I thought so too :)
anthonyvd
2015/08/06 20:07:19
I don't have a CL but I'll create one from what yo
| |
164 [NSApp runModalSession:modalSession_]; | |
165 } | |
166 | |
167 - (void)close { | |
168 [[self window] close]; | |
169 } | |
170 @end | |
171 | |
78 // Window controller for the User Manager view. | 172 // Window controller for the User Manager view. |
79 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { | 173 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { |
80 @private | 174 @private |
81 scoped_ptr<content::WebContents> webContents_; | 175 scoped_ptr<content::WebContents> webContents_; |
82 scoped_ptr<UserManagerWebContentsDelegate> webContentsDelegate_; | 176 scoped_ptr<UserManagerWebContentsDelegate> webContentsDelegate_; |
83 UserManagerMac* userManagerObserver_; // Weak. | 177 UserManagerMac* userManagerObserver_; // Weak. |
84 } | 178 } |
85 - (void)windowWillClose:(NSNotification*)notification; | 179 - (void)windowWillClose:(NSNotification*)notification; |
86 - (void)dealloc; | 180 - (void)dealloc; |
87 - (id)initWithProfile:(Profile*)profile | 181 - (id)initWithProfile:(Profile*)profile |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 | 325 |
232 // static | 326 // static |
233 void UserManager::OnUserManagerShown() { | 327 void UserManager::OnUserManagerShown() { |
234 if (instance_) | 328 if (instance_) |
235 instance_->LogTimeToOpen(); | 329 instance_->LogTimeToOpen(); |
236 } | 330 } |
237 | 331 |
238 // static | 332 // static |
239 void UserManager::ShowReauthDialog(content::BrowserContext* browser_context, | 333 void UserManager::ShowReauthDialog(content::BrowserContext* browser_context, |
240 const std::string& email) { | 334 const std::string& email) { |
241 // TODO(rogerta): See equivalent views implementation in user_manager_view.cc. | 335 DCHECK(instance_); |
336 instance_->ShowReauthDialog(browser_context, email); | |
337 } | |
338 | |
339 void UserManagerMac::ShowReauthDialog(content::BrowserContext* browser_context, | |
340 const std::string& email) { | |
341 if (reauthWindow_) { | |
groby-ooo-7-16
2015/08/05 21:16:38
No need to check - send to nil always works.
anthonyvd
2015/08/06 19:08:04
Done.
| |
342 [reauthWindow_ close]; | |
343 } | |
344 | |
345 reauthWindow_.reset( | |
346 [[ReauthDialogWindowController alloc] | |
347 initWithProfile:Profile::FromBrowserContext(browser_context) | |
348 email:email]); | |
349 } | |
350 | |
351 void UserManagerMac::CloseReauthDialog() { | |
352 [reauthWindow_ close]; | |
353 reauthWindow_.reset(); | |
242 } | 354 } |
243 | 355 |
244 UserManagerMac::UserManagerMac(Profile* profile) { | 356 UserManagerMac::UserManagerMac(Profile* profile) { |
245 window_controller_.reset([[UserManagerWindowController alloc] | 357 window_controller_.reset([[UserManagerWindowController alloc] |
246 initWithProfile:profile withObserver:this]); | 358 initWithProfile:profile withObserver:this]); |
247 } | 359 } |
248 | 360 |
249 UserManagerMac::~UserManagerMac() { | 361 UserManagerMac::~UserManagerMac() { |
250 } | 362 } |
251 | 363 |
(...skipping 11 matching lines...) Expand all Loading... | |
263 void UserManagerMac::LogTimeToOpen() { | 375 void UserManagerMac::LogTimeToOpen() { |
264 if (user_manager_started_showing_ == base::Time()) | 376 if (user_manager_started_showing_ == base::Time()) |
265 return; | 377 return; |
266 | 378 |
267 ProfileMetrics::LogTimeToOpenUserManager( | 379 ProfileMetrics::LogTimeToOpenUserManager( |
268 base::Time::Now() - user_manager_started_showing_); | 380 base::Time::Now() - user_manager_started_showing_); |
269 user_manager_started_showing_ = base::Time(); | 381 user_manager_started_showing_ = base::Time(); |
270 } | 382 } |
271 | 383 |
272 void UserManagerMac::WindowWasClosed() { | 384 void UserManagerMac::WindowWasClosed() { |
385 if (reauthWindow_) { | |
386 CloseReauthDialog(); | |
groby-ooo-7-16
2015/08/05 21:16:38
Just call CloseReauthDialog - it's perfectly fine
anthonyvd
2015/08/06 19:08:04
Done.
| |
387 } | |
273 instance_ = NULL; | 388 instance_ = NULL; |
274 delete this; | 389 delete this; |
275 } | 390 } |
OLD | NEW |