summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/constrained_web_dialog_delegate_mac.mm
blob: 220299f6aa7e8884d1e398de16523749443517aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h"

#import <Cocoa/Cocoa.h>

#include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialog_sheet.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/geometry/size.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
#include "ui/web_dialogs/web_dialog_ui.h"
#include "ui/web_dialogs/web_dialog_web_contents_delegate.h"

using content::WebContents;
using ui::WebDialogDelegate;
using ui::WebDialogWebContentsDelegate;

namespace {

class ConstrainedWebDialogDelegateMac
    : public ConstrainedWebDialogDelegateBase {
 public:
  ConstrainedWebDialogDelegateMac(
      content::BrowserContext* browser_context,
      WebDialogDelegate* delegate)
      : ConstrainedWebDialogDelegateBase(browser_context, delegate, NULL) {}

  // WebDialogWebContentsDelegate interface.
  void CloseContents(WebContents* source) override {
    window_->CloseWebContentsModalDialog();
  }

  void set_window(ConstrainedWindowMac* window) { window_ = window; }
  ConstrainedWindowMac* window() const { return window_; }

 private:
  // Weak, owned by ConstrainedWebDialogDelegateViewMac.
  ConstrainedWindowMac* window_;

  DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac);
};

}  // namespace

class ConstrainedWebDialogDelegateViewMac :
    public ConstrainedWindowMacDelegate,
    public ConstrainedWebDialogDelegate {

 public:
  ConstrainedWebDialogDelegateViewMac(
      content::BrowserContext* browser_context,
      WebDialogDelegate* delegate,
      content::WebContents* web_contents);
  ~ConstrainedWebDialogDelegateViewMac() override {}

  // ConstrainedWebDialogDelegate interface
  const WebDialogDelegate* GetWebDialogDelegate() const override {
    return impl_->GetWebDialogDelegate();
  }
  WebDialogDelegate* GetWebDialogDelegate() override {
    return impl_->GetWebDialogDelegate();
  }
  void OnDialogCloseFromWebUI() override {
    return impl_->OnDialogCloseFromWebUI();
  }
  void ReleaseWebContentsOnDialogClose() override {
    return impl_->ReleaseWebContentsOnDialogClose();
  }
  gfx::NativeWindow GetNativeDialog() override { return window_; }
  WebContents* GetWebContents() override { return impl_->GetWebContents(); }
  gfx::Size GetMinimumSize() const override {
    NOTIMPLEMENTED();
    return gfx::Size();
  }
  gfx::Size GetMaximumSize() const override {
    NOTIMPLEMENTED();
    return gfx::Size();
  }
  gfx::Size GetPreferredSize() const override {
    NOTIMPLEMENTED();
    return gfx::Size();
  }

  // ConstrainedWindowMacDelegate interface
  void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
    if (!impl_->closed_via_webui())
      GetWebDialogDelegate()->OnDialogClosed("");
    delete this;
  }

 private:
  scoped_ptr<ConstrainedWebDialogDelegateMac> impl_;
  scoped_ptr<ConstrainedWindowMac> constrained_window_;
  base::scoped_nsobject<NSWindow> window_;

  DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac);
};

ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac(
    content::BrowserContext* browser_context,
    WebDialogDelegate* delegate,
    content::WebContents* web_contents)
    : impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate)) {
  // Create a window to hold web_contents in the constrained sheet:
  gfx::Size size;
  delegate->GetDialogSize(&size);
  NSRect frame = NSMakeRect(0, 0, size.width(), size.height());

  window_.reset(
      [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]);
  [GetWebContents()->GetNativeView() setFrame:frame];
  [GetWebContents()->GetNativeView() setAutoresizingMask:
      NSViewWidthSizable|NSViewHeightSizable];
  [[window_ contentView] addSubview:GetWebContents()->GetNativeView()];

  base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet(
      [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_
                                                  webDialogDelegate:delegate]);
  constrained_window_.reset(new ConstrainedWindowMac(
      this, web_contents, sheet));

  impl_->set_window(constrained_window_.get());
}

ConstrainedWebDialogDelegate* ShowConstrainedWebDialog(
        content::BrowserContext* browser_context,
        WebDialogDelegate* delegate,
        content::WebContents* web_contents) {
  // Deleted when the dialog closes.
  ConstrainedWebDialogDelegateViewMac* constrained_delegate =
      new ConstrainedWebDialogDelegateViewMac(
          browser_context, delegate, web_contents);
  return constrained_delegate;
}