File: runtime_application_dispatcher_impl.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (127 lines) | stat: -rw-r--r-- 4,738 bytes parent folder | download | duplicates (9)
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
// 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 COMPONENTS_CAST_RECEIVER_BROWSER_RUNTIME_APPLICATION_DISPATCHER_IMPL_H_
#define COMPONENTS_CAST_RECEIVER_BROWSER_RUNTIME_APPLICATION_DISPATCHER_IMPL_H_

#include <memory>

#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/sequence_checker.h"
#include "components/cast_receiver/browser/application_client.h"
#include "components/cast_receiver/browser/public/application_config.h"
#include "components/cast_receiver/browser/public/runtime_application_dispatcher.h"
#include "components/cast_receiver/browser/runtime_application_base.h"
#include "components/cast_receiver/browser/streaming_runtime_application.h"
#include "components/cast_receiver/browser/web_runtime_application.h"
#include "components/cast_streaming/common/public/app_ids.h"

namespace cast_receiver {

template <typename TEmbedderApplication>
class RuntimeApplicationDispatcherImpl
    : public RuntimeApplicationDispatcher<TEmbedderApplication> {
 public:
  // |application_client| is expected to persist for the lifetime of this
  // instance.
  explicit RuntimeApplicationDispatcherImpl(
      ApplicationClient& application_client);
  ~RuntimeApplicationDispatcherImpl() override = default;

  RuntimeApplicationDispatcherImpl(RuntimeApplicationDispatcherImpl& other) =
      delete;
  RuntimeApplicationDispatcherImpl& operator=(
      RuntimeApplicationDispatcherImpl& other) = delete;

 private:
  using EmbedderApplicationFactory = typename RuntimeApplicationDispatcher<
      TEmbedderApplication>::EmbedderApplicationFactory;

  // RuntimeApplicationDispatcher implementation.
  TEmbedderApplication* CreateApplication(
      std::string session_id,
      ApplicationConfig app_config,
      EmbedderApplicationFactory factory) override;
  TEmbedderApplication* GetApplication(const std::string& session_id) override;
  std::unique_ptr<TEmbedderApplication> DestroyApplication(
      const std::string& session_id) override;

  SEQUENCE_CHECKER(sequence_checker_);
  raw_ref<ApplicationClient> const application_client_;

  base::flat_map<std::string, std::unique_ptr<TEmbedderApplication>>
      loaded_apps_;
};

template <typename TEmbedderApplication>
RuntimeApplicationDispatcherImpl<TEmbedderApplication>::
    RuntimeApplicationDispatcherImpl(ApplicationClient& application_client)
    : application_client_(application_client) {}

template <typename TEmbedderApplication>
TEmbedderApplication*
RuntimeApplicationDispatcherImpl<TEmbedderApplication>::CreateApplication(
    std::string session_id,
    ApplicationConfig app_config,
    EmbedderApplicationFactory factory) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::unique_ptr<RuntimeApplicationBase> app;
  if (cast_streaming::IsStreamingReceiverAppId(app_config.app_id)) {
    app = std::make_unique<StreamingRuntimeApplication>(
        session_id, std::move(app_config), *application_client_);
  } else {
    app = std::make_unique<WebRuntimeApplication>(
        session_id, std::move(app_config), *application_client_);
  }

  // TODO(b/232140331): Call this only when foreground app changes.
  application_client_->OnForegroundApplicationChanged(app.get());

  auto* app_ptr = app.get();
  std::unique_ptr<TEmbedderApplication> embedder_app =
      std::move(factory).Run(std::move(app));
  app_ptr->SetEmbedderApplication(*embedder_app);

  auto [iter, success] =
      loaded_apps_.emplace(std::move(session_id), std::move(embedder_app));
  DCHECK(success);
  return iter->second.get();
}

template <typename TEmbedderApplication>
TEmbedderApplication*
RuntimeApplicationDispatcherImpl<TEmbedderApplication>::GetApplication(
    const std::string& session_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto iter = loaded_apps_.find(session_id);
  return iter == loaded_apps_.end() ? nullptr : iter->second.get();
}

template <typename TEmbedderApplication>
std::unique_ptr<TEmbedderApplication>
RuntimeApplicationDispatcherImpl<TEmbedderApplication>::DestroyApplication(
    const std::string& session_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto iter = loaded_apps_.find(session_id);
  if (iter == loaded_apps_.end()) {
    return nullptr;
  }

  auto app = std::move(iter->second);
  loaded_apps_.erase(iter);

  // TODO(b/232140331): Call this only when foreground app changes.
  application_client_->OnForegroundApplicationChanged(nullptr);
  return app;
}

}  // namespace cast_receiver

#endif  // COMPONENTS_CAST_RECEIVER_BROWSER_RUNTIME_APPLICATION_DISPATCHER_IMPL_H_