File: link_to_text_menu_observer.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 (129 lines) | stat: -rw-r--r-- 5,116 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
// Copyright 2020 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_BROWSER_RENDERER_CONTEXT_MENU_LINK_TO_TEXT_MENU_OBSERVER_H_
#define CHROME_BROWSER_RENDERER_CONTEXT_MENU_LINK_TO_TEXT_MENU_OBSERVER_H_

#include "base/memory/raw_ptr.h"
#include "components/renderer_context_menu/render_view_context_menu_observer.h"
#include "components/shared_highlighting/core/common/shared_highlighting_metrics.h"
#include "content/public/browser/render_frame_host.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/mojom/annotation/annotation.mojom-shared.h"
#include "third_party/blink/public/mojom/link_to_text/link_to_text.mojom.h"
#include "url/gurl.h"

class RenderViewContextMenuProxy;
class ToastController;

// A class that implements the menu item for copying selected text and a link
// to the selected text to the user's clipboard.
class LinkToTextMenuObserver : public RenderViewContextMenuObserver {
 public:
  static std::unique_ptr<LinkToTextMenuObserver> Create(
      RenderViewContextMenuProxy* proxy,
      content::GlobalRenderFrameHostId render_frame_host_id,
      ToastController* toast_controller);

  LinkToTextMenuObserver(const LinkToTextMenuObserver&) = delete;
  LinkToTextMenuObserver& operator=(const LinkToTextMenuObserver&) = delete;
  ~LinkToTextMenuObserver() override;

  // RenderViewContextMenuObserver.
  void InitMenu(const content::ContextMenuParams& params) override;
  bool IsCommandIdSupported(int command_id) override;
  bool IsCommandIdEnabled(int command_id) override;
  void ExecuteCommand(int command_id) override;

  // Used in tests for waiting and receiving generation result.
  static void RegisterGenerationCompleteCallbackForTesting(
      base::OnceCallback<void(const std::string& selector)> cb);

 private:
  friend class MockLinkToTextMenuObserver;

  LinkToTextMenuObserver(RenderViewContextMenuProxy* proxy,
                         content::GlobalRenderFrameHostId render_frame_host_id,
                         ToastController* toast_controller);

  // Requests link generation if needed.
  void RequestLinkGeneration();

  // Make an async request to the renderer to generate the link to text.
  // (virtual so it can be mocked in tests).
  virtual void StartLinkGenerationRequestWithTimeout();

  // Callback after the request to generate the selector has completed. This is
  // called with an empty selector if the generation failed or was called on
  // an invalid selection.
  void OnRequestLinkGenerationCompleted(
      const std::string& selector,
      shared_highlighting::LinkGenerationError error,
      shared_highlighting::LinkGenerationReadyStatus ready_status);

  // Called when "Copy Link to Text" option is selected from a context menu for
  // a selected text.
  void ExecuteCopyLinkToText();

  // Make a request to the renderer to retrieve the selector for an
  // existing highlight.
  // (virtual so it can be mocked in tests).
  virtual void ReshareLink();

  // Callback after the request to retrieve an existing selector
  // is complete.
  void OnGetExistingSelectorsComplete(
      const std::vector<std::string>& selectors);

  // Removes the highlight from the page and updates the URL.
  void RemoveHighlights();

  // Cancels link generation if we are still waiting for it.
  void Timeout();

  // Completes necessary tasks when link to text was not generated or generation
  // was unsuccessful.
  void CompleteWithError(shared_highlighting::LinkGenerationError error);

  // Copies given text to clipboard.
  void CopyTextToClipboard(const std::string& text);

  // Returns |remote_|, for the frame in which the context menu was opened.
  mojo::Remote<blink::mojom::TextFragmentReceiver>& GetRemote();

  mojo::Remote<blink::mojom::TextFragmentReceiver> remote_;
  raw_ptr<RenderViewContextMenuProxy> proxy_;
  raw_ptr<ToastController> const toast_controller_;

  GURL url_;
  GURL raw_url_;
  content::GlobalRenderFrameHostId render_frame_host_id_;

  std::unordered_map<content::GlobalRenderFrameHostId,
                     std::vector<std::string>,
                     content::GlobalRenderFrameHostIdHasher>
      frames_selectors_;

  std::vector<content::GlobalRenderFrameHostId> render_frame_host_ids_;
  std::vector<mojo::Remote<blink::mojom::TextFragmentReceiver>>
      text_fragment_remotes_;
  size_t get_frames_existing_selectors_counter_;

  // True when the context menu was opened with text selected.
  bool open_from_new_selection_ = false;

  std::optional<std::string> generated_link_;

  // True when generation is completed.
  bool is_generation_complete_ = false;

  // Set when the context menu was opened with an annotation (with a value
  // corresponding to the type of annotation). We show different menu items
  // based on the type.
  std::optional<blink::mojom::AnnotationType> annotation_type_;

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

#endif  // CHROME_BROWSER_RENDERER_CONTEXT_MENU_LINK_TO_TEXT_MENU_OBSERVER_H_