File: runtime_application_base.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 (142 lines) | stat: -rw-r--r-- 5,260 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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_BASE_H_
#define COMPONENTS_CAST_RECEIVER_BROWSER_RUNTIME_APPLICATION_BASE_H_

#include <ostream>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "components/cast_receiver/browser/application_client.h"
#include "components/cast_receiver/browser/public/application_config.h"
#include "components/cast_receiver/browser/public/content_window_controls.h"
#include "components/cast_receiver/browser/public/embedder_application.h"
#include "components/cast_receiver/browser/public/runtime_application.h"
#include "components/url_rewrite/mojom/url_request_rewrite.mojom.h"
#include "net/base/net_errors.h"

namespace content {
class WebContents;
}  // namespace content

namespace cast_receiver {

// This class is for sharing code between Web and streaming RuntimeApplication
// implementations, including Load and Launch behavior.
class RuntimeApplicationBase
    : public RuntimeApplication,
      public ContentWindowControls::VisibilityChangeObserver {
 public:
  ~RuntimeApplicationBase() override;

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

  // Sets the |embedder_application| to be used for making calls to platform-
  // specific implementations of cast_receiver interfaces.
  void SetEmbedderApplication(EmbedderApplication& embedder_application);

  // RuntimeApplication implementation.
  //
  // To be implemented by descendants of this class:
  // - Launch(StatusCallback callback)
  // - IsStreamingApplication()
  void Load(StatusCallback callback) override;
  void Stop(StatusCallback callback) override;
  void SetUrlRewriteRules(
      url_rewrite::mojom::UrlRequestRewriteRulesPtr mojom_rules) override;
  void SetMediaBlocking(bool load_blocked, bool start_blocked) override;
  void SetVisibility(bool is_visible) override;
  void SetTouchInputEnabled(bool enabled) override;
  const std::string& GetDisplayName() const override;
  const std::string& GetAppId() const override;
  const std::string& GetCastSessionId() const override;
  bool IsApplicationRunning() const override;

 protected:
  // |application_client| is expected to exist for the lifetime of this
  // instance.
  RuntimeApplicationBase(std::string cast_session_id,
                         ApplicationConfig app_config,
                         ApplicationClient& application_client);

  // Stops the running application. Must be called before destruction of any
  // instance of the implementing object.
  virtual void StopApplication(
      EmbedderApplication::ApplicationStopReason stop_reason,
      net::Error net_error_code);

  scoped_refptr<base::SequencedTaskRunner> task_runner() {
    return task_runner_;
  }

  EmbedderApplication& embedder_application() {
    DCHECK(embedder_application_);
    return *embedder_application_;
  }

  // NOTE: This field is empty until after Load() is called.
  const ApplicationConfig& config() const { return app_config_; }

  ApplicationClient& application_client() { return *application_client_; }

  // Navigated to the page at the given |url| in the associated WebContents.
  void NavigateToPage(const GURL& url);

  // Called by the actual implementation after the Cast application page has
  // been navigated to, following a call to NavigateToPage().
  void OnPageNavigationComplete();

  // Sets the permissions for the provided |web_contents| to that as configured
  // in |app_config_|.
  void SetContentPermissions(content::WebContents& web_contents);

  // Returns the ApplicationControls associated with this application, if such
  // controls exist.
  //
  // TODO(crbug.com/1382907): Change to a callback-based API.
  ApplicationClient::ApplicationControls& GetApplicationControls();

 private:
  void SetWebVisibilityAndPaint(bool is_visible);

  // ContentWindowControls::VisibilityChangeObserver implementation:
  void OnWindowShown() override;
  void OnWindowHidden() override;

  const std::string cast_session_id_;
  const ApplicationConfig app_config_;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  raw_ref<ApplicationClient> application_client_;

  raw_ptr<EmbedderApplication> embedder_application_{nullptr};

  // Cached mojom rules that are set iff |cast_web_view_| is not created before
  // SetUrlRewriteRules is called.
  url_rewrite::mojom::UrlRequestRewriteRulesPtr cached_mojom_rules_{nullptr};

  // Flags whether the application is running or stopped.
  bool is_application_running_ = false;

  // Media-related states of the application.
  bool is_media_load_blocked_ = true;
  bool is_media_start_blocked_ = true;
  bool is_visible_ = false;
  bool is_touch_input_enabled_ = false;

  SEQUENCE_CHECKER(sequence_checker_);
  base::WeakPtrFactory<RuntimeApplicationBase> weak_factory_{this};
};

}  // namespace cast_receiver

#endif  // COMPONENTS_CAST_RECEIVER_BROWSER_RUNTIME_APPLICATION_BASE_H_