1 // Copyright 2019 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 #ifndef COMPONENTS_REMOTE_COCOA_APP_SHIM_ALERT_H_
6 #define COMPONENTS_REMOTE_COCOA_APP_SHIM_ALERT_H_
7 
8 #import <Cocoa/Cocoa.h>
9 
10 #include "base/mac/scoped_nsobject.h"
11 #include "components/remote_cocoa/app_shim/remote_cocoa_app_shim_export.h"
12 #include "components/remote_cocoa/common/alert.mojom.h"
13 #include "mojo/public/cpp/bindings/pending_receiver.h"
14 #include "mojo/public/cpp/bindings/receiver.h"
15 #include "ui/gfx/text_elider.h"
16 
17 @class AlertBridgeHelper;
18 
19 namespace remote_cocoa {
20 
21 // Class that displays an NSAlert with associated UI as described by the mojo
22 // AlertBridge interface.
23 class REMOTE_COCOA_APP_SHIM_EXPORT AlertBridge
24     : public remote_cocoa::mojom::AlertBridge {
25  public:
26   // Creates a new alert which controls its own lifetime. It will destroy itself
27   // once its NSAlert goes away.
28   AlertBridge(mojo::PendingReceiver<mojom::AlertBridge> bridge_receiver);
29 
30   // Send the specified disposition via the Show callback, then destroy |this|.
31   void SendResultAndDestroy(mojom::AlertDisposition disposition);
32 
33   // Called by Cocoa to indicate when the NSAlert is visible (and can be
34   // programmatically updated by Accept, Cancel, and Close).
35   void SetAlertHasShown();
36 
37  private:
38   // Private destructor is called only through SendResultAndDestroy.
39   ~AlertBridge() override;
40 
41   // Handle being disconnected (e.g, because the alert was programmatically
42   // dismissed).
43   void OnMojoDisconnect();
44 
45   // remote_cocoa::mojom::Alert:
46   void Show(mojom::AlertBridgeInitParamsPtr params,
47             ShowCallback callback) override;
48 
49   // The NSAlert's owner and delegate.
50   base::scoped_nsobject<AlertBridgeHelper> helper_;
51 
52   // Set once the alert window is showing (needed because showing is done in a
53   // posted task).
54   bool alert_shown_ = false;
55 
56   // The callback to make when the dialog has finished running.
57   ShowCallback callback_;
58 
59   mojo::Receiver<remote_cocoa::mojom::AlertBridge> mojo_receiver_{this};
60   base::WeakPtrFactory<AlertBridge> weak_factory_;
61   DISALLOW_COPY_AND_ASSIGN(AlertBridge);
62 };
63 
64 }  // namespace remote_cocoa
65 
66 #endif  // COMPONENTS_REMOTE_COCOA_APP_SHIM_ALERT_H_
67