File: read_anything_app_model.h

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 (520 lines) | stat: -rw-r--r-- 19,766 bytes parent folder | download | duplicates (5)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_RENDERER_ACCESSIBILITY_READ_ANYTHING_READ_ANYTHING_APP_MODEL_H_
#define CHROME_RENDERER_ACCESSIBILITY_READ_ANYTHING_READ_ANYTHING_APP_MODEL_H_

#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/timer/timer.h"
#include "chrome/common/read_anything/read_anything.mojom.h"
#include "chrome/common/read_anything/read_anything_util.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/accessibility/ax_event_generator.h"
#include "ui/accessibility/ax_node_id_forward.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/accessibility/ax_tree_manager.h"
#include "ui/accessibility/ax_tree_update_forward.h"

namespace ui {
class AXNode;
class AXSelection;
class AXSerializableTree;
}  // namespace ui

// A class that holds state for the ReadAnythingAppController for the Read
// Anything WebUI app.
class ReadAnythingAppModel {
 public:
  // Allows one to observer changes in the model state.
  class ModelObserver : public base::CheckedObserver {
   public:
    virtual void OnTreeAdded(ui::AXTree* tree) = 0;
    virtual void OnTreeRemoved(ui::AXTree* tree) = 0;
  };

  // Enum for logging when we show the empty state.
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  //
  // LINT.IfChange(ReadAnythingEmptyState)
  enum class EmptyState {
    kShown = 0,
    kShownWithSelectionAfter = 1,
    kMaxValue = kShownWithSelectionAfter,
  };
  // LINT.ThenChange(//tools/metrics/histograms/metadata/accessibility/enums.xml:ReadAnythingEmptyState)

  struct AXTreeInfo {
    explicit AXTreeInfo(std::unique_ptr<ui::AXTreeManager> manager);
    AXTreeInfo(const AXTreeInfo&) = delete;
    AXTreeInfo& operator=(const AXTreeInfo&) = delete;
    ~AXTreeInfo();

    // Store AXTrees of web contents in the browser's tab strip as
    // AXTreeManagers.
    std::unique_ptr<ui::AXTreeManager> manager;

    // The UKM source ID of the main frame that sources this AXTree. This is
    // used for metrics collection. Only root AXTrees have this set.
    ukm::SourceId ukm_source_id = ukm::kInvalidSourceId;

    // Used to keep track of how many selections were made for the
    // ukm_source_id. Only recorded during the select-to-distill flow (when the
    // empty state page is shown).
    int num_selections = 0;

    // Whether URL information, namely is_docs, has been set.
    bool is_url_information_set = false;

    // Google Docs are different from regular webpages. We want to distill
    // content from the annotated canvas elements, not the main tree. Only root
    // AXTrees have this set.
    bool is_docs = false;

    // Whether the latest tree is a reload of the previous tree. If false, the
    // latest tree is a new page.
    bool is_reload = false;

    // TODO(41496290): Include any information that is associated with a
    // particular AXTree, namely is_pdf. Right now, this is set every time the
    // active ax tree id changes; instead, it should be set once when a new tree
    // is added.
  };

  // Represents a grouping of AXTreeUpdates received in the same accessibility
  // event.
  using Updates = std::vector<ui::AXTreeUpdate>;

  // Updates need to be grouped by the order in which they were received,
  // rather than just a single vector containing all updates from multiple
  // accessibility events. This is so that Unserialize can be called in
  // batches on the group of Updates received from each call to
  // AccessibilityEventReceived. Otherwise, intermediary updates might
  // cause tree inconsistency issues with the final update.
  using PendingUpdates = std::map<ui::AXTreeID, std::vector<Updates>>;

  static constexpr char kEmptyStateHistogramName[] =
      "Accessibility.ReadAnything.EmptyState";

  ReadAnythingAppModel();
  ReadAnythingAppModel(const ReadAnythingAppModel&) = delete;
  ReadAnythingAppModel& operator=(const ReadAnythingAppModel&) = delete;
  ~ReadAnythingAppModel();

  bool requires_distillation() const { return requires_distillation_; }
  void set_requires_distillation(bool requires_distillation) {
    requires_distillation_ = requires_distillation;
  }

  bool requires_post_process_selection() const {
    return requires_post_process_selection_;
  }
  void set_requires_post_process_selection(
      bool requires_post_process_selection) {
    requires_post_process_selection_ = requires_post_process_selection;
  }

  bool reset_draw_timer() const { return reset_draw_timer_; }
  void set_reset_draw_timer(bool value) { reset_draw_timer_ = value; }

  const ui::AXNodeID& last_expanded_node_id() const {
    return last_expanded_node_id_;
  }
  void set_last_expanded_node_id(const ui::AXNodeID& last_expanded_node_id) {
    last_expanded_node_id_ = last_expanded_node_id;
  }
  void reset_last_expanded_node_id() {
    set_last_expanded_node_id(ui::kInvalidAXNodeID);
  }

  bool redraw_required() const { return redraw_required_; }
  void reset_redraw_required() { redraw_required_ = false; }

  int unprocessed_selections_from_reading_mode() {
    return selections_from_reading_mode_;
  }
  void increment_selections_from_reading_mode() {
    ++selections_from_reading_mode_;
  }
  void decrement_selections_from_reading_mode() {
    --selections_from_reading_mode_;
  }

  const std::string& base_language_code() const { return base_language_code_; }
  void SetBaseLanguageCode(std::string base_language_code);

  const std::vector<std::string>& supported_fonts() const {
    return supported_fonts_;
  }

  const std::string& font_name() const { return font_name_; }
  void set_font_name(std::string font_name) {
    font_name_ = std::move(font_name);
  }

  float font_size() const { return font_size_; }

  bool links_enabled() const { return links_enabled_; }
  void set_links_enabled(bool links_enabled) { links_enabled_ = links_enabled; }

  bool images_enabled() const { return images_enabled_; }
  void set_images_enabled(bool images_enabled) {
    images_enabled_ = images_enabled;
  }

  read_anything::mojom::LetterSpacing letter_spacing() const {
    return letter_spacing_;
  }
  void set_letter_spacing(read_anything::mojom::LetterSpacing letter_spacing) {
    letter_spacing_ = letter_spacing;
  }

  read_anything::mojom::LineSpacing line_spacing() const {
    return line_spacing_;
  }
  void set_line_spacing(read_anything::mojom::LineSpacing line_spacing) {
    line_spacing_ = line_spacing;
  }

  read_anything::mojom::Colors color_theme() const { return color_theme_; }
  void set_color_theme(read_anything::mojom::Colors color_theme) {
    color_theme_ = color_theme;
  }

  // Sometimes iframes can return selection objects that have a valid id but
  // aren't in the tree.
  bool has_selection() const {
    return start_.is_valid() && GetAXNode(start_.id);
  }
  ui::AXNodeID start_node_id() const { return start_.id; }
  ui::AXNodeID end_node_id() const { return end_.id; }
  int start_offset() const { return start_.offset; }
  int end_offset() const { return end_.offset; }

  bool distillation_in_progress() const { return distillation_in_progress_; }
  void set_distillation_in_progress(bool distillation_in_progress) {
    distillation_in_progress_ = distillation_in_progress;
  }

  // The following methods are used for the screen2x data collection pipeline.
  // They all have CHECKs to ensure that the DataCollectionModeForScreen2x
  // feature flag is enabled.
  bool ScreenAIServiceReadyForDataCollection() const;
  void SetScreenAIServiceReadyForDataCollection();
  bool PageFinishedLoadingForDataCollection() const;
  void SetDataCollectionForScreen2xCallback(
      base::OnceCallback<void()> callback);

  bool page_finished_loading() const { return page_finished_loading_; }
  void set_page_finished_loading(bool page_finished_loading) {
    page_finished_loading_ = page_finished_loading;
  }

  bool requires_tree_lang() const { return requires_tree_lang_; }
  void set_requires_tree_lang(bool requires_tree_lang) {
    requires_tree_lang_ = requires_tree_lang;
  }

  bool will_hide() const { return will_hide_; }
  void set_will_hide(bool will_hide) { will_hide_ = will_hide; }

  const std::vector<ui::AXNodeID>& content_node_ids() const {
    return content_node_ids_;
  }

  const std::set<ui::AXNodeID>& display_node_ids() const {
    return display_node_ids_;
  }

  const std::set<ui::AXNodeID>& selection_node_ids() const {
    return selection_node_ids_;
  }

  bool is_empty() const {
    return display_node_ids_.empty() && selection_node_ids_.empty();
  }

  const ui::AXTreeID& active_tree_id() const { return active_tree_id_; }
  void SetActiveTreeId(ui::AXTreeID active_tree_id);
  void SetRootTreeId(ui::AXTreeID root_tree_id);

  ukm::SourceId GetUkmSourceId() const;
  void SetUkmSourceIdForTree(const ui::AXTreeID& tree,
                             ukm::SourceId ukm_source_id);

  int GetNumSelections() const;
  void SetNumSelections(int num_selections);
  void SetTreeInfoUrlInformation(AXTreeInfo& tree_info);
  void SetUrlInformationCallback(base::OnceCallback<void()> callback);
  bool IsDocs() const;
  bool IsReload() const;

  const std::set<ui::AXNodeID>* GetCurrentlyVisibleNodes() const;

  ui::AXNode* GetAXNode(const ui::AXNodeID& ax_node_id) const;

  // Inserts `id` into `non_ignored_ids` if it corresponds to a node that should
  // not be ignored during content distillation. Nodes may be ignored for
  // various reasons, such as being synthetic markers of some type or (some
  // kinds of) interactive elements.
  void InsertIdIfNotIgnored(ui::AXNodeID id,
                            std::set<ui::AXNodeID>& non_ignored_ids);

  bool NodeIsContentNode(ui::AXNodeID ax_node_id) const;

  void OnSettingsRestoredFromPrefs(
      read_anything::mojom::LineSpacing line_spacing,
      read_anything::mojom::LetterSpacing letter_spacing,
      std::string font_name,
      double font_size,
      bool links_enabled,
      bool images_enabled,
      read_anything::mojom::Colors color);

  void OnScroll(bool on_selection, bool from_reading_mode) const;

  void Reset(std::vector<ui::AXNodeID> content_node_ids);

  // Helper functions for the rendering algorithm. Post-process the AXTree and
  // cache values before sending an `updateContent` notification to the Read
  // Anything app.ts.
  //
  // Computes selection nodes from the user's selection. The selection nodes
  // list is only populated when the user's selection contains nodes outside of
  // the display nodes list. By keeping two separate lists of nodes, we can
  // switch back to displaying the default distilled content without recomputing
  // the nodes when the user clears their selection or selects content inside
  // the distilled content.
  bool PostProcessSelection();

  // Computes display nodes from the content nodes. These display nodes will be
  // displayed in the Read Anything app.ts by default.
  void ComputeDisplayNodeIdsForDistilledTree();

  ui::AXSerializableTree* GetActiveTree() const;

  bool ContainsTree(const ui::AXTreeID& tree_id) const;

  bool ContainsActiveTree() const;

  void UnserializePendingUpdates(const ui::AXTreeID& tree_id);

  void ClearPendingUpdates();

  void AccessibilityEventReceived(const ui::AXTreeID& tree_id,
                                  Updates& updates,
                                  std::vector<ui::AXEvent>& events,
                                  bool speech_playing);

  void OnAXTreeDestroyed(const ui::AXTreeID& tree_id);

  const PendingUpdates& pending_updates_for_testing() const {
    return pending_updates_;
  }

  const std::map<ui::AXTreeID, std::unique_ptr<AXTreeInfo>>&
  tree_infos_for_testing() const {
    return tree_infos_;
  }

  void AdjustTextSize(int increment);
  void ResetTextSize();

  // PDF handling.
  bool is_pdf() const { return is_pdf_; }
  void set_is_pdf(bool is_pdf) { is_pdf_ = is_pdf; }

  void AddObserver(ModelObserver* observer);
  void RemoveObserver(ModelObserver* observer);

  // TODO: crbug.com/416483312 - Longer term, reading mode should support
  // distilling from multiple trees, if they have important content.
  // Currently, reading mode only distills from a child tree if the root tree
  // has no distillable content.

  // Signal if reading mode should allow use of child trees for the active tree
  // if the web content's root AXTree has no distillable content.
  void AllowChildTreeForActiveTree(bool use_child_tree);

 private:
  struct SelectionEndpoint {
    enum class Source {
      kAnchor,
      kFocus,
    };

    SelectionEndpoint() = default;
    SelectionEndpoint(const ui::AXSelection& selection, Source source);

    constexpr bool operator==(const SelectionEndpoint&) const = default;

    constexpr bool is_valid() const { return id != ui::kInvalidAXNodeID; }

    ui::AXNodeID id = ui::kInvalidAXNodeID;
    int offset = -1;
  };

  ui::AXSerializableTree* GetTreeFromId(const ui::AXTreeID& tree_id) const;

  void ResetSelection();

  bool ContentNodesOnlyContainHeadings();

  void AddPendingUpdates(const ui::AXTreeID& tree_id, Updates& updates);

  void UnserializeUpdates(Updates& updates, const ui::AXTreeID& tree_id);

  void ProcessNonGeneratedEvents(const std::vector<ui::AXEvent>& events);

  // The tree size arguments are used to determine if distillation of a PDF is
  // necessary.
  void ProcessGeneratedEvents(const ui::AXEventGenerator& event_generator,
                              size_t prev_tree_size,
                              size_t tree_size);

  // Runs the data collection for screen2x pipeline, provided in the form of a
  // callback from the ReadAnythingAppController. This should only be called
  // when the DataCollectionModeForScreen2x feature is enabled.
  void MaybeRunDataCollectionForScreen2xCallback();

  void OnPageLoadTimerTriggered();
  void OnTreeChangeTimerTriggered();

  void SetFontSize(double font_size, int increment = 0);
  void SetUkmSourceId(ukm::SourceId ukm_source_id);

  // State.
  std::map<ui::AXTreeID, std::unique_ptr<AXTreeInfo>> tree_infos_;

  // The AXTreeID of the currently active web contents. For PDFs, this will
  // always be the AXTreeID of the main web contents (not the PDF iframe or its
  // child).
  ui::AXTreeID active_tree_id_ = ui::AXTreeIDUnknown();

  // The AXTreeID of the root tree of the web contents. This will be the same
  // as active_tree_id_ unless root_tree_id_ has no distillable content but has
  // a child tree with distillable content.
  ui::AXTreeID root_tree_id_ = ui::AXTreeIDUnknown();

  // For determining whether the latest tree is a reload or new page.
  std::string previous_tree_url_;
  base::OnceCallback<void()> set_url_information_callback_;

  // PDFs are handled differently than regular webpages. That is because they
  // are stored in a different web contents and the actual PDF text is inside an
  // iframe. In order to get tree information from the PDF web contents, we need
  // to enable accessibility on it first. Then, we will get tree updates from
  // the iframe to send to the distiller.
  // This is the flow:
  //    main web contents -> pdf web contents -> iframe
  // In accessibility terms:
  //    AXTree -(via child tree)-> AXTree -(via child tree)-> AXTree
  // The last AXTree is the one we want to send to the distiller since it
  // contains the PDF text.
  bool is_pdf_ = false;

  // Distillation is slow and happens out-of-process when Screen2x is running.
  // This boolean marks when distillation is in progress to avoid sending
  // new distillation requests during that time.
  bool distillation_in_progress_ = false;

  // A mapping of a tree ID to a queue of pending updates on the active AXTree,
  // which will be unserialized once distillation completes.
  PendingUpdates pending_updates_;

  // The node IDs identified as main by the distiller. These are static text
  // nodes when generated by Screen2x. When generated by the rules-based
  // distiller, these are heading or paragraph subtrees.
  std::vector<ui::AXNodeID> content_node_ids_;

  // This contains all ancestors and descendants of each content node. These
  // nodes will be displayed in the Read Anything app if there is no user
  // selection or if the users selection is contained within these nodes.
  std::set<ui::AXNodeID> display_node_ids_;

  // If the user's selection contains nodes outside of display_node_ids, this
  // contains all nodes between the start and end nodes of the selection.
  std::set<ui::AXNodeID> selection_node_ids_;

  // The current base language code used for fonts or reading aloud.
  std::string base_language_code_ = "en";

  bool redraw_required_ = false;
  ui::AXNodeID last_expanded_node_id_ = ui::kInvalidAXNodeID;

  // Cached set of fonts that support `base_language_code_`, updated whenever
  // that is changed.
  std::vector<std::string> supported_fonts_ =
      GetSupportedFonts(base_language_code_);

  // Theme information.
  std::string font_name_ = supported_fonts_.front();
  float font_size_;
  bool links_enabled_ = true;
  bool images_enabled_ = false;
  read_anything::mojom::LetterSpacing letter_spacing_ =
      read_anything::mojom::LetterSpacing::kDefaultValue;
  read_anything::mojom::LineSpacing line_spacing_ =
      read_anything::mojom::LineSpacing::kDefaultValue;
  read_anything::mojom::Colors color_theme_ =
      read_anything::mojom::Colors::kDefaultValue;

  // Invariant: Either both endpoints are `!is_valid()`, or they are both valid
  // and non-equal.
  SelectionEndpoint start_;
  SelectionEndpoint end_;

  bool requires_distillation_ = false;
  bool reset_draw_timer_ = false;
  bool requires_post_process_selection_ = false;
  int selections_from_reading_mode_ = 0;

  // For screen2x data collection, Chrome is launched from the CLI to open one
  // webpage. We record the result of the distill() call for this entire
  // webpage, so we only make the call once the webpage finished loading and
  // screen ai has loaded.
  bool screen_ai_service_ready_for_data_collection_ = false;
  bool waiting_for_page_load_completion_timer_trigger_ = true;
  bool waiting_for_tree_change_timer_trigger_ = true;
  base::OneShotTimer timer_since_page_load_for_data_collection_;
  base::RetainingOneShotTimer timer_since_tree_changed_for_data_collection_;
  base::OnceCallback<void()> data_collection_for_screen2x_callback_;

  // Whether the webpage has finished loading or not.
  bool page_finished_loading_ = false;

  // If the page language can't be determined by the model, we can check the
  // AX tree to see if it has that information, but the ax tree is created
  // asynchronously from the language determination so we need to keep track of
  // that here.
  bool requires_tree_lang_ = false;

  bool will_hide_ = false;

  std::map<ui::AXTreeID, ukm::SourceId> pending_ukm_sources_;

  // Possible child tree ids that could be used to distill content if the
  // root tree has no distillable content. This will only be used if
  // may_use_child_for_active_tree_ is true.
  std::set<ui::AXTreeID> child_tree_ids_;

  // If reading mode should attempt to use child trees to distill content. This
  // should only be true if the root tree has no distillable content.
  bool may_use_child_for_active_tree_ = false;

  // List of observers of model state changes.
  base::ObserverList<ModelObserver, /*check_empty=*/true> observers_;

  base::WeakPtrFactory<ReadAnythingAppModel> weak_ptr_factory_{this};
};

#endif  // CHROME_RENDERER_ACCESSIBILITY_READ_ANYTHING_READ_ANYTHING_APP_MODEL_H_