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

ash / webui / scanner_feedback_ui / scanner_feedback_page_handler.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_WEBUI_SCANNER_FEEDBACK_UI_SCANNER_FEEDBACK_PAGE_HANDLER_H_
#define ASH_WEBUI_SCANNER_FEEDBACK_UI_SCANNER_FEEDBACK_PAGE_HANDLER_H_

#include "ash/webui/scanner_feedback_ui/mojom/scanner_feedback_ui.mojom.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ref.h"
#include "base/unguessable_token.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"

namespace content {
class BrowserContext;
}

namespace ash {

// Stores and handles all data related to a Scanner feedback prompt.
//
// Used for serving Mojo calls for a given `ScannerFeedbackUntrustedUI`. Serves
// data from feedback info stored on the browser context - see
// scanner_feedback_browser_context_data.h for more details. Use `id()` to get
// the ID used for this page.
class ScannerFeedbackPageHandler
    : public mojom::scanner_feedback_ui::PageHandler {
 public:
  // Callback called from `CloseDialog`.
  // Not guaranteed to be called if, for example, if the user closes the UI
  // through other means such as the Escape key.
  // May be called multiple times if, for example, this callback is a no-op and
  // the button is clicked multiple times, or a user's click races the closing
  // of the dialog.
  using CloseDialogCallback = base::RepeatingClosure;

  explicit ScannerFeedbackPageHandler(content::BrowserContext& browser_context);

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

  ~ScannerFeedbackPageHandler() override;

  // Binds the receiver, unbinding the existing one if necessary.
  void Bind(
      mojo::PendingReceiver<mojom::scanner_feedback_ui::PageHandler> receiver);

  void SetCloseDialogCallback(CloseDialogCallback close_dialog_callback) {
    close_dialog_callback_ = std::move(close_dialog_callback);
  }

  base::UnguessableToken id() const { return id_; }

  // mojom::scanner_feedback_ui::PageHandler:
  void GetFeedbackInfo(GetFeedbackInfoCallback callback) override;
  void CloseDialog() override;

 private:
  const base::UnguessableToken id_;

  // Null on construction. Set in `SetCloseDialogCallback`.
  CloseDialogCallback close_dialog_callback_;

  const raw_ref<content::BrowserContext> browser_context_;

  mojo::Receiver<mojom::scanner_feedback_ui::PageHandler> receiver_{this};
};

}  // namespace ash

#endif  // ASH_WEBUI_SCANNER_FEEDBACK_UI_SCANNER_FEEDBACK_PAGE_HANDLER_H_