Skip to content

Commit

Permalink
Add InfoBar delegate for previews
Browse files Browse the repository at this point in the history
Add PreviewsInfoBarDelegate to show an infobar when a preview
page has been loaded. Also add the corresponding resources.

BUG=615566

Review-Url: https://codereview.chromium.org/2250223002
Cr-Commit-Position: refs/heads/master@{#418413}
  • Loading branch information
megjablon authored and Commit bot committed Sep 13, 2016
1 parent f47b481 commit 82aba52
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 135 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions chrome/app/generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -6391,6 +6391,18 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_FLAGS_DATA_REDUCTION_PROXY_CONFIG_CLIENT_DESCRIPTION" desc="Description in about::flags to use the Data Reduction Proxy configuration service" translateable="false">
Uses the Data Reduction Proxy configuration service for authentication and proxy configuration.
</message>

<!-- Preview InfoBar -->
<message name="IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE" desc="The text of the infobar notifying the user that a preview page has been shown to save data.">
Saved data.
</message>
<message name="IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE" desc="The text of the infobar notifying the user that a preview page has been shown to load faster.">
Faster page loaded.
</message>
<message name="IDS_PREVIEWS_INFOBAR_LINK" desc="The text of the link to load the original page from the infobar notifying the user that a preview page has been shown.">
Show original
</message>

<message name="IDS_FLAGS_LCD_TEXT_NAME" desc="Name of about:flags option for LCD text.">
LCD text antialiasing
</message>
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ split_static_library("browser") {
"prerender/prerender_tab_helper.h",
"prerender/prerender_util.cc",
"prerender/prerender_util.h",
"previews/previews_infobar_delegate.cc",
"previews/previews_infobar_delegate.h",
"previews/previews_service.cc",
"previews/previews_service.h",
"previews/previews_service_factory.cc",
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/android/resource_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_GEOLOCATION,
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_FROZEN_TAB, R.drawable.infobar_restore)
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_SUBRESOURCE_FILTERING,
R.drawable.infobar_subresource_filtering)
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_PREVIEWS, R.drawable.infobar_previews)

// WebsiteSettingsUI images, used in ConnectionInfoPopup
// Good:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "build/build_config.h"
#include "chrome/browser/net/spdyproxy/chrome_data_use_group_provider.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/previews/previews_infobar_delegate.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/common/pref_names.h"
Expand All @@ -25,7 +26,6 @@

#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#include "chrome/browser/android/tab_android.h"
#endif

namespace content {
Expand All @@ -36,18 +36,11 @@ using data_reduction_proxy::DataReductionProxyParams;

namespace {

// For Android builds, notifies the TabAndroid associated with |web_contents|
// that a Lo-Fi response has been received. The TabAndroid then handles showing
// Lo-Fi UI if this is the first Lo-Fi response for a page load. |is_preview|
// indicates whether the response was a Lo-Fi preview response.
void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents,
bool is_preview) {
// If this is the first Lo-Fi response for a page load, a
// PreviewsInfoBarDelegate is created, which handles showing Lo-Fi UI.
void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if defined(OS_ANDROID)
TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
if (tab)
tab->OnLoFiResponseReceived(is_preview);
#endif
PreviewsInfoBarDelegate::Create(web_contents, PreviewsInfoBarDelegate::LOFI);
}

} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged(
void DataReductionProxySettingsAndroid::IncrementLoFiSnackbarShown(
JNIEnv* env,
const JavaParamRef<jobject>& obj) {
Settings()->IncrementLoFiSnackbarShown();
Settings()->IncrementLoFiUIShown();
}

void DataReductionProxySettingsAndroid::IncrementLoFiUserRequestsForImages(
Expand Down
97 changes: 97 additions & 0 deletions chrome/browser/previews/previews_infobar_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/previews/previews_infobar_delegate.h"

#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"

PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() {}

// static
void PreviewsInfoBarDelegate::Create(content::WebContents* web_contents,
PreviewsInfoBarType infobar_type) {
// TODO(megjablon): Check that the infobar was not already shown.

InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents);

infobars::InfoBar* infobar =
infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate>(
new PreviewsInfoBarDelegate(web_contents, infobar_type))));

if (infobar && (infobar_type == LITE_PAGE || infobar_type == LOFI)) {
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents->GetBrowserContext());
data_reduction_proxy_settings->IncrementLoFiUIShown();
}
}

PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
content::WebContents* web_contents,
PreviewsInfoBarType infobar_type)
: ConfirmInfoBarDelegate(),
infobar_type_(infobar_type) {}

infobars::InfoBarDelegate::InfoBarIdentifier
PreviewsInfoBarDelegate::GetIdentifier() const {
return DATA_REDUCTION_PROXY_PREVIEW_INFOBAR_DELEGATE;
}

int PreviewsInfoBarDelegate::GetIconId() const {
#if defined(OS_ANDROID)
return IDR_ANDROID_INFOBAR_PREVIEWS;
#else
return kNoIconID;
#endif
}

bool PreviewsInfoBarDelegate::ShouldExpire(
const NavigationDetails& details) const {
// TODO(megjablon): Record UMA data.
return InfoBarDelegate::ShouldExpire(details);
}

base::string16 PreviewsInfoBarDelegate::GetMessageText() const {
return l10n_util::GetStringUTF16((infobar_type_ == OFFLINE)
? IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE
: IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE);
}

int PreviewsInfoBarDelegate::GetButtons() const {
return BUTTON_NONE;
}

base::string16 PreviewsInfoBarDelegate::GetLinkText() const {
return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK);
}

bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
// TODO(megjablon): Record UMA data.
if (infobar_type_ == LITE_PAGE || infobar_type_ == LOFI) {
auto* web_contents =
InfoBarService::WebContentsFromInfoBar(infobar());

if (infobar_type_ == LITE_PAGE)
web_contents->GetController().ReloadDisableLoFi(true);
else if (infobar_type_ == LOFI)
web_contents->ReloadLoFiImages();

auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents->GetBrowserContext());
data_reduction_proxy_settings->IncrementLoFiUserRequestsForImages();
}

return true;
}
53 changes: 53 additions & 0 deletions chrome/browser/previews/previews_infobar_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PREVIEWS_PREVIEWS_INFOBAR_DELEGATE_H_
#define CHROME_BROWSER_PREVIEWS_PREVIEWS_INFOBAR_DELEGATE_H_

#include "components/infobars/core/confirm_infobar_delegate.h"

namespace content {
class WebContents;
}

// Shows an infobar that lets the user know that a preview page has been loaded,
// and gives the user a link to reload the original page. This infobar will only
// be shown once per page load. Records UMA data for user interactions with the
// infobar.
class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
public:
// The type of the infobar. It controls the strings and what UMA data is
// recorded for the infobar.
enum PreviewsInfoBarType {
LOFI, // Server-side image replacement.
LITE_PAGE, // Server-side page rewrite.
OFFLINE, // Offline copy of the page.
};

~PreviewsInfoBarDelegate() override;

// Creates a preview infobar and corresponding delegate and adds the infobar
// to InfoBarService.
static void Create(content::WebContents* web_contents,
PreviewsInfoBarType infobar_type);

private:
PreviewsInfoBarDelegate(content::WebContents* web_contents,
PreviewsInfoBarType infobar_type);

// ConfirmInfoBarDelegate overrides:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
int GetIconId() const override;
bool ShouldExpire(const NavigationDetails& details) const override;
base::string16 GetMessageText() const override;
int GetButtons() const override;
base::string16 GetLinkText() const override;
bool LinkClicked(WindowOpenDisposition disposition) override;

PreviewsInfoBarType infobar_type_;

DISALLOW_COPY_AND_ASSIGN(PreviewsInfoBarDelegate);
};

#endif // CHROME_BROWSER_PREVIEWS_PREVIEWS_INFOBAR_DELEGATE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ ContentLoFiUIService::ContentLoFiUIService(

ContentLoFiUIService::~ContentLoFiUIService() {}

void ContentLoFiUIService::OnLoFiReponseReceived(const net::URLRequest& request,
bool is_preview) {
void ContentLoFiUIService::OnLoFiReponseReceived(
const net::URLRequest& request) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
int render_process_id = -1;
int render_frame_id = -1;
Expand All @@ -37,23 +37,21 @@ void ContentLoFiUIService::OnLoFiReponseReceived(const net::URLRequest& request,
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ContentLoFiUIService::OnLoFiResponseReceivedOnUIThread,
base::Unretained(this), render_process_id, render_frame_id,
is_preview));
base::Unretained(this), render_process_id, render_frame_id));
}
}

void ContentLoFiUIService::OnLoFiResponseReceivedOnUIThread(
int render_process_id,
int render_frame_id,
bool is_preview) {
int render_frame_id) {
DCHECK(ui_task_runner_->BelongsToCurrentThread());
content::RenderFrameHost* frame =
content::RenderFrameHost::FromID(render_process_id, render_frame_id);
if (frame) {
DCHECK(!on_lofi_response_received_callback_.is_null());
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame);
on_lofi_response_received_callback_.Run(web_contents, is_preview);
on_lofi_response_received_callback_.Run(web_contents);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class URLRequest;

namespace data_reduction_proxy {

typedef base::Callback<void(content::WebContents* web_contents,
bool is_preview)> OnLoFiResponseReceivedCallback;
using OnLoFiResponseReceivedCallback =
base::Callback<void(content::WebContents* web_contents)>;

// Passes notifications to the UI thread that a Lo-Fi response has been
// received. These notifications may be used to show Lo-Fi UI. This object lives
Expand All @@ -38,17 +38,14 @@ class ContentLoFiUIService : public LoFiUIService {
~ContentLoFiUIService() override;

// LoFiUIService implementation:
void OnLoFiReponseReceived(const net::URLRequest& request,
bool is_preview) override;
void OnLoFiReponseReceived(const net::URLRequest& request) override;

private:
// Using the |render_process_id| and |render_frame_id|, gets the associated
// WebContents if it exists and runs the
// |notify_lofi_response_received_callback_|. |is_preview| indicates whether
// the response was a Lo-Fi preview response.
// |notify_lofi_response_received_callback_|.
void OnLoFiResponseReceivedOnUIThread(int render_process_id,
int render_frame_id,
bool is_preview);
int render_frame_id);

// A task runner to post calls to OnLoFiReponseReceivedOnUI on the UI
// thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace data_reduction_proxy {

class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness {
public:
ContentLoFiUIServiceTest() : callback_called_(false), is_preview_(false) {
ContentLoFiUIServiceTest() : callback_called_(false) {
// Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness.
SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
}

void RunTestOnIOThread(base::RunLoop* ui_run_loop, bool is_preview) {
void RunTestOnIOThread(base::RunLoop* ui_run_loop) {
ASSERT_TRUE(ui_run_loop);
EXPECT_TRUE(
content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
Expand All @@ -52,7 +52,7 @@ class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness {
std::unique_ptr<net::URLRequest> request =
CreateRequest(context, &delegate);

content_lofi_ui_service_->OnLoFiReponseReceived(*request, is_preview);
content_lofi_ui_service_->OnLoFiReponseReceived(*request);

content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
Expand Down Expand Up @@ -81,47 +81,32 @@ class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness {
return request;
}

void OnLoFiResponseReceivedCallback(content::WebContents* web_contents,
bool is_preview) {
void OnLoFiResponseReceivedCallback(content::WebContents* web_contents) {
EXPECT_TRUE(
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
callback_called_ = true;
is_preview_ = is_preview;
}

void VerifyOnLoFiResponseReceivedCallback(bool is_preview) {
void VerifyOnLoFiResponseReceivedCallback() {
EXPECT_TRUE(
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
EXPECT_TRUE(callback_called_);
EXPECT_EQ(is_preview, is_preview_);
}

private:
std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_;
bool callback_called_;
bool is_preview_;
};

TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceived) {
base::RunLoop ui_run_loop;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread,
base::Unretained(this), &ui_run_loop, false));
base::Unretained(this), &ui_run_loop));
ui_run_loop.Run();
base::RunLoop().RunUntilIdle();
VerifyOnLoFiResponseReceivedCallback(false);
}

TEST_F(ContentLoFiUIServiceTest, OnLoFiPreviewResponseReceived) {
base::RunLoop ui_run_loop;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread,
base::Unretained(this), &ui_run_loop, true));
ui_run_loop.Run();
base::RunLoop().RunUntilIdle();
VerifyOnLoFiResponseReceivedCallback(true);
VerifyOnLoFiResponseReceivedCallback();
}

} // namespace data_reduction_proxy
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,7 @@ class TestLoFiUIService : public LoFiUIService {
TestLoFiUIService() {}
~TestLoFiUIService() override {}

void OnLoFiReponseReceived(const net::URLRequest& request,
bool is_preview) override {}
void OnLoFiReponseReceived(const net::URLRequest& request) override {}
};

class DataReductionProxyDelegateTest : public testing::Test {
Expand Down
Loading

0 comments on commit 82aba52

Please sign in to comment.