File: glic_media_context.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (107 lines) | stat: -rw-r--r-- 3,207 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/glic/media/glic_media_context.h"

#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "content/public/browser/page.h"
#include "content/public/browser/web_contents.h"
#include "media/mojo/mojom/speech_recognition_result.h"

namespace {
constexpr char kGlicMediaContextName[] = "GlicMediaContext";
}  // namespace

namespace glic {

GlicMediaContext::GlicMediaContext(content::Page* page) : page_(page) {}

GlicMediaContext::~GlicMediaContext() = default;

// static
GlicMediaContext* GlicMediaContext::GetOrCreateFor(
    content::WebContents* web_contents) {
  if (!web_contents || !web_contents->GetPrimaryMainFrame()) {
    return nullptr;
  }

  auto& page = web_contents->GetPrimaryMainFrame()->GetPage();

  if (auto* media_context = static_cast<GlicMediaContext*>(
          page.GetUserData(kGlicMediaContextName))) {
    return media_context;
  }

  auto new_media_context = std::make_unique<GlicMediaContext>(&page);
  auto* media_context = new_media_context.get();
  page.SetUserData(kGlicMediaContextName, std::move(new_media_context));

  return media_context;
}

// static
GlicMediaContext* GlicMediaContext::GetIfExistsFor(
    content::WebContents* web_contents) {
  if (!web_contents || !web_contents->GetPrimaryMainFrame()) {
    return nullptr;
  }

  auto& page = web_contents->GetPrimaryMainFrame()->GetPage();

  return static_cast<GlicMediaContext*>(
      page.GetUserData(kGlicMediaContextName));
}

bool GlicMediaContext::OnResult(const media::SpeechRecognitionResult& result) {
  if (IsExcludedFromTranscript()) {
    return false;
  }

  if (!result.is_final) {
    most_recent_nonfinal_ = result.transcription;
    return true;
  }
  text_context_ += result.transcription;
  most_recent_nonfinal_.clear();

  // Trim to `max_size`.  Note that we should utf8-trim, but this is easier.
  constexpr size_t max_size = 20000;
  if (size_t text_context_size = text_context_.length()) {
    if (text_context_size > max_size) {
      // Remove the beginning of the context, leaving the end.
      text_context_ = text_context_.substr(text_context_size - max_size);
    }
  }

  return true;
}

std::string GlicMediaContext::GetContext() const {
  if (IsExcludedFromTranscript()) {
    return "";
  }
  return text_context_ + most_recent_nonfinal_;
}

void GlicMediaContext::OnPeerConnectionAdded() {
  is_excluded_from_transcript_ = true;
}

bool GlicMediaContext::IsExcludedFromTranscript() const {
  if (is_excluded_from_transcript_) {
    // Skip checking if it's already excluded.
    return true;
  }

  content::WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(&page_->GetMainDocument());
  is_excluded_from_transcript_ |= MediaCaptureDevicesDispatcher::GetInstance()
                                      ->GetMediaStreamCaptureIndicator()
                                      ->IsCapturingUserMedia(web_contents);

  return is_excluded_from_transcript_;
}

}  // namespace glic