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 "chrome/app/chrome_command_ids.h" |
7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
9 #include "chrome/browser/ui/browser_dialogs.h" | 10 #include "chrome/browser/ui/browser_dialogs.h" |
| 11 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
| 12 #include "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
| 13 #include "content/public/browser/native_web_keyboard_event.h" |
10 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/browser/web_contents_delegate.h" |
11 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
12 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
13 | 18 |
14 // Default window size. Taken from the views implementation in | 19 // Default window size. Taken from the views implementation in |
15 // chrome/browser/ui/views/user_manager_view.cc. | 20 // chrome/browser/ui/views/user_manager_view.cc. |
16 // TODO(noms): Figure out if this size can be computed dynamically or adjusted | 21 // TODO(noms): Figure out if this size can be computed dynamically or adjusted |
17 // for smaller screens. | 22 // for smaller screens. |
18 const int kWindowWidth = 900; | 23 const int kWindowWidth = 900; |
19 const int kWindowHeight = 700; | 24 const int kWindowHeight = 700; |
20 | 25 |
21 namespace chrome { | 26 namespace chrome { |
22 | 27 |
23 // Declared in browser_dialogs.h so others don't have to depend on this header. | 28 // Declared in browser_dialogs.h so others don't have to depend on this header. |
24 void ShowUserManager(const base::FilePath& profile_path_to_focus) { | 29 void ShowUserManager(const base::FilePath& profile_path_to_focus) { |
25 UserManagerMac::Show( | 30 UserManagerMac::Show( |
26 profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL); | 31 profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL); |
27 } | 32 } |
28 | 33 |
29 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) { | 34 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) { |
30 UserManagerMac::Show(base::FilePath(), tutorial); | 35 UserManagerMac::Show(base::FilePath(), tutorial); |
31 } | 36 } |
32 | 37 |
33 void HideUserManager() { | 38 void HideUserManager() { |
34 UserManagerMac::Hide(); | 39 UserManagerMac::Hide(); |
35 } | 40 } |
36 | 41 |
37 } // namespace chrome | 42 } // namespace chrome |
38 | 43 |
| 44 // Custom WebContentsDelegate that allows handling of hotkeys. |
| 45 class UserManagerWebContentsDelegate : public content::WebContentsDelegate { |
| 46 public: |
| 47 UserManagerWebContentsDelegate(ChromeEventProcessingWindow* window) |
| 48 : window_(window) {} |
| 49 |
| 50 // WebContentsDelegate implementation. Forwards all unhandled keyboard events |
| 51 // to the current window. |
| 52 virtual void HandleKeyboardEvent( |
| 53 content::WebContents* source, |
| 54 const content::NativeWebKeyboardEvent& event) OVERRIDE { |
| 55 if (![BrowserWindowUtils shouldHandleKeyboardEvent:event]) |
| 56 return; |
| 57 |
| 58 int commandId = [BrowserWindowUtils getCommandId:event]; |
| 59 |
| 60 // Since the User Manager is a "top level" window, only handle close events. |
| 61 if (commandId == IDC_CLOSE_WINDOW || commandId == IDC_EXIT) { |
| 62 // Not invoking +[BrowserWindowUtils handleKeyboardEvent here], since the |
| 63 // window in question is a ConstrainedWindowCustomWindow, not a |
| 64 // BrowserWindow. |
| 65 [window_ redispatchKeyEvent:event.os_event]; |
| 66 } |
| 67 } |
| 68 |
| 69 private: |
| 70 ChromeEventProcessingWindow* window_; // Used to redispatch key events. |
| 71 }; |
| 72 |
39 // Window controller for the User Manager view. | 73 // Window controller for the User Manager view. |
40 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { | 74 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { |
41 @private | 75 @private |
42 scoped_ptr<content::WebContents> webContents_; | 76 scoped_ptr<content::WebContents> webContents_; |
| 77 scoped_ptr<UserManagerWebContentsDelegate> webContentsDelegate_; |
43 UserManagerMac* userManagerObserver_; // Weak. | 78 UserManagerMac* userManagerObserver_; // Weak. |
44 } | 79 } |
45 - (void)windowWillClose:(NSNotification*)notification; | 80 - (void)windowWillClose:(NSNotification*)notification; |
46 - (void)dealloc; | 81 - (void)dealloc; |
47 - (id)initWithProfile:(Profile*)profile | 82 - (id)initWithProfile:(Profile*)profile |
48 withObserver:(UserManagerMac*)userManagerObserver; | 83 withObserver:(UserManagerMac*)userManagerObserver; |
49 - (void)showURL:(const GURL&)url; | 84 - (void)showURL:(const GURL&)url; |
50 - (void)show; | 85 - (void)show; |
51 - (void)close; | 86 - (void)close; |
52 - (BOOL)isVisible; | 87 - (BOOL)isVisible; |
53 @end | 88 @end |
54 | 89 |
55 @implementation UserManagerWindowController | 90 @implementation UserManagerWindowController |
56 | 91 |
57 - (id)initWithProfile:(Profile*)profile | 92 - (id)initWithProfile:(Profile*)profile |
58 withObserver:(UserManagerMac*)userManagerObserver { | 93 withObserver:(UserManagerMac*)userManagerObserver { |
59 | 94 |
60 // Center the window on the primary screen. | 95 // Center the window on the primary screen. |
61 CGFloat screenHeight = | 96 CGFloat screenHeight = |
62 [[[NSScreen screens] objectAtIndex:0] frame].size.height; | 97 [[[NSScreen screens] objectAtIndex:0] frame].size.height; |
63 CGFloat screenWidth = | 98 CGFloat screenWidth = |
64 [[[NSScreen screens] objectAtIndex:0] frame].size.width; | 99 [[[NSScreen screens] objectAtIndex:0] frame].size.width; |
65 | 100 |
66 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2, | 101 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2, |
67 (screenHeight - kWindowHeight) / 2, | 102 (screenHeight - kWindowHeight) / 2, |
68 kWindowWidth, kWindowHeight); | 103 kWindowWidth, kWindowHeight); |
69 NSWindow* window = [[NSWindow alloc] | 104 ChromeEventProcessingWindow* window = [[ChromeEventProcessingWindow alloc] |
70 initWithContentRect:contentRect | 105 initWithContentRect:contentRect |
71 styleMask:NSTitledWindowMask | | 106 styleMask:NSTitledWindowMask | |
72 NSClosableWindowMask | | 107 NSClosableWindowMask | |
73 NSResizableWindowMask | 108 NSResizableWindowMask |
74 backing:NSBackingStoreBuffered | 109 backing:NSBackingStoreBuffered |
75 defer:NO]; | 110 defer:NO]; |
76 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)]; | 111 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)]; |
77 [window setMinSize:NSMakeSize(kWindowWidth, kWindowHeight)]; | 112 [window setMinSize:NSMakeSize(kWindowWidth, kWindowHeight)]; |
78 | 113 |
79 if ((self = [super initWithWindow:window])) { | 114 if ((self = [super initWithWindow:window])) { |
80 userManagerObserver_ = userManagerObserver; | 115 userManagerObserver_ = userManagerObserver; |
81 | 116 |
82 // Initialize the web view. | 117 // Initialize the web view. |
83 webContents_.reset(content::WebContents::Create( | 118 webContents_.reset(content::WebContents::Create( |
84 content::WebContents::CreateParams(profile))); | 119 content::WebContents::CreateParams(profile))); |
85 window.contentView = webContents_->GetNativeView(); | 120 window.contentView = webContents_->GetNativeView(); |
| 121 webContentsDelegate_.reset(new UserManagerWebContentsDelegate(window)); |
| 122 webContents_->SetDelegate(webContentsDelegate_.get()); |
86 DCHECK(window.contentView); | 123 DCHECK(window.contentView); |
87 | 124 |
88 [[NSNotificationCenter defaultCenter] | 125 [[NSNotificationCenter defaultCenter] |
89 addObserver:self | 126 addObserver:self |
90 selector:@selector(windowWillClose:) | 127 selector:@selector(windowWillClose:) |
91 name:NSWindowWillCloseNotification | 128 name:NSWindowWillCloseNotification |
92 object:self.window]; | 129 object:self.window]; |
93 } | 130 } |
94 return self; | 131 return self; |
95 } | 132 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 void UserManagerMac::OnGuestProfileCreated(Profile* guest_profile, | 206 void UserManagerMac::OnGuestProfileCreated(Profile* guest_profile, |
170 const std::string& url) { | 207 const std::string& url) { |
171 instance_ = new UserManagerMac(guest_profile); | 208 instance_ = new UserManagerMac(guest_profile); |
172 [instance_->window_controller_ showURL:GURL(url)]; | 209 [instance_->window_controller_ showURL:GURL(url)]; |
173 } | 210 } |
174 | 211 |
175 void UserManagerMac::WindowWasClosed() { | 212 void UserManagerMac::WindowWasClosed() { |
176 instance_ = NULL; | 213 instance_ = NULL; |
177 delete this; | 214 delete this; |
178 } | 215 } |
OLD | NEW |