File: lens_region_search_controller.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 (303 lines) | stat: -rw-r--r-- 11,181 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
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
// Copyright 2021 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/lens/region_search/lens_region_search_controller.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/image_editor/screenshot_flow.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/lens/lens_region_search_instructions_view.h"
#include "components/lens/buildflags.h"
#include "components/lens/lens_entrypoints.h"
#include "components/lens/lens_features.h"
#include "components/lens/lens_metadata.mojom.h"
#include "components/lens/lens_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/widget/widget.h"

namespace {
#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
views::Widget* OpenLensRegionSearchInstructions(
    Browser* browser,
    base::OnceClosure close_callback,
    base::OnceClosure escape_callback) {
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
  CHECK(browser_view);
  // Our anchor should be the browser view's top container view. This makes sure
  // that we account for side panel width and the top container view.
  views::View* anchor = browser_view->contents_web_view();
  return views::BubbleDialogDelegateView::CreateBubble(
      std::make_unique<lens::LensRegionSearchInstructionsView>(
          anchor, std::move(close_callback), std::move(escape_callback)));
}
#endif // BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
}  // namespace

namespace lens {

LensRegionSearchControllerData::LensRegionSearchControllerData() = default;
LensRegionSearchControllerData::~LensRegionSearchControllerData() = default;

RegionSearchCapturedData::RegionSearchCapturedData() = default;
RegionSearchCapturedData::~RegionSearchCapturedData() = default;

LensRegionSearchController::LensRegionSearchController() {
  weak_this_ = weak_factory_.GetWeakPtr();
}

LensRegionSearchController::~LensRegionSearchController() {
  CloseWithReason(views::Widget::ClosedReason::kLostFocus);
}

void LensRegionSearchController::Start(
    content::WebContents* web_contents,
    bool use_fullscreen_capture,
    bool is_google_default_search_provider,
    lens::AmbientSearchEntryPoint entry_point) {
  entry_point_ = entry_point;
  is_google_default_search_provider_ = is_google_default_search_provider;
  // Return early if web contents/browser don't exist and if capture mode is
  // already active.
  if (!web_contents || in_capture_mode_) {
    return;
  }
  Browser* browser = chrome::FindBrowserWithTab(web_contents);
  if (!browser) {
    return;
  }

  Observe(web_contents);
  if (!screenshot_flow_) {
    screenshot_flow_ =
        std::make_unique<image_editor::ScreenshotFlow>(web_contents);
  }

  base::OnceCallback<void(const image_editor::ScreenshotCaptureResult&)>
      callback = base::BindOnce(&LensRegionSearchController::OnCaptureCompleted,
                                weak_this_);
  in_capture_mode_ = true;
  if (use_fullscreen_capture) {
    screenshot_flow_->StartFullscreenCapture(std::move(callback));
  } else {
#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
    // Create user education bubble anchored to the toolbar container.
    // This is only done for non-fulllscreen capture.
    bubble_widget_ = OpenLensRegionSearchInstructions(
        browser,
        base::BindOnce(&LensRegionSearchController::Close,
                       base::Unretained(this)),
        base::BindOnce(&LensRegionSearchController::Escape,
                       base::Unretained(this)));
    bubble_widget_->Show();
#endif
    screenshot_flow_->Start(std::move(callback));
  }
}

void LensRegionSearchController::RecordCaptureResult(
    lens::LensRegionSearchCaptureResult result) {
  UMA_HISTOGRAM_ENUMERATION(lens::kLensRegionSearchCaptureResultHistogramName,
                            result);
}

int LensRegionSearchController::CalculateViewportProportionFromAreas(
    int screen_height,
    int screen_width,
    int image_width,
    int image_height) {
  // To get the region proportion of the screen, we must calculate the areas of
  // the screen and captured region. Then, we must divide the area of the region
  // by the area of the screen to get the percentage. Multiply by 100 to make it
  // an integer. Returns -1 if screen_area is 0 to prevent undefined values.
  double screen_area = screen_width * screen_height;
  if (screen_area <= 0) {
    return -1;
  }
  double region_area = image_width * image_height;
  double region_proportion = region_area / screen_area;
  int region_proportion_int = region_proportion * 100;
  return region_proportion_int;
}

lens::LensRegionSearchAspectRatio
LensRegionSearchController::GetAspectRatioFromSize(int image_height,
                                                   int image_width) {
  // Convert to double to prevent integer division.
  double width = image_width;
  double height = image_height;

  // To record region aspect ratio, we must divide the region's width by height.
  // Should never be zero, but check to prevent any crashes or undefined
  // recordings.
  if (height > 0) {
    double aspect_ratio = width / height;
    if (aspect_ratio <= 1.2 && aspect_ratio >= 0.8) {
      return lens::LensRegionSearchAspectRatio::SQUARE;
    } else if (aspect_ratio < 0.8 && aspect_ratio >= 0.3) {
      return lens::LensRegionSearchAspectRatio::TALL;
    } else if (aspect_ratio < 0.3) {
      return lens::LensRegionSearchAspectRatio::VERY_TALL;
    } else if (aspect_ratio > 1.2 && aspect_ratio <= 1.7) {
      return lens::LensRegionSearchAspectRatio::WIDE;
    } else if (aspect_ratio > 1.7) {
      return lens::LensRegionSearchAspectRatio::VERY_WIDE;
    }
  }
  return lens::LensRegionSearchAspectRatio::UNDEFINED;
}

void LensRegionSearchController::RecordRegionSizeRelatedMetrics(
    gfx::Rect screen_bounds,
    gfx::Size image_size) {
  // If any of the rects are empty, it means the area is zero. In this case,
  // return.
  if (screen_bounds.IsEmpty() || image_size.IsEmpty()) {
    return;
  }
  double region_width = image_size.width();
  double region_height = image_size.height();

  int region_proportion = CalculateViewportProportionFromAreas(
      screen_bounds.height(), screen_bounds.width(), region_width,
      region_height);
  if (region_proportion >= 0) {
    base::UmaHistogramPercentage(
        lens::kLensRegionSearchRegionViewportProportionHistogramName,
        region_proportion);
  }

  // To record region aspect ratio, we must divide the region's width by height.
  base::UmaHistogramEnumeration(
      lens::kLensRegionSearchRegionAspectRatioHistogramName,
      GetAspectRatioFromSize(region_height, region_width));
}

void LensRegionSearchController::OnCaptureCompleted(
    const image_editor::ScreenshotCaptureResult& result) {
  // Close all open UI overlays and bubbles.
  CloseWithReason(views::Widget::ClosedReason::kLostFocus);
  image_editor::ScreenshotCaptureResultCode code = result.result_code;
  if (code == image_editor::ScreenshotCaptureResultCode::USER_NAVIGATED_EXIT) {
    RecordCaptureResult(
        lens::LensRegionSearchCaptureResult::USER_NAVIGATED_FROM_CAPTURE);
    return;
  } else if (code ==
             image_editor::ScreenshotCaptureResultCode::USER_ESCAPE_EXIT) {
    RecordCaptureResult(
        lens::LensRegionSearchCaptureResult::USER_EXITED_CAPTURE_ESCAPE);
    return;
  }

  const gfx::Image& image = result.image;

  // If image is empty, then record UMA and close.
  if (image.IsEmpty()) {
    RecordCaptureResult(
        lens::LensRegionSearchCaptureResult::ERROR_CAPTURING_REGION);
    return;
  }

  // Record region size related UMA histograms according to region and screen.
  RecordRegionSizeRelatedMetrics(result.screen_bounds, image.Size());

  auto* core_tab_helper = CoreTabHelper::FromWebContents(web_contents());
  if (!core_tab_helper) {
    RecordCaptureResult(
        lens::LensRegionSearchCaptureResult::FAILED_TO_OPEN_TAB);
    return;
  }

  lens::RecordAmbientSearchQuery(entry_point_);

  if (is_google_default_search_provider_) {
    lens::EntryPoint lens_entry_point =
        lens::EntryPoint::CHROME_REGION_SEARCH_MENU_ITEM;
    switch (entry_point_) {
      case lens::AmbientSearchEntryPoint::
          LENS_OVERLAY_LOCATION_BAR_ACCESSIBILITY_FALLBACK:
        if (!lens::features::IsLensOverlayKeyboardSelectionEnabled()) {
          lens_entry_point = lens::EntryPoint::CHROME_LENS_OVERLAY_LOCATION_BAR;
        }
        break;
      default:
        // The other possible values of `entry_point_` should not be possible
        // and are considered invalid.
        break;
    }
    core_tab_helper->SearchWithLens(image, lens_entry_point);
  } else {
    core_tab_helper->SearchByImage(image);
  }

  RecordCaptureResult(lens::LensRegionSearchCaptureResult::SUCCESS);
}

void LensRegionSearchController::WebContentsDestroyed() {
  CloseWithReason(views::Widget::ClosedReason::kLostFocus);
}

void LensRegionSearchController::OnVisibilityChanged(
    content::Visibility visibility) {
  if (in_capture_mode_ && visibility == content::Visibility::HIDDEN) {
    RecordCaptureResult(
        lens::LensRegionSearchCaptureResult::USER_NAVIGATED_FROM_CAPTURE);
    CloseWithReason(views::Widget::ClosedReason::kLostFocus);
  }
}

void LensRegionSearchController::Close() {
  CloseWithReason(views::Widget::ClosedReason::kCloseButtonClicked);
  // Record a capture result when the instructional bubble is responsible for
  // exiting out of the capture mode.
  RecordCaptureResult(
      LensRegionSearchCaptureResult::USER_EXITED_CAPTURE_CLOSE_BUTTON);
}

void LensRegionSearchController::Escape() {
  CloseWithReason(views::Widget::ClosedReason::kEscKeyPressed);
  // Record a capture result when the instructional bubble is responsible for
  // exiting out of the capture mode.
  RecordCaptureResult(
      LensRegionSearchCaptureResult::USER_EXITED_CAPTURE_ESCAPE);
}

void LensRegionSearchController::CloseWithReason(
    views::Widget::ClosedReason reason) {
  in_capture_mode_ = false;
  if (bubble_widget_) {
    std::exchange(bubble_widget_, nullptr)->CloseWithReason(reason);
  }
  if (screenshot_flow_) {
    screenshot_flow_->CancelCapture();
    screenshot_flow_.reset();
  }
}

bool LensRegionSearchController::IsOverlayUIVisibleForTesting() {
  if (!bubble_widget_ || !screenshot_flow_) {
    return false;
  }
  return bubble_widget_->IsVisible() && screenshot_flow_->IsCaptureModeActive();
}

void LensRegionSearchController::SetEntryPointForTesting(
    lens::AmbientSearchEntryPoint entry_point) {
  entry_point_ = entry_point;
}

void LensRegionSearchController::SetWebContentsForTesting(
    content::WebContents* web_contents) {
  Observe(web_contents);
}

}  // namespace lens