File: unscoped_extension_provider_delegate_impl.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (120 lines) | stat: -rw-r--r-- 4,852 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
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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.

#ifndef CHROME_BROWSER_AUTOCOMPLETE_UNSCOPED_EXTENSION_PROVIDER_DELEGATE_IMPL_H_
#define CHROME_BROWSER_AUTOCOMPLETE_UNSCOPED_EXTENSION_PROVIDER_DELEGATE_IMPL_H_

#include <string>
#include <unordered_map>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/omnibox_input_watcher.h"
#include "components/omnibox/browser/unscoped_extension_provider.h"
#include "components/omnibox/browser/unscoped_extension_provider_delegate.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_id.h"

#if !BUILDFLAG(ENABLE_EXTENSIONS)
#error "Should not be included when extensions are disabled"
#endif

namespace omnibox_api = extensions::api::omnibox;

class UnscopedExtensionProvider;
class Profile;

// Delegate observes extension events as well as input changes in the omnibox.
// This class is considered the intermediary between the provider (in
// components/) and the Omnibox API (in chrome/browser).
class UnscopedExtensionProviderDelegateImpl
    : public UnscopedExtensionProviderDelegate,
      public OmniboxInputWatcher::Observer,
      public OmniboxSuggestionsWatcher::Observer {
 public:
  UnscopedExtensionProviderDelegateImpl(Profile* profile,
                                        UnscopedExtensionProvider* provider);
  UnscopedExtensionProviderDelegateImpl(
      const UnscopedExtensionProviderDelegateImpl&) = delete;
  UnscopedExtensionProviderDelegateImpl& operator=(
      const UnscopedExtensionProviderDelegateImpl&) = delete;
  ~UnscopedExtensionProviderDelegateImpl() override;

  // UnscopedExtensionProviderDelegate:
  void Start(const AutocompleteInput& input,
             bool minimal_changes,
             std::set<std::string> unscoped_mode_extension_ids) override;
  void Stop(bool clear_cached_results) override;
  void DeleteSuggestion(const TemplateURL* template_url,
                        const std::u16string& suggestion_text) override;

  // OmniboxInputWatcher::Observer:
  void OnOmniboxInputEntered() override;
  // OmniboxSuggestionsWatcher::Observer:
  void OnOmniboxSuggestionsReady(
      const std::vector<ExtensionSuggestion>& suggestions,
      const int request_id,
      const std::string& extension_id) override;

 private:
  // Creates an `AutocompleteMatch` for the suggestion.
  AutocompleteMatch CreateAutocompleteMatch(
      const ExtensionSuggestion& suggestion,
      int relevance,
      const std::string& extension_id);

  // Returns true if an extension is enabled.
  bool IsEnabledExtension(const std::string& extension_id);

  // Clears the current list of cached matches and suggestion group information.
  void ClearSuggestions();

  void OnActionExecuted(const std::string& extension_id,
                        const std::string& action_name,
                        const std::string& contents);

  // Incremented each time a new request for suggestions is sent to extensions
  // or when the input is accepted. Used to discard any suggestions that may be
  // incoming later with a stale request ID.
  int current_request_id_ = 0;

  // The first relevance score to assign to the suggestions for the current
  // request for suggestions.
  int first_suggestion_relevance_ = 0;

  // Current list of matches received from the extensions. Used to update the
  // list of matches in the provider.
  std::vector<AutocompleteMatch> extension_suggest_matches_;

  // Next group available to be given to a set of extension suggestions.
  // Possible groups are defined in `kReservedGroupIdMap`.
  size_t next_available_group_index_ = 0;
  // Next section available to be given to a set of extension suggestions.
  // Possible sections are defined in `kReservedSectionMap`.
  size_t next_available_section_index_ = 0;

  // Maps extension IDs to group IDs. Allows suggestions from different
  // extensions to have distinct headers.
  std::unordered_map<extensions::ExtensionId, omnibox::GroupId>
      extension_id_to_group_id_map_;

  raw_ptr<Profile> profile_;

  // The owner of this class.
  raw_ptr<UnscopedExtensionProvider> provider_;

  base::ScopedObservation<OmniboxInputWatcher, OmniboxInputWatcher::Observer>
      omnibox_input_observation_{this};
  base::ScopedObservation<OmniboxSuggestionsWatcher,
                          OmniboxSuggestionsWatcher::Observer>
      omnibox_suggestions_observation_{this};

  base::WeakPtrFactory<UnscopedExtensionProviderDelegateImpl> weak_factory_{
      this};
};

#endif  // CHROME_BROWSER_AUTOCOMPLETE_UNSCOPED_EXTENSION_PROVIDER_DELEGATE_IMPL_H_