File: payment_request_display_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,314 bytes parent folder | download | duplicates (8)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_DISPLAY_MANAGER_H_
#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_DISPLAY_MANAGER_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/render_frame_host.h"
#include "url/gurl.h"

namespace payments {

class ContentPaymentRequestDelegate;
class PaymentRequest;

// The callback type for functions that need to signal back to the ServiceWorker
// when a window was/failed to open following an openWindow call. The parameter
// indicates whether the call was successful or not.
using PaymentHandlerOpenWindowCallback =
    base::OnceCallback<void(bool /* success */,
                            int /* render_process_id */,
                            int /* render_frame_id */)>;

// Enum of possible outcomes from a call to
// PaymentRequestDisplayManager::TryShow, used for logging purposes.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(PaymentRequestTryShowOutcome)
enum class PaymentRequestTryShowOutcome {
  kAbleToShow = 0,
  kCannotShowUnknownReason = 1,
  kCannotShowDelegateWasNull = 2,
  kCannotShowExistingPaymentRequestSameTab = 3,
  kCannotShowExistingPaymentRequestDifferentTab = 4,
  kMaxValue = kCannotShowExistingPaymentRequestDifferentTab,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/payment/enums.xml:PaymentRequestTryShowOutcome)

// This KeyedService is responsible for displaying and hiding Payment Request
// UI. It ensures that only one Payment Request is showing per profile.
class PaymentRequestDisplayManager : public KeyedService {
 public:
  class DisplayHandle {
   public:
    DisplayHandle(base::WeakPtr<PaymentRequestDisplayManager> display_manager,
                  base::WeakPtr<ContentPaymentRequestDelegate> delegate);

    DisplayHandle(const DisplayHandle&) = delete;
    DisplayHandle& operator=(const DisplayHandle&) = delete;

    ~DisplayHandle();
    void Show(base::WeakPtr<PaymentRequest> request);
    void Retry();
    // Attempt to display |url| inside the Payment Request dialog and run
    // |callback| after navigation is completed, passing true/false to indicate
    // success/failure.
    void DisplayPaymentHandlerWindow(const GURL& url,
                                     PaymentHandlerOpenWindowCallback callback);

    // Returns true after Show() was called.
    bool was_shown() const { return was_shown_; }

    base::WeakPtr<ContentPaymentRequestDelegate> delegate() {
      return delegate_;
    }

    base::WeakPtr<DisplayHandle> GetWeakPtr();

   private:
    base::WeakPtr<PaymentRequestDisplayManager> display_manager_;
    base::WeakPtr<ContentPaymentRequestDelegate> delegate_;
    bool was_shown_ = false;

    base::WeakPtrFactory<DisplayHandle> weak_ptr_factory_{this};
  };

  PaymentRequestDisplayManager();

  PaymentRequestDisplayManager(const PaymentRequestDisplayManager&) = delete;
  PaymentRequestDisplayManager& operator=(const PaymentRequestDisplayManager&) =
      delete;

  ~PaymentRequestDisplayManager() override;

  // If no PaymentRequest is currently showing, returns a unique_ptr to a
  // display handle that can be used to display the PaymentRequest dialog. The
  // UI is considered open until the handle object is deleted. |callback| is
  // called with true if the window is finished opening successfully, false if
  // opening it failed.
  std::unique_ptr<DisplayHandle> TryShow(
      base::WeakPtr<ContentPaymentRequestDelegate> delegate);
  void ShowPaymentHandlerWindow(const GURL& url,
                                PaymentHandlerOpenWindowCallback callback);

  base::WeakPtr<PaymentRequestDisplayManager> GetWeakPtr();

 private:
  void set_current_handle(base::WeakPtr<DisplayHandle> handle) {
    current_handle_ = handle;
  }

  base::WeakPtr<DisplayHandle> current_handle_;

  base::WeakPtrFactory<PaymentRequestDisplayManager> weak_ptr_factory_{this};
};

}  // namespace payments

#endif  // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_DISPLAY_MANAGER_H_