-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Add functionality to pass a full page screenshot to chrome://l…
…ens page for display. - This also starts the Lens region search controller when the page is done loading. - This allows the region search controller to observe more than one web contents in its lifetime. - Closes the web contents on capture complete if the static page feature is enabled. Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome,mac-chrome Bug: b:242724302, b:242724636 Change-Id: I18a65da2564a07caad09e236b2957967e0f0ff5c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4018087 Reviewed-by: Ali Stanfield <stanfield@google.com> Reviewed-by: Scott Violet <sky@chromium.org> Commit-Queue: Juan Mojica <juanmojica@google.com> Cr-Commit-Position: refs/heads/main@{#1070575}
- Loading branch information
Juan Mojica
authored and
Chromium LUCI CQ
committed
Nov 11, 2022
1 parent
66273c7
commit 9b85c86
Showing
12 changed files
with
276 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
chrome/browser/ui/views/lens/lens_static_page_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2022 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/views/lens/lens_static_page_controller.h" | ||
|
||
#include "chrome/browser/lens/region_search/lens_region_search_controller.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/common/webui_url_constants.h" | ||
#include "ui/base/webui/web_ui_util.h" | ||
#include "ui/gfx/image/image.h" | ||
#include "ui/snapshot/snapshot.h" | ||
|
||
namespace lens { | ||
|
||
LensStaticPageData::LensStaticPageData() = default; | ||
LensStaticPageData::~LensStaticPageData() = default; | ||
|
||
LensStaticPageController::LensStaticPageController(Browser* browser) | ||
: browser_(browser) {} | ||
LensStaticPageController::~LensStaticPageController() = default; | ||
|
||
void LensStaticPageController::OpenStaticPage() { | ||
DCHECK(browser_); | ||
// Take a screenshot of the active web contents and save it to profile user | ||
// data. | ||
content::WebContents* active_web_contents = | ||
browser_->tab_strip_model()->GetActiveWebContents(); | ||
gfx::Rect fullscreen_size = gfx::Rect(active_web_contents->GetSize()); | ||
// TODO(crbug/1383279): Refactor screenshot code shared here with code in | ||
// image_editor::ScreenshotFlow. | ||
#if BUILDFLAG(IS_MAC) | ||
const gfx::NativeView& native_view = | ||
active_web_contents->GetContentNativeView(); | ||
gfx::Image img; | ||
bool rval = ui::GrabViewSnapshot(native_view, fullscreen_size, &img); | ||
// If |img| is empty, clients should treat it as a canceled action, but | ||
// we have a DCHECK for development as we expected this call to succeed. | ||
DCHECK(rval); | ||
LoadChromeLens(img); | ||
#else | ||
ui::GrabWindowSnapshotAsyncCallback load_url_callback = base::BindOnce( | ||
&LensStaticPageController::LoadChromeLens, base::Unretained(this)); | ||
const gfx::NativeWindow& native_window = active_web_contents->GetNativeView(); | ||
ui::GrabWindowSnapshotAsync(native_window, fullscreen_size, | ||
std::move(load_url_callback)); | ||
#endif | ||
} | ||
|
||
void LensStaticPageController::LoadChromeLens(gfx::Image image) { | ||
DCHECK(browser_); | ||
auto region_search_data = std::make_unique<RegionSearchCapturedData>(); | ||
region_search_data->image = image; | ||
browser_->profile()->SetUserData(RegionSearchCapturedData::kDataKey, | ||
std::move(region_search_data)); | ||
|
||
// After we have set the image data, we can open the WebUI tab and finish | ||
// displaying the region search experience. | ||
GURL url(chrome::kChromeUILensURL); | ||
content::OpenURLParams params( | ||
url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, | ||
ui::PAGE_TRANSITION_LINK, /*is_renderer_initiated=*/false); | ||
params.initiator_origin = url::Origin::Create(url); | ||
content::WebContents* new_tab = browser_->OpenURL(params); | ||
// Observe the new web contents in order to start the region search controller | ||
// once it is properly loaded (this prevents the region search controller from | ||
// closing prematurely). | ||
Observe(new_tab); | ||
} | ||
|
||
void LensStaticPageController::DocumentOnLoadCompletedInPrimaryMainFrame() { | ||
StartRegionSearch(web_contents()); | ||
} | ||
|
||
void LensStaticPageController::StartRegionSearch( | ||
content::WebContents* contents) { | ||
DCHECK(contents); | ||
DCHECK(browser_); | ||
if (!lens_region_search_controller_) { | ||
lens_region_search_controller_ = | ||
std::make_unique<lens::LensRegionSearchController>(browser_); | ||
} | ||
lens_region_search_controller_->Start( | ||
contents, | ||
/*use_fullscreen_capture=*/false, | ||
/*is_google_default_search_provider=*/true); | ||
} | ||
|
||
} // namespace lens |
Oops, something went wrong.