Chromium Code Reviews| 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 | |
| 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 web_contents_(web_contents), | |
| 89 email_address_(email) { | |
| 90 Observe(web_contents); | |
|
Roger Tawa OOO till Jul 10th
2015/08/05 13:36:05
Can you put this Observe() call into the ctor of R
anthonyvd
2015/08/05 18:20:44
Done.
| |
| 91 } | |
| 92 | |
| 93 // profiles::ReauthDialogObserver: | |
| 94 void CloseReauthDialog() override { | |
| 95 CloseInstanceReauthDialog(); | |
| 96 } | |
| 97 | |
| 98 private: | |
| 99 content::WebContents* web_contents_; | |
| 100 std::string email_address_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(ReauthDialogDelegate); | |
| 103 }; | |
| 104 | |
| 105 @interface ReauthDialogWindowController | |
| 106 : NSWindowController <NSWindowDelegate> { | |
| 107 @private | |
| 108 std::string emailAddress_; | |
| 109 scoped_ptr<content::WebContents> webContents_; | |
| 110 scoped_ptr<ReauthDialogDelegate> webContentsDelegate_; | |
| 111 NSModalSession modalSession_; | |
| 112 } | |
| 113 - (void)windowWillClose:(NSNotification*)notification; | |
| 114 - (id)initWithProfile:(Profile*)profile email:(std::string)email; | |
| 115 @end | |
| 116 | |
| 117 @implementation ReauthDialogWindowController | |
| 118 - (void)windowWillClose:(NSNotification*)notification { | |
| 119 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 120 [NSApp endModalSession:modalSession_]; | |
| 121 } | |
| 122 | |
| 123 - (id)initWithProfile:(Profile*)profile email:(std::string)email { | |
| 124 emailAddress_ = email; | |
| 125 // Center the window on the screen that currently has focus. | |
| 126 NSScreen* mainScreen = [NSScreen mainScreen]; | |
| 127 CGFloat screenHeight = [mainScreen frame].size.height; | |
| 128 CGFloat screenWidth = [mainScreen frame].size.width; | |
| 129 NSRect contentRect = | |
| 130 NSMakeRect((screenWidth - UserManager::kReauthDialogWidth) / 2, | |
| 131 (screenHeight - UserManager::kReauthDialogHeight) / 2, | |
| 132 UserManager::kReauthDialogWidth, | |
| 133 UserManager::kReauthDialogHeight); | |
| 134 ChromeEventProcessingWindow* window = [[ChromeEventProcessingWindow alloc] | |
| 135 initWithContentRect:contentRect | |
| 136 styleMask:NSTitledWindowMask | | |
| 137 NSClosableWindowMask | | |
| 138 NSResizableWindowMask | |
| 139 backing:NSBackingStoreBuffered | |
| 140 defer:NO | |
| 141 screen:mainScreen]; | |
| 142 [window setTitle:l10n_util::GetNSString(IDS_PRODUCT_NAME)]; | |
| 143 [window setMinSize:NSMakeSize(UserManager::kReauthDialogWidth, | |
| 144 UserManager::kReauthDialogHeight)]; | |
| 145 | |
| 146 if ((self = [super initWithWindow:window])) { | |
| 147 // Initialize the web view. | |
| 148 webContents_.reset(content::WebContents::Create( | |
| 149 content::WebContents::CreateParams(profile))); | |
| 150 window.contentView = webContents_->GetNativeView(); | |
| 151 webContentsDelegate_.reset( | |
| 152 new ReauthDialogDelegate(webContents_.get(), emailAddress_)); | |
| 153 webContents_->SetDelegate(webContentsDelegate_.get()); | |
| 154 DCHECK(window.contentView); | |
| 155 | |
| 156 [[NSNotificationCenter defaultCenter] | |
| 157 addObserver:self | |
| 158 selector:@selector(windowWillClose:) | |
| 159 name:NSWindowWillCloseNotification | |
| 160 object:self.window]; | |
| 161 [self show]; | |
| 162 } | |
| 163 return self; | |
| 164 } | |
| 165 | |
| 166 - (void)show { | |
| 167 GURL url = signin::GetReauthURLWithEmail(emailAddress_); | |
| 168 webContents_->GetController().LoadURL(url, content::Referrer(), | |
| 169 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 170 std::string()); | |
| 171 modalSession_ = [NSApp beginModalSessionForWindow:[self window]]; | |
| 172 [NSApp runModalSession:modalSession_]; | |
| 173 } | |
| 174 | |
| 175 - (void)close { | |
| 176 [[self window] close]; | |
| 177 } | |
| 178 @end | |
| 179 | |
| 78 // Window controller for the User Manager view. | 180 // Window controller for the User Manager view. |
| 79 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { | 181 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> { |
| 80 @private | 182 @private |
| 81 scoped_ptr<content::WebContents> webContents_; | 183 scoped_ptr<content::WebContents> webContents_; |
| 82 scoped_ptr<UserManagerWebContentsDelegate> webContentsDelegate_; | 184 scoped_ptr<UserManagerWebContentsDelegate> webContentsDelegate_; |
| 83 UserManagerMac* userManagerObserver_; // Weak. | 185 UserManagerMac* userManagerObserver_; // Weak. |
| 84 } | 186 } |
| 85 - (void)windowWillClose:(NSNotification*)notification; | 187 - (void)windowWillClose:(NSNotification*)notification; |
| 86 - (void)dealloc; | 188 - (void)dealloc; |
| 87 - (id)initWithProfile:(Profile*)profile | 189 - (id)initWithProfile:(Profile*)profile |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 | 333 |
| 232 // static | 334 // static |
| 233 void UserManager::OnUserManagerShown() { | 335 void UserManager::OnUserManagerShown() { |
| 234 if (instance_) | 336 if (instance_) |
| 235 instance_->LogTimeToOpen(); | 337 instance_->LogTimeToOpen(); |
| 236 } | 338 } |
| 237 | 339 |
| 238 // static | 340 // static |
| 239 void UserManager::ShowReauthDialog(content::BrowserContext* browser_context, | 341 void UserManager::ShowReauthDialog(content::BrowserContext* browser_context, |
| 240 const std::string& email) { | 342 const std::string& email) { |
| 241 // TODO(rogerta): See equivalent views implementation in user_manager_view.cc. | 343 DCHECK(instance_); |
| 344 instance_->ShowReauthDialog(browser_context, email); | |
| 345 } | |
| 346 | |
| 347 void UserManagerMac::ShowReauthDialog(content::BrowserContext* browser_context, | |
| 348 const std::string& email) { | |
| 349 if (reauthWindow_) { | |
| 350 [reauthWindow_ close]; | |
| 351 } | |
| 352 | |
| 353 reauthWindow_.reset( | |
| 354 [[ReauthDialogWindowController alloc] | |
| 355 initWithProfile:Profile::FromBrowserContext(browser_context) | |
| 356 email:email]); | |
| 357 } | |
| 358 | |
| 359 void UserManagerMac::CloseReauthDialog() { | |
| 360 [reauthWindow_ close]; | |
| 361 reauthWindow_.reset(); | |
| 242 } | 362 } |
| 243 | 363 |
| 244 UserManagerMac::UserManagerMac(Profile* profile) { | 364 UserManagerMac::UserManagerMac(Profile* profile) { |
| 245 window_controller_.reset([[UserManagerWindowController alloc] | 365 window_controller_.reset([[UserManagerWindowController alloc] |
| 246 initWithProfile:profile withObserver:this]); | 366 initWithProfile:profile withObserver:this]); |
| 247 } | 367 } |
| 248 | 368 |
| 249 UserManagerMac::~UserManagerMac() { | 369 UserManagerMac::~UserManagerMac() { |
| 250 } | 370 } |
| 251 | 371 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 263 void UserManagerMac::LogTimeToOpen() { | 383 void UserManagerMac::LogTimeToOpen() { |
| 264 if (user_manager_started_showing_ == base::Time()) | 384 if (user_manager_started_showing_ == base::Time()) |
| 265 return; | 385 return; |
| 266 | 386 |
| 267 ProfileMetrics::LogTimeToOpenUserManager( | 387 ProfileMetrics::LogTimeToOpenUserManager( |
| 268 base::Time::Now() - user_manager_started_showing_); | 388 base::Time::Now() - user_manager_started_showing_); |
| 269 user_manager_started_showing_ = base::Time(); | 389 user_manager_started_showing_ = base::Time(); |
| 270 } | 390 } |
| 271 | 391 |
| 272 void UserManagerMac::WindowWasClosed() { | 392 void UserManagerMac::WindowWasClosed() { |
| 393 if (reauthWindow_) { | |
| 394 CloseReauthDialog(); | |
| 395 } | |
| 273 instance_ = NULL; | 396 instance_ = NULL; |
| 274 delete this; | 397 delete this; |
| 275 } | 398 } |
| OLD | NEW |