File: mahi_web_contents_manager_impl.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 (488 lines) | stat: -rw-r--r-- 17,489 bytes parent folder | download | duplicates (6)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2024 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/ash/mahi/web_contents/mahi_web_contents_manager_impl.h"

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>

#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/unguessable_token.h"
#include "chrome/browser/ash/mahi/web_contents/mahi_content_extraction_delegate.h"
#include "chrome/browser/content_extraction/inner_text.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/components/mahi/public/cpp/mahi_browser_util.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "chromeos/components/mahi/public/cpp/mahi_media_app_content_manager.h"
#include "chromeos/components/mahi/public/cpp/mahi_util.h"
#include "chromeos/components/mahi/public/cpp/mahi_web_contents_manager.h"
#include "chromeos/crosapi/mojom/mahi.mojom-forward.h"
#include "components/pdf/browser/pdf_frame_util.h"
#include "components/pdf/common/constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "pdf/pdf_features.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/ax_updates_and_events.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"

#if DCHECK_IS_ON()
#include "base/functional/callback_helpers.h"
#include "chromeos/constants/chromeos_features.h"
#endif

namespace mahi {

namespace {

using chromeos::mahi::ButtonType;
using chromeos::mahi::GetContentCallback;

// The character count threshold for a distillable page.
static constexpr int kCharCountThreshold = 300;

// Whether the `window` is a media app window.
bool IsMediaAppWindow(const aura::Window* window) {
  if (chromeos::MahiMediaAppContentManager::Get() &&
      chromeos::MahiMediaAppContentManager::Get()->ObservingWindow(window)) {
    return true;
  }

  return false;
}

crosapi::mojom::MahiPageInfoPtr ConvertWebContentStateToPageInfo(
    const WebContentState& web_content_state) {
  // Generates `page_info` from `web_content_state`.
  crosapi::mojom::MahiPageInfoPtr page_info = crosapi::mojom::MahiPageInfo::New(
      /*client_id, deprecated*/ base::UnguessableToken::Create(),
      /*page_id=*/web_content_state.page_id,
      /*url=*/web_content_state.url, /*title=*/web_content_state.title,
      /*favicon_image=*/web_content_state.favicon.DeepCopy(),
      /*is_distillable=*/std::nullopt,
      /*is_incognito=*/web_content_state.is_incognito);
  if (web_content_state.is_distillable.has_value()) {
    page_info->IsDistillable = web_content_state.is_distillable.value();
  }

  return page_info;
}

// Checks if |web_contents| contains a PDF
bool IsPDFWebContents(content::WebContents* web_contents) {
  return web_contents->GetContentsMimeType() == pdf::kPDFMimeType;
}

// Check if |web_contents| is from a incognito profile
bool IsFromIncognito(content::WebContents* web_contents) {
  auto* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  if (!profile) {
    return false;
  }

  return profile->IsIncognitoProfile();
}

// Get the RenderFrameHost that contains the PDF content.
content::RenderFrameHost* GetPDFRenderFrameHost(
    content::WebContents* contents) {
  // Pick the plugin frame host if `contents` is a PDF viewer guest. If using
  // OOPIF PDF viewer, pick the PDF extension frame host.
  content::RenderFrameHost* full_page_pdf_embedder_host =
      chrome_pdf::features::IsOopifPdfEnabled()
          ? pdf_frame_util::FindFullPagePdfExtensionHost(contents)
          : printing::GetFullPagePlugin(contents);
  content::RenderFrameHost* pdf_rfh = pdf_frame_util::FindPdfChildFrame(
      full_page_pdf_embedder_host ? full_page_pdf_embedder_host
                                  : contents->GetPrimaryMainFrame());
  return pdf_rfh;
}

// When the size of the AXTreeUpdate meets this threshold, we consider them
// contain enough content and start extraction without subsequence updates.
constexpr int kAXTreeUpdateByteSizeThreshold = 2000;

// When total observation time for accessibility changes for PDF greater than
// this limit, we stop observing the changes, and processes whatever updates
// received so far.
constexpr base::TimeDelta kPdfObservationTimeLimit = base::Seconds(30);

}  // namespace

MahiPDFObserver::MahiPDFObserver(content::WebContents* web_contents,
                                 ui::AXMode accessibility_mode,
                                 ui::AXTreeID tree_id,
                                 PDFContentObservedCallback callback)
    : tree_id_(tree_id), callback_(std::move(callback)) {
  Observe(web_contents);

  timer_.Start(FROM_HERE, kPdfObservationTimeLimit,
               base::BindOnce(&MahiPDFObserver::OnTimerFired,
                              weak_ptr_factory_.GetWeakPtr()));

  // Enable accessibility for the top level render frame and all descendants.
  // This causes AXTreeSerializer to reset and send accessibility events of
  // the AXTree when it is re-serialized.
  if (!web_contents) {
    return;
  }
  // Force a reset if web accessibility is already enabled to ensure that new
  // observers of accessibility events get the full accessibility tree from
  // scratch.
  const bool need_reset =
      web_contents->GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);

  scoped_accessibility_mode_ =
      content::BrowserAccessibilityState::GetInstance()
          ->CreateScopedModeForWebContents(web_contents, accessibility_mode);

  if (need_reset) {
    web_contents->ResetAccessibility();
  }
}

MahiPDFObserver::~MahiPDFObserver() = default;

void MahiPDFObserver::AccessibilityEventReceived(
    const ui::AXUpdatesAndEvents& details) {
  if (details.ax_tree_id != tree_id_ || !callback_) {
    return;
  }

  for (const auto& update : details.updates) {
    updates_.push_back(update);
    if (update.ByteSize() >= kAXTreeUpdateByteSizeThreshold) {
      std::move(callback_).Run(updates_);
      return;
    }
  }
}

void MahiPDFObserver::OnTimerFired() {
  if (!callback_) {
    return;
  }
  std::move(callback_).Run(updates_);
}

MahiWebContentsManagerImpl::MahiWebContentsManagerImpl() = default;

MahiWebContentsManagerImpl::~MahiWebContentsManagerImpl() {
  focused_web_contents_ = nullptr;
}

void MahiWebContentsManagerImpl::OnFocusedPageLoadComplete(
    content::WebContents* web_contents) {
  auto* mahi_manager = chromeos::MahiManager::Get();
  if (!mahi_manager) {
    return;
  }

  // Do not notify mahi manager if the web_content is from a media app window,
  // to avoid overriding media app focus status.
  if (IsMediaAppWindow(web_contents->GetTopLevelNativeWindow())) {
    return;
  }

  if (ShouldSkip(web_contents)) {
    ClearFocusedWebContentState();
    return;
  }

  base::Time start_time = base::Time::Now();

  focused_web_contents_ = web_contents;
  focused_web_content_state_ =
      WebContentState(focused_web_contents_->GetLastCommittedURL(),
                      focused_web_contents_->GetTitle());
  focused_web_content_state_.favicon = GetFavicon(focused_web_contents_);
  focused_web_content_state_.is_incognito =
      IsFromIncognito(focused_web_contents_);

  crosapi::mojom::MahiPageInfoPtr page_info =
      ConvertWebContentStateToPageInfo(focused_web_content_state_);

  // Skip the distillable check for PDF content.
  if (IsPDFWebContents(web_contents)) {
    is_pdf_focused_web_contents_ = true;
    focused_web_content_state_.is_distillable.emplace(true);
    page_info->IsDistillable = true;
    mahi_manager->SetCurrentFocusedPageInfo(std::move(page_info));
    return;
  }

  is_pdf_focused_web_contents_ = false;
  // Notifies `MahiManager` the focused page has changed.
  mahi_manager->SetCurrentFocusedPageInfo(std::move(page_info));

  auto* rfh = web_contents->GetPrimaryMainFrame();
  if (!rfh || !rfh->IsRenderFrameLive()) {
    return;
  }

  content_extraction::GetInnerText(
      *rfh, /*node_id=*/std::nullopt,
      base::BindOnce(&MahiWebContentsManagerImpl::OnGetInnerText,
                     weak_pointer_factory_.GetWeakPtr(),
                     focused_web_content_state_.page_id, start_time));
}

void MahiWebContentsManagerImpl::ClearFocusedWebContentState() {
  selected_text_ = std::u16string();
  focused_web_contents_ = nullptr;
  is_pdf_focused_web_contents_ = false;
  focused_web_content_state_ = WebContentState(/*url=*/GURL(), /*title=*/u"");
  if (!chromeos::MahiManager::Get()) {
    return;
  }

  // Notifies `MahiManager` the focused page has changed.
  chromeos::MahiManager::Get()->SetCurrentFocusedPageInfo(
      ConvertWebContentStateToPageInfo(focused_web_content_state_));
}

void MahiWebContentsManagerImpl::WebContentsDestroyed(
    content::WebContents* web_contents) {
  if (IsMediaAppWindow(web_contents->GetTopLevelNativeWindow())) {
    return;
  }
  if (focused_web_contents_ == web_contents) {
    ClearFocusedWebContentState();
  }
}

void MahiWebContentsManagerImpl::OnContextMenuClicked(
    int64_t display_id,
    ButtonType button_type,
    std::u16string_view question,
    const gfx::Rect& mahi_menu_bounds) {
  // Records the `button_type` has been clicked.
  base::UmaHistogramEnumeration(chromeos::mahi::kMahiContextMenuActivated,
                                button_type);

  if (!chromeos::MahiManager::Get()) {
    base::UmaHistogramEnumeration(
        chromeos::mahi::kMahiContextMenuActivatedFailed, button_type);
    return;
  }

  // Generates the context menu request.
  crosapi::mojom::MahiContextMenuRequestPtr context_menu_request =
      crosapi::mojom::MahiContextMenuRequest::New(
          /*display_id=*/display_id,
          /*action_type=*/
          chromeos::mahi::MatchButtonTypeToActionType(button_type),
          /*question=*/std::nullopt,
          /*mahi_menu_bounds=*/mahi_menu_bounds);
  if (button_type == chromeos::mahi::ButtonType::kQA) {
    context_menu_request->question = std::u16string(question);
  }
  if (button_type == chromeos::mahi::ButtonType::kElucidation ||
      button_type == chromeos::mahi::ButtonType::kSummaryOfSelection) {
    CHECK(!selected_text_.empty());
  }

  // Forwards the UI request to `MahiManager`.
  chromeos::MahiManager::Get()->OnContextMenuClicked(
      std::move(context_menu_request));
}

bool MahiWebContentsManagerImpl::IsFocusedPageDistillable() {
  if (!focused_web_content_state_.is_distillable.has_value()) {
    return false;
  }
  return focused_web_content_state_.is_distillable.value();
}

void MahiWebContentsManagerImpl::OnGetInnerText(
    const base::UnguessableToken& page_id,
    const base::Time& start_time,
    std::unique_ptr<content_extraction::InnerTextResult> result) {
  if (focused_web_content_state_.page_id != page_id || !focused_web_contents_) {
    // TODO(b:336438243): Add UMA to track this.
    return;
  }
  if (!chromeos::MahiManager::Get()) {
    return;
  }

  base::UmaHistogramMicrosecondsTimes(
      chromeos::mahi::kMahiContentExtractionTriggeringLatency,
      base::Time::Now() - start_time);
  focused_web_content_state_.url = focused_web_contents_->GetLastCommittedURL();
  focused_web_content_state_.title = focused_web_contents_->GetTitle();
  focused_web_content_state_.favicon = GetFavicon(focused_web_contents_);
  bool distillable =
      result ? result->inner_text.length() > kCharCountThreshold : false;
  focused_web_content_state_.is_distillable.emplace(distillable);
  // Notifies `MahiManager` the focused page has changed.
  chromeos::MahiManager::Get()->SetCurrentFocusedPageInfo(
      ConvertWebContentStateToPageInfo(focused_web_content_state_));
}

void MahiWebContentsManagerImpl::OnGetSnapshot(
    const base::UnguessableToken& page_id,
    content::WebContents* web_contents,
    const base::Time& start_time,
    GetContentCallback callback,
    ui::AXTreeUpdate& snapshot) {
  if (focused_web_content_state_.page_id != page_id) {
    // TODO(b:336438243): Add UMA to track this.
    std::move(callback).Run(nullptr);
    return;
  }
  focused_web_content_state_.snapshot = snapshot;
  content_extraction_delegate_->ExtractContent(
      focused_web_content_state_,
      /*client_id, deprecated*/ base::UnguessableToken::Create(),
      std::move(callback));
}

void MahiWebContentsManagerImpl::RequestContent(
    const base::UnguessableToken& page_id,
    GetContentCallback callback) {
  if (focused_web_content_state_.page_id != page_id || !focused_web_contents_) {
    // TODO(b:336438243): Add UMA to track this.
    std::move(callback).Run(nullptr);
    return;
  }

  if (!content_extraction_delegate_) {
    content_extraction_delegate_ =
        std::make_unique<MahiContentExtractionDelegate>();
  }

  if (IsPDFWebContents(focused_web_contents_)) {
    RequestPDFContent(page_id, std::move(callback));
  } else {
    RequestWebContent(page_id, std::move(callback));
  }
}

void MahiWebContentsManagerImpl::SetSelectedText(
    const std::u16string& selected_text) {
  selected_text_ = selected_text;
}

std::u16string MahiWebContentsManagerImpl::GetSelectedText() const {
  return selected_text_;
}

void MahiWebContentsManagerImpl::RequestWebContent(
    const base::UnguessableToken& page_id,
    GetContentCallback callback) {
  base::Time start_time = base::Time::Now();
  focused_web_contents_->RequestAXTreeSnapshot(
      base::BindOnce(&MahiWebContentsManagerImpl::OnGetSnapshot,
                     weak_pointer_factory_.GetWeakPtr(),
                     focused_web_content_state_.page_id, focused_web_contents_,
                     start_time, std::move(callback)),
      ui::kAXModeWebContentsOnly,
      /* max_nodes= */ 5000, /* timeout= */ {},
      content::WebContents::AXTreeSnapshotPolicy::kAll);
}

void MahiWebContentsManagerImpl::RequestPDFContent(
    const base::UnguessableToken& page_id,
    GetContentCallback callback) {
  content::RenderFrameHost* rfh_pdf =
      GetPDFRenderFrameHost(focused_web_contents_);
  if (!rfh_pdf) {
    LOG(ERROR) << "Couldn't find RenderFrameHost contains PDF.";
    std::move(callback).Run(nullptr);
    return;
  }

  // If OOPIF PDF is enabled, we need to observe the focused web contents for
  // a11y changes. Otherwise, we need to observe the inner web contents.
  content::WebContents* web_contents_to_observe = focused_web_contents_;
  if (!chrome_pdf::features::IsOopifPdfEnabled()) {
    std::vector<content::WebContents*> inner_contents =
        focused_web_contents_ ? focused_web_contents_->GetInnerWebContents()
                              : std::vector<content::WebContents*>();

    if (inner_contents.size() != 1u) {
      LOG(ERROR) << "Couldn't find inner WebContents contains PDF.";
      std::move(callback).Run(nullptr);
      return;
    }

    web_contents_to_observe = inner_contents[0];
  }

  pdf_observer_ = std::make_unique<MahiPDFObserver>(
      web_contents_to_observe, ui::kAXModeWebContentsOnly,
      rfh_pdf->GetAXTreeID(),
      base::BindOnce(&MahiWebContentsManagerImpl::OnGetAXTreeUpdatesForPDF,
                     weak_pointer_factory_.GetWeakPtr(), std::move(callback)));
}

void MahiWebContentsManagerImpl::OnGetAXTreeUpdatesForPDF(
    GetContentCallback callback,
    const std::vector<ui::AXTreeUpdate>& updates) {
  content_extraction_delegate_->ExtractContent(
      focused_web_content_state_, std::move(updates),
      /*client_id, deprecated*/ base::UnguessableToken::Create(),
      std::move(callback));

  // No need to observes more a11y changes from PDF content.
  pdf_observer_.reset();
}

gfx::ImageSkia MahiWebContentsManagerImpl::GetFavicon(
    content::WebContents* web_contents) const {
  return favicon::TabFaviconFromWebContents(web_contents).AsImageSkia();
}

bool MahiWebContentsManagerImpl::ShouldSkip(
    content::WebContents* web_contents) {
  const auto url = web_contents->GetURL();

  static constexpr auto kSkipUrls = base::MakeFixedFlatSet<std::string_view>(
      {// blank and default pages.
       "about:blank", "chrome://newtab/",
       // Workspace
       "mail.google.com", "meet.google.com", "calendar.google.com",
       "tasks.google.com", "drive.google.com", "docs.google.com",
       "keep.google.com", "script.google.com", "voice.google.com"});
  // A tab should be skipped if it is empty, or have the domain in the
  // `kSkipUrls` list
  if (url.spec().empty()) {
    return true;
  }
  for (const auto& skip_url : kSkipUrls) {
    if (url.DomainIs(skip_url)) {
      return true;
    }
  }

  // Also skip urls that begins with `chrome` and `view-source`. They are
  // usually web UI and internal pages. E.g., `chrome://`, `chrome-internal://`
  // and `chrome-untrusted://`.
  return (url.spec().rfind("chrome", 0) == 0) ||
         (url.spec().rfind("view-source", 0) == 0);
}

}  // namespace mahi