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

ash / webui / media_app_ui / media_app_guest_ui.h [blame]

// Copyright 2021 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_MEDIA_APP_UI_MEDIA_APP_GUEST_UI_H_
#define ASH_WEBUI_MEDIA_APP_UI_MEDIA_APP_GUEST_UI_H_

#include <optional>
#include <string>

#include "ash/webui/media_app_ui/media_app_ui_untrusted.mojom.h"
#include "base/files/file_path.h"
#include "base/task/sequenced_task_runner.h"
#include "chromeos/ash/components/mantis/media_app/mantis_untrusted_service_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui_data_source.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/webui/color_change_listener/color_change_handler.h"
#include "ui/webui/resources/cr_components/color_change_listener/color_change_listener.mojom.h"
#include "ui/webui/untrusted_web_ui_controller.h"

namespace ui {
class ColorChangeHandler;
}

namespace ash {

// A delegate used during data source creation to expose some //chrome
// functionality to the data source
class MediaAppGuestUIDelegate {
 public:
  virtual ~MediaAppGuestUIDelegate() = default;
  // Takes a WebUI and WebUIDataSource, and populates its load-time data.
  virtual void PopulateLoadTimeData(content::WebUI* web_ui,
                                    content::WebUIDataSource* source) = 0;
  virtual PrefService* GetPrefService(content::WebUI* web_ui) = 0;
  virtual void CreateAndBindOcrUntrustedService(
      content::BrowserContext& context,
      gfx::NativeWindow native_window,
      mojo::PendingReceiver<ash::media_app_ui::mojom::OcrUntrustedService>
          receiver,
      mojo::PendingRemote<ash::media_app_ui::mojom::OcrUntrustedPage> page) = 0;

  virtual void CreateAndBindMahiUntrustedService(
      mojo::PendingReceiver<ash::media_app_ui::mojom::MahiUntrustedService>
          receiver,
      mojo::PendingRemote<ash::media_app_ui::mojom::MahiUntrustedPage> page,
      const std::string& file_name,
      gfx::NativeWindow window) = 0;
};

// The webui for chrome-untrusted://media-app.
class MediaAppGuestUI : public ui::UntrustedWebUIController,
                        public content::WebContentsObserver,
                        public media_app_ui::mojom::UntrustedServiceFactory {
 public:
  MediaAppGuestUI(content::WebUI* web_ui,
                  std::unique_ptr<MediaAppGuestUIDelegate> delegate);
  MediaAppGuestUI(const MediaAppGuestUI&) = delete;
  MediaAppGuestUI& operator=(const MediaAppGuestUI&) = delete;
  ~MediaAppGuestUI() override;

  // content::WebContentsObserver:
  void ReadyToCommitNavigation(content::NavigationHandle* handle) override;

  // Binds a PageHandler to MediaAppGuestUI. This handler grabs a reference to
  // the page and pushes a colorChangeEvent to the untrusted JS running there
  // when the OS color scheme has changed.
  void BindInterface(
      mojo::PendingReceiver<color_change_listener::mojom::PageHandler>
          receiver);

  // Binds UntrustedServiceFactory which is used to bind other interfaces
  // used to communicate between the untrusted MediaApp frame and the browser.
  void BindInterface(
      mojo::PendingReceiver<media_app_ui::mojom::UntrustedServiceFactory>
          receiver);

 private:
  WEB_UI_CONTROLLER_TYPE_DECL();

  // media_app_ui::mojom::UntrustedServiceFactory:
  void CreateOcrUntrustedService(
      mojo::PendingReceiver<media_app_ui::mojom::OcrUntrustedService> receiver,
      mojo::PendingRemote<media_app_ui::mojom::OcrUntrustedPage> page) override;
  void CreateMahiUntrustedService(
      mojo::PendingReceiver<media_app_ui::mojom::MahiUntrustedService> receiver,
      mojo::PendingRemote<media_app_ui::mojom::MahiUntrustedPage> page,
      const std::string& file_name) override;
  void OnMantisAvailableDone(IsMantisAvailableCallback callback, bool result);
  void IsMantisAvailable(IsMantisAvailableCallback callback) override;
  void CreateMantisUntrustedService(
      mojo::PendingRemote<media_app_ui::mojom::MantisUntrustedPage> page,
      CreateMantisUntrustedServiceCallback callback) override;

  void StartFontDataRequest(
      const std::string& path,
      content::WebUIDataSource::GotDataCallback got_data_callback);
  void StartFontDataRequestAfterPathExists(
      const base::FilePath& font_path,
      content::WebUIDataSource::GotDataCallback got_data_callback,
      bool path_exists);

  // The background task runner on which file I/O is performed.
  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  // Whether ReadyToCommitNavigation has occurred for the main `app.html`.
  bool app_navigation_committed_ = false;

  std::unique_ptr<ui::ColorChangeHandler> color_provider_handler_;
  std::optional<bool> is_mantis_available_;
  std::unique_ptr<MantisUntrustedServiceManager>
      mantis_untrusted_service_manager_;
  mojo::Receiver<media_app_ui::mojom::UntrustedServiceFactory>
      untrusted_service_factory_{this};
  std::unique_ptr<MediaAppGuestUIDelegate> delegate_;

  base::WeakPtrFactory<MediaAppGuestUI> weak_factory_{this};
};

struct MediaAppUserActions {
  bool clicked_edit_image_in_photos;
  bool clicked_edit_video_in_photos;
};
// Returns a snapshot of the user actions that are tracked whilst any MediaApp
// instance is running, in order to populate product-specific survey data.
MediaAppUserActions GetMediaAppUserActionsForHappinessTracking();

}  // namespace ash

#endif  // ASH_WEBUI_MEDIA_APP_UI_MEDIA_APP_GUEST_UI_H_