File: feature_manager.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (179 lines) | stat: -rw-r--r-- 5,869 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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.

#include "chromecast/renderer/feature_manager.h"

#include <iostream>
#include <map>
#include <string>
#include <vector>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromecast/base/cast_features.h"
#include "chromecast/common/feature_constants.h"
#include "chromecast/renderer/assistant_bindings.h"
#include "chromecast/renderer/cast_demo_bindings.h"
#include "chromecast/renderer/cast_window_manager_bindings.h"
#include "chromecast/renderer/settings_ui_bindings.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_media_playback_options.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_security_policy.h"

namespace chromecast {

FeatureManager::FeatureManager(content::RenderFrame* render_frame)
    : content::RenderFrameObserver(render_frame),
      configured_(false),
      can_install_bindings_(false),
      dev_origin_(GURL::EmptyGURL()),
      secure_origin_set_(false) {
  registry_.AddInterface(base::BindRepeating(
      &FeatureManager::OnFeatureManagerRequest, base::Unretained(this)));
}

FeatureManager::~FeatureManager() {}

void FeatureManager::OnInterfaceRequestForFrame(
    const std::string& interface_name,
    mojo::ScopedMessagePipeHandle* interface_pipe) {
  registry_.TryBindInterface(interface_name, interface_pipe);
}

void FeatureManager::OnDestruct() {
  delete this;
}

void FeatureManager::DidClearWindowObject() {
  can_install_bindings_ = true;
  if (!configured_)
    return;

  EnableBindings();
}

void FeatureManager::ConfigureFeatures(
    std::vector<chromecast::shell::mojom::FeaturePtr> features) {
  if (configured_)
    return;
  configured_ = true;
  for (auto& feature : features) {
    // If we want to add enabled/disabled status to FeaturePtr, we can overlap
    // previous setting via [] operator
    features_map_[feature->name] = std::move(feature);
  }

  ConfigureFeaturesInternal();

  if (!can_install_bindings_)
    return;
  EnableBindings();
}

void FeatureManager::ConfigureFeaturesInternal() {
  if (FeatureEnabled(feature::kEnableDevMode)) {
    const base::Value::Dict& dev_mode_config =
        (features_map_.find(feature::kEnableDevMode)->second)->config;
    const std::string* dev_mode_origin =
        dev_mode_config.FindString(feature::kDevModeOrigin);
    DCHECK(dev_mode_origin);
    dev_origin_ = GURL(*dev_mode_origin);
    DCHECK(dev_origin_.is_valid());
  }

  if (FeatureEnabled(feature::kDisableBackgroundSuspend)) {
    auto options = render_frame()->GetRenderFrameMediaPlaybackOptions();
    options.is_background_suspend_enabled = false;
    render_frame()->SetRenderFrameMediaPlaybackOptions(options);
  }

  // Call feature-specific functions.
  SetupAdditionalSecureOrigin();

  // Disable timer throttling for background tabs before the frame is painted.
  if (FeatureEnabled(feature::kDisableBackgroundTabTimerThrottle)) {
    blink::WebRuntimeFeatures::EnableTimerThrottlingForBackgroundTabs(false);
  }
  if (FeatureEnabled(feature::kEnableSettingsUiMojo)) {
    v8_bindings_.insert(new shell::SettingsUiBindings(render_frame()));
  }

  // Window manager bindings will install themselves depending on the specific
  // feature flags enabled, so we pass the feature manager through to let it
  // decide.
  v8_bindings_.insert(
      new shell::CastWindowManagerBindings(render_frame(), this));
  if (FeatureEnabled(feature::kEnableDemoStandaloneMode)) {
    v8_bindings_.insert(new shell::CastDemoBindings(render_frame()));
  }

  if (FeatureEnabled(feature::kEnableAssistantMessagePipe)) {
    auto& feature = GetFeature(feature::kEnableAssistantMessagePipe);
    v8_bindings_.insert(
        new shell::AssistantBindings(render_frame(), feature->config));
  }
}

void FeatureManager::EnableBindings() {
  LOG(INFO) << "Enabling bindings: " << *this;
  for (auto* binding : v8_bindings_) {
    binding->TryInstall();
  }
}

void FeatureManager::OnFeatureManagerRequest(
    mojo::PendingReceiver<shell::mojom::FeatureManager> request) {
  bindings_.Add(this, std::move(request));
}

bool FeatureManager::FeatureEnabled(const std::string& feature) const {
  return base::Contains(features_map_, feature);
}

const chromecast::shell::mojom::FeaturePtr& FeatureManager::GetFeature(
    const std::string& feature) const {
  auto itor = features_map_.find(feature);
  DCHECK(itor != features_map_.end());
  return itor->second;
}

void FeatureManager::SetupAdditionalSecureOrigin() {
  if (!dev_origin_.is_valid()) {
    return;
  }

  // Secure origin should be only set once, otherwise it will cause CHECK
  // failure when race between origin safelist changing and thread creation
  // happens (b/63583734).
  if (secure_origin_set_) {
    return;
  }

  secure_origin_set_ = true;

  LOG(INFO) << "Treat origin " << dev_origin_ << " as secure origin";

  blink::WebSecurityPolicy::AddSchemeToSecureContextSafelist(
      blink::WebString::FromASCII(dev_origin_.scheme()));

  network::SecureOriginAllowlist::GetInstance().SetAuxiliaryAllowlist(
      dev_origin_.spec(), nullptr);
}

std::ostream& operator<<(std::ostream& os, const FeatureManager& features) {
  for (auto& feature : features.features_map_) {
    os << feature.first << " ";
  }
  return os;
}

}  // namespace chromecast