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
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162

fuchsia_web / webengine / browser / web_engine_browser_main_parts.h [blame]

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

#ifndef FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_BROWSER_MAIN_PARTS_H_
#define FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_BROWSER_MAIN_PARTS_H_

#include <fuchsia/web/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>

#include <memory>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "build/chromecast_buildflags.h"
#include "content/public/browser/browser_main_parts.h"
#include "fuchsia_web/webengine/browser/context_impl.h"
#include "fuchsia_web/webengine/browser/web_engine_browser_context.h"
#include "fuchsia_web/webengine/web_engine_export.h"
#include "services/network/public/cpp/network_quality_tracker.h"

namespace base {
class FuchsiaIntlProfileWatcher;
}

namespace aura {
class ScreenOzone;
}

namespace content {
class ContentBrowserClient;
}

#if BUILDFLAG(ENABLE_CAST_RECEIVER)
namespace fuchsia_legacymetrics {
class LegacyMetricsClient;
}
#endif

namespace media {
class FuchsiaCdmManager;
}

namespace inspect {
class ComponentInspector;
}

class WebEngineMemoryInspector;

// Implements the fuchsia.web.FrameHost protocol using a ContextImpl with
// incognito browser context.
class FrameHostImpl final : public fuchsia::web::FrameHost {
 public:
  explicit FrameHostImpl(
      inspect::Node inspect_node,
      WebEngineDevToolsController* devtools_controller,
      network::NetworkQualityTracker* network_quality_tracker)
      : context_(
            WebEngineBrowserContext::CreateIncognito(network_quality_tracker),
            std::move(inspect_node),
            devtools_controller) {}
  ~FrameHostImpl() override = default;

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

  // fuchsia.web.FrameHost implementation.
  void CreateFrameWithParams(
      fuchsia::web::CreateFrameParams params,
      fidl::InterfaceRequest<fuchsia::web::Frame> request) override;

  ContextImpl* context_impl_for_test() { return &context_; }

 private:
  ContextImpl context_;
};

class WEB_ENGINE_EXPORT WebEngineBrowserMainParts
    : public content::BrowserMainParts {
 public:
  explicit WebEngineBrowserMainParts(
      content::ContentBrowserClient* browser_client);
  ~WebEngineBrowserMainParts() override;

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

  std::vector<content::BrowserContext*> browser_contexts() const;
  WebEngineDevToolsController* devtools_controller() const {
    return devtools_controller_.get();
  }

  // content::BrowserMainParts overrides.
  void PostEarlyInitialization() override;
  int PreMainMessageLoopRun() override;
  void WillRunMainMessageLoop(
      std::unique_ptr<base::RunLoop>& run_loop) override;
  void PostMainMessageLoopRun() override;

  // Methods used by tests.
  static void SetContextRequestForTest(
      fidl::InterfaceRequest<fuchsia::web::Context> request);

  // Returns the bound ContextImpl instance, or nullptr if there isn't one.
  ContextImpl* context_for_test() const;

  // Returns all FrameHostImpl instances.
  std::vector<FrameHostImpl*> frame_hosts_for_test() const;

 private:
  // Handle fuchsia.web.Context and fuchsia.web.FrameHost connection requests.
  void HandleContextRequest(
      fidl::InterfaceRequest<fuchsia::web::Context> request);
  void HandleFrameHostRequest(
      fidl::InterfaceRequest<fuchsia::web::FrameHost> request);

  // Notified if the system timezone, language, settings change.
  void OnIntlProfileChanged(const fuchsia::intl::Profile& profile);

  // Quits the main loop and gracefully shuts down the instance.
  void BeginGracefulShutdown();

  const raw_ptr<content::ContentBrowserClient> browser_client_;

  std::unique_ptr<aura::ScreenOzone> screen_;

  // Used to publish diagnostics including the active Contexts and FrameHosts.
  std::unique_ptr<inspect::ComponentInspector> component_inspector_;
  std::unique_ptr<WebEngineMemoryInspector> memory_inspector_;

  // Browsing contexts for the connected clients. There is at most one
  // fuchsia.web.Context binding, and any number of fuchsia.web.FrameHost
  // bindings.
  fidl::BindingSet<fuchsia::web::Context, std::unique_ptr<ContextImpl>>
      context_bindings_;
  fidl::BindingSet<fuchsia::web::FrameHost, std::unique_ptr<FrameHostImpl>>
      frame_host_bindings_;

  std::unique_ptr<WebEngineDevToolsController> devtools_controller_;

#if BUILDFLAG(ENABLE_CAST_RECEIVER)
  std::unique_ptr<fuchsia_legacymetrics::LegacyMetricsClient>
      legacy_metrics_client_;
#endif

  std::unique_ptr<media::FuchsiaCdmManager> cdm_manager_;

  // Used to respond to changes to the system's current locale.
  std::unique_ptr<base::FuchsiaIntlProfileWatcher> intl_profile_watcher_;

  // Used to report networking-related Client Hints.
  std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_;
  std::unique_ptr<
      network::NetworkQualityTracker::RTTAndThroughputEstimatesObserver>
      network_quality_observer_;

  base::OnceClosure quit_closure_;
};

#endif  // FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_BROWSER_MAIN_PARTS_H_