1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "chrome/browser/ui/cocoa/profile_signin_confirmation_dialog_cocoa.h"
6
7#include "base/message_loop/message_loop.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_dialogs.h"
10#include "chrome/browser/ui/browser_finder.h"
11#include "chrome/browser/ui/browser_window.h"
12#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
13#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
14#include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
15#include "chrome/browser/ui/tabs/tab_strip_model.h"
16
17namespace {
18
19// static
20void ShowDialog(
21    Browser* browser,
22    content::WebContents* web_contents,
23    Profile* profile,
24    const std::string& username,
25    ui::ProfileSigninConfirmationDelegate* delegate,
26    bool offer_profile_creation) {
27  // The dialog owns itself.
28  new ProfileSigninConfirmationDialogCocoa(browser,
29                                           web_contents,
30                                           profile,
31                                           username,
32                                           delegate,
33                                           offer_profile_creation);
34}
35
36}  // namespace
37
38namespace chrome {
39
40// Declared in browser_dialogs.h
41void ShowProfileSigninConfirmationDialog(
42    Browser* browser,
43    content::WebContents* web_contents,
44    Profile* profile,
45    const std::string& username,
46    ui::ProfileSigninConfirmationDelegate* delegate) {
47  ui::CheckShouldPromptForNewProfile(
48      profile,
49      base::Bind(ShowDialog,
50                 browser, web_contents, profile, username, delegate));
51}
52
53}  // namespace chrome
54
55ProfileSigninConfirmationDialogCocoa::ProfileSigninConfirmationDialogCocoa(
56    Browser* browser,
57    content::WebContents* web_contents,
58    Profile* profile,
59    const std::string& username,
60    ui::ProfileSigninConfirmationDelegate* delegate,
61    bool offer_profile_creation) {
62  // Setup the dialog view controller.
63  const base::Closure& closeDialogCallback =
64      base::Bind(&ProfileSigninConfirmationDialogCocoa::Close,
65                 base::Unretained(this));
66  controller_.reset(
67      [[ProfileSigninConfirmationViewController alloc]
68          initWithBrowser:browser
69                 username:username
70                 delegate:delegate
71      closeDialogCallback:closeDialogCallback
72     offerProfileCreation:offer_profile_creation]);
73
74  // Setup the constrained window that will show the view.
75  base::scoped_nsobject<NSWindow> window([[ConstrainedWindowCustomWindow alloc]
76      initWithContentRect:[[controller_ view] bounds]]);
77  [[window contentView] addSubview:[controller_ view]];
78  base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
79      [[CustomConstrainedWindowSheet alloc] initWithCustomWindow:window]);
80  window_.reset(new ConstrainedWindowMac(this, web_contents, sheet));
81}
82
83ProfileSigninConfirmationDialogCocoa::~ProfileSigninConfirmationDialogCocoa() {
84}
85
86void ProfileSigninConfirmationDialogCocoa::Close() {
87  window_->CloseWebContentsModalDialog();
88}
89
90void ProfileSigninConfirmationDialogCocoa::OnConstrainedWindowClosed(
91    ConstrainedWindowMac* window) {
92  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
93}
94