File: lens_searchbox_handler.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 7,444 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
// 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/ui/webui/searchbox/lens_searchbox_handler.h"

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/webui/searchbox/lens_searchbox_client.h"
#include "chrome/browser/ui/webui/searchbox/searchbox_omnibox_client.h"
#include "components/lens/lens_features.h"
#include "components/lens/proto/server/lens_overlay_response.pb.h"
#include "components/omnibox/browser/autocomplete_match_type.h"
#include "components/omnibox/browser/omnibox_client.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/prefs/pref_service.h"
#include "components/sessions/core/session_id.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
#include "url/gurl.h"

namespace {
// Interface that allows the Lens searchbox to interact with its embedder
// (i.e., LensSearchboxController).
class LensOmniboxClient : public SearchboxOmniboxClient {
 public:
  LensOmniboxClient(Profile* profile,
                    content::WebContents* web_contents,
                    LensSearchboxClient* lens_searchbox_client);
  ~LensOmniboxClient() override;

  // OmniboxClient:
  SessionID GetSessionID() const override;
  const GURL& GetURL() const override;
  metrics::OmniboxEventProto::PageClassification GetPageClassification(
      bool is_prefetch) const override;
  std::optional<lens::proto::LensOverlaySuggestInputs>
  GetLensOverlaySuggestInputs() const override;
  void OnThumbnailRemoved() override;
  void OnAutocompleteAccept(
      const GURL& destination_url,
      TemplateURLRef::PostContent* post_content,
      WindowOpenDisposition disposition,
      ui::PageTransition transition,
      AutocompleteMatchType::Type match_type,
      base::TimeTicks match_selection_timestamp,
      bool destination_url_entered_without_scheme,
      bool destination_url_entered_with_http_scheme,
      const std::u16string& text,
      const AutocompleteMatch& match,
      const AutocompleteMatch& alternative_nav_match) override;

 private:
  raw_ptr<LensSearchboxClient> lens_searchbox_client_;
};
LensOmniboxClient::LensOmniboxClient(Profile* profile,
                                     content::WebContents* web_contents,
                                     LensSearchboxClient* lens_searchbox_client)
    : SearchboxOmniboxClient(profile, web_contents),
      lens_searchbox_client_(lens_searchbox_client) {}

LensOmniboxClient::~LensOmniboxClient() = default;

SessionID LensOmniboxClient::GetSessionID() const {
  return lens_searchbox_client_->GetTabId();
}

const GURL& LensOmniboxClient::GetURL() const {
  return lens_searchbox_client_->GetPageURL();
}

std::optional<lens::proto::LensOverlaySuggestInputs>
LensOmniboxClient::GetLensOverlaySuggestInputs() const {
  return lens_searchbox_client_->GetLensSuggestInputs();
}

void LensOmniboxClient::OnThumbnailRemoved() {
  lens_searchbox_client_->OnThumbnailRemoved();
}

metrics::OmniboxEventProto::PageClassification
LensOmniboxClient::GetPageClassification(bool is_prefetch) const {
  return lens_searchbox_client_->GetPageClassification();
}

void LensOmniboxClient::OnAutocompleteAccept(
    const GURL& destination_url,
    TemplateURLRef::PostContent* post_content,
    WindowOpenDisposition disposition,
    ui::PageTransition transition,
    AutocompleteMatchType::Type match_type,
    base::TimeTicks match_selection_timestamp,
    bool destination_url_entered_without_scheme,
    bool destination_url_entered_with_http_scheme,
    const std::u16string& text,
    const AutocompleteMatch& match,
    const AutocompleteMatch& alternative_nav_match) {
  lens_searchbox_client_->OnSuggestionAccepted(
      destination_url, match.type,
      match.subtypes.contains(omnibox::SUBTYPE_ZERO_PREFIX));
}

}  // namespace

LensSearchboxHandler::LensSearchboxHandler(
    mojo::PendingReceiver<searchbox::mojom::PageHandler> pending_page_handler,
    Profile* profile,
    content::WebContents* web_contents,
    MetricsReporter* metrics_reporter,
    LensSearchboxClient* lens_searchbox_client)
    : SearchboxHandler(std::move(pending_page_handler),
                       profile,
                       web_contents,
                       metrics_reporter),
      lens_searchbox_client_(lens_searchbox_client) {
  owned_controller_ = std::make_unique<OmniboxController>(
      /*view=*/nullptr,
      std::make_unique<LensOmniboxClient>(profile_, web_contents_,
                                          lens_searchbox_client),
      lens::features::GetLensSearchboxAutocompleteTimeout());
  controller_ = owned_controller_.get();

  autocomplete_controller_observation_.Observe(autocomplete_controller());
}

LensSearchboxHandler::~LensSearchboxHandler() = default;

void LensSearchboxHandler::SetPage(
    mojo::PendingRemote<searchbox::mojom::Page> pending_page) {
  SearchboxHandler::SetPage(std::move(pending_page));

  // The client may have text waiting to be sent to the searchbox that it
  // couldn't do earlier since the page binding was not set. So now we let the
  // client know the binding is ready.
  lens_searchbox_client_->OnPageBound();
}

void LensSearchboxHandler::OnFocusChanged(bool focused) {
  SearchboxHandler::OnFocusChanged(focused);
  lens_searchbox_client_->OnFocusChanged(focused);
}

void LensSearchboxHandler::QueryAutocomplete(const std::u16string& input,
                                             bool prevent_inline_autocomplete) {
  lens_searchbox_client_->OnTextModified();

  SearchboxHandler::QueryAutocomplete(input, prevent_inline_autocomplete);
}

void LensSearchboxHandler::SetInputText(const std::string& input_text) {
  page_->SetInputText(input_text);
}

void LensSearchboxHandler::SetThumbnail(const std::string& thumbnail_url,
                                        bool is_deletable) {
  page_->SetThumbnail(thumbnail_url, is_deletable);
}

void LensSearchboxHandler::OnThumbnailRemoved() {
  omnibox_controller()->client()->OnThumbnailRemoved();
}

void LensSearchboxHandler::OnAutocompleteStopTimerTriggered(
    const AutocompleteInput& input) {
  // Only notify the lens controller when autocomplete stop timer is triggered
  // for zero suggest inputs.
  if (input.IsZeroSuggest() && autocomplete_controller()->done()) {
    lens_searchbox_client_->ShowGhostLoaderErrorState();
  }
}

void LensSearchboxHandler::OnResultChanged(AutocompleteController* controller,
                                           bool default_match_changed) {
  SearchboxHandler::OnResultChanged(controller, default_match_changed);
  // Show the ghost loader error state if the result is empty on the last
  // async pass of the autocomplete controller (there will not be anymore
  // updates). controller->done() itself is not a sufficient check since it
  // takes into account kStop update types which occurs when a user unfocuses
  // the searchbox, and the error state should not be shown in this case.
  if (controller->done() &&
      controller->last_update_type() ==
          AutocompleteController::UpdateType::kLastAsyncPass &&
      controller->result().empty()) {
    lens_searchbox_client_->ShowGhostLoaderErrorState();
  }

  if (controller->input().IsZeroSuggest() && !controller->result().empty()) {
    lens_searchbox_client_->OnZeroSuggestShown();
  }
}