Skip to content

Commit

Permalink
[iOS] [WebChannels] Open NTP with Following feed selected
Browse files Browse the repository at this point in the history
Upon following a site, a snackbar appears with a "Go to feed" button.
This button now opens the NTP with the Following feed selected, scrolled
to the feed. This is achieved using the NTPHelper which can be used to
set startup properties for the next opened NTP.

Renames existing NewTabPageCommands to NewTabPageDelegate, since we are
now adding a new NewTabPageCommands in ui/commands.

Tests cannot be added for now since Bling doesn't support WPR yet.

Downstream CL: https://crrev.com/i/4651875

Bug: 1264872
Change-Id: I9ea9cfee1853f296baffe33bc5daf17e2fd92e0c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3558809
Reviewed-by: Tina Wang <tinazwang@chromium.org>
Reviewed-by: Sergio Collazos <sczs@chromium.org>
Commit-Queue: Adam Arcaro <adamta@google.com>
Cr-Commit-Position: refs/heads/main@{#989046}
  • Loading branch information
adamta authored and Chromium LUCI CQ committed Apr 5, 2022
1 parent 43c7dd3 commit b71ad7a
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 72 deletions.
1 change: 1 addition & 0 deletions ios/chrome/browser/ntp/BUILD.gn
Expand Up @@ -19,6 +19,7 @@ source_set("ntp") {
"//components/url_formatter",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/discover_feed:constants",
"//ios/chrome/browser/ui:feature_flags",
"//ios/components/webui:url_constants",
"//ios/web/common:features",
Expand Down
23 changes: 23 additions & 0 deletions ios/chrome/browser/ntp/new_tab_page_tab_helper.h
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include "base/timer/timer.h"
#include "ios/chrome/browser/discover_feed/feed_constants.h"
#include "ios/web/public/web_state_observer.h"
#import "ios/web/public/web_state_user_data.h"

Expand Down Expand Up @@ -48,6 +49,19 @@ class NewTabPageTabHelper : public web::WebStateObserver,
// state.
bool IgnoreLoadRequests() const;

// Returns the initially selected feed for the next NTP and then resets it to
// default.
FeedType GetNextNTPFeedType();

// Sets the default feed for the next NTP.
void SetNextNTPFeedType(FeedType feed_type);

// Returns whether the next NTP should be initially scrolled to the feed.
bool GetNextNTPScrolledToFeed();

// Sets whether the next NTP should be initially scrolled to the feed.
void SetNextNTPScrolledToFeed(bool scrolled_to_feed);

// Sets the NTP's NavigationItem title and virtualURL to the appropriate
// string and chrome://newtab respectively.
static void UpdateItem(web::NavigationItem* item);
Expand Down Expand Up @@ -80,6 +94,9 @@ class NewTabPageTabHelper : public web::WebStateObserver,
// timer.
void DisableIgnoreLoadRequests();

// Returns the default selected feed for the NTP.
FeedType GetDefaultFeedType();

// Used to present and dismiss the NTP.
__weak id<NewTabPageTabHelperDelegate> delegate_ = nil;

Expand All @@ -92,6 +109,12 @@ class NewTabPageTabHelper : public web::WebStateObserver,
// |YES| if the NTP's underlying ios/web page is still loading.
BOOL ignore_load_requests_ = NO;

// The default feed type of the next NTP.
FeedType next_ntp_feed_type_;

// Whether the next NTP should be initially scrolled to the feed.
BOOL next_ntp_scrolled_to_feed_ = NO;

// Ensure the ignore_load_requests_ flag is never set to NO for more than
// |kMaximumIgnoreLoadRequestsTime| seconds.
std::unique_ptr<base::OneShotTimer> ignore_load_requests_timer_;
Expand Down
31 changes: 31 additions & 0 deletions ios/chrome/browser/ntp/new_tab_page_tab_helper.mm
Expand Up @@ -51,6 +51,7 @@
: web_state_(web_state) {
web_state->AddObserver(this);
active_ = IsNTPURL(web_state_->GetVisibleURL());
next_ntp_feed_type_ = GetDefaultFeedType();
}

void NewTabPageTabHelper::SetDelegate(
Expand Down Expand Up @@ -82,6 +83,30 @@
return ignore_load_requests_;
}

FeedType NewTabPageTabHelper::GetNextNTPFeedType() {
FeedType feed_type = next_ntp_feed_type_;
// Resets feed type to default in case next_ntp_feed_type_ was overriden by
// SetNextNTPFeedType.
next_ntp_feed_type_ = GetDefaultFeedType();
return feed_type;
}

void NewTabPageTabHelper::SetNextNTPFeedType(FeedType feed_type) {
next_ntp_feed_type_ = feed_type;
}

bool NewTabPageTabHelper::GetNextNTPScrolledToFeed() {
bool scrolled_ = next_ntp_scrolled_to_feed_;
// Resets next_ntp_scrolled_to_feed_ in case it was overriden by
// SetNextNTPScrolledToFeed.
next_ntp_scrolled_to_feed_ = NO;
return scrolled_;
}

void NewTabPageTabHelper::SetNextNTPScrolledToFeed(bool scrolled_to_feed) {
next_ntp_scrolled_to_feed_ = scrolled_to_feed;
}

// static
void NewTabPageTabHelper::UpdateItem(web::NavigationItem* item) {
if (item && item->GetURL() == GURL(kChromeUIAboutNewTabURL)) {
Expand Down Expand Up @@ -113,6 +138,12 @@
ignore_load_requests_ = NO;
}

FeedType NewTabPageTabHelper::GetDefaultFeedType() {
// TODO(crbug.com/1277974): Make sure that we always want the Discover feed as
// default.
return FeedTypeDiscover;
}

#pragma mark - WebStateObserver

void NewTabPageTabHelper::WebStateDestroyed(web::WebState* web_state) {
Expand Down
29 changes: 29 additions & 0 deletions ios/chrome/browser/ui/browser_view/browser_view_controller.mm
Expand Up @@ -28,6 +28,7 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#include "ios/chrome/browser/crash_report/crash_keys_helper.h"
#include "ios/chrome/browser/discover_feed/feed_constants.h"
#include "ios/chrome/browser/feature_engagement/tracker_factory.h"
#include "ios/chrome/browser/feature_engagement/tracker_util.h"
#import "ios/chrome/browser/find_in_page/find_tab_helper.h"
Expand Down Expand Up @@ -549,6 +550,9 @@ - (instancetype)initWithBrowser:(Browser*)browser
[self.commandDispatcher
startDispatchingToTarget:self
forProtocol:@protocol(BrowserCommands)];
[self.commandDispatcher
startDispatchingToTarget:self
forProtocol:@protocol(NewTabPageCommands)];

_toolbarCoordinatorAdaptor =
[[ToolbarCoordinatorAdaptor alloc] initWithDispatcher:self.dispatcher];
Expand Down Expand Up @@ -3929,6 +3933,25 @@ - (void)searchImageWithLens:(SearchImageWithLensCommand*)command {
[self presentViewController:lensViewController animated:YES completion:nil];
}

#pragma mark - NewTabPageCommands

- (void)openNTPScrolledIntoFeedType:(FeedType)feedType {
// Configure next NTP to be scrolled into |feedType|.
NewTabPageTabHelper* NTPHelper =
NewTabPageTabHelper::FromWebState(self.currentWebState);
if (NTPHelper) {
NTPHelper->SetNextNTPFeedType(feedType);
NTPHelper->SetNextNTPScrolledToFeed(YES);
}

// Navigate to NTP in same tab.
UrlLoadingBrowserAgent* urlLoadingBrowserAgent =
UrlLoadingBrowserAgent::FromBrowser(self.browser);
UrlLoadParams urlLoadParams =
UrlLoadParams::InCurrentTab(GURL(kChromeUINewTabURL));
urlLoadingBrowserAgent->Load(urlLoadParams);
}

#pragma mark - ChromeLensControllerDelegate
// TODO(crbug.com/1272549): Move this delegate implmentation into
// BrowserCoordinator, or into the dedicated lens coordinator.
Expand Down Expand Up @@ -4542,6 +4565,9 @@ - (void)newTabPageHelperDidChangeVisibility:(NewTabPageTabHelper*)NTPHelper
if (NTPHelper->IsActive()) {
[self.ntpCoordinator ntpDidChangeVisibility:YES];
self.ntpCoordinator.webState = webState;
self.ntpCoordinator.selectedFeed = NTPHelper->GetNextNTPFeedType();
self.ntpCoordinator.shouldScrollIntoFeed =
NTPHelper->GetNextNTPScrolledToFeed();
} else {
[self.ntpCoordinator ntpDidChangeVisibility:NO];
self.ntpCoordinator.webState = nullptr;
Expand All @@ -4562,6 +4588,9 @@ - (void)newTabPageHelperDidChangeVisibility:(NewTabPageTabHelper*)NTPHelper
newTabPageCoordinator.toolbarDelegate = self.toolbarInterface;
newTabPageCoordinator.webState = webState;
newTabPageCoordinator.bubblePresenter = self.bubblePresenter;
newTabPageCoordinator.selectedFeed = NTPHelper->GetNextNTPFeedType();
newTabPageCoordinator.shouldScrollIntoFeed =
NTPHelper->GetNextNTPScrolledToFeed();
_ntpCoordinatorsForWebStates[webState] = newTabPageCoordinator;
} else {
NewTabPageCoordinator* newTabPageCoordinator =
Expand Down
2 changes: 2 additions & 0 deletions ios/chrome/browser/ui/commands/BUILD.gn
Expand Up @@ -23,6 +23,7 @@ source_set("commands") {
"help_commands.h",
"infobar_commands.h",
"load_query_commands.h",
"new_tab_page_commands.h",
"omnibox_commands.h",
"omnibox_suggestion_commands.h",
"open_new_tab_command.h",
Expand Down Expand Up @@ -65,6 +66,7 @@ source_set("commands") {
"//components/browsing_data/core",
"//components/password_manager/core/browser",
"//ios/chrome/browser/browsing_data:browsing_data_remove_mask",
"//ios/chrome/browser/discover_feed:constants",
]
}

Expand Down
2 changes: 2 additions & 0 deletions ios/chrome/browser/ui/commands/browser_commands.h
Expand Up @@ -11,6 +11,7 @@
#import "ios/chrome/browser/ui/commands/activity_service_commands.h"
#import "ios/chrome/browser/ui/commands/browser_coordinator_commands.h"
#import "ios/chrome/browser/ui/commands/infobar_commands.h"
#import "ios/chrome/browser/ui/commands/new_tab_page_commands.h"
#import "ios/chrome/browser/ui/commands/page_info_commands.h"
#import "ios/chrome/browser/ui/commands/popup_menu_commands.h"
#import "ios/chrome/browser/ui/commands/qr_scanner_commands.h"
Expand All @@ -29,6 +30,7 @@ class GURL;
ActivityServiceCommands,
BrowserCoordinatorCommands,
InfobarCommands,
NewTabPageCommands,
PageInfoCommands,
PopupMenuCommands,
QRScannerCommands,
Expand Down
20 changes: 20 additions & 0 deletions ios/chrome/browser/ui/commands/new_tab_page_commands.h
@@ -0,0 +1,20 @@
// Copyright 2022 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 IOS_CHROME_BROWSER_UI_COMMANDS_NEW_TAB_PAGE_COMMANDS_H_
#define IOS_CHROME_BROWSER_UI_COMMANDS_NEW_TAB_PAGE_COMMANDS_H_

#import <UIKit/UIKit.h>

#include "ios/chrome/browser/discover_feed/feed_constants.h"

// Commands related to the new tab page.
@protocol NewTabPageCommands

// Opens a new tab page scrolled into the feed with a given |feedType| selected.
- (void)openNTPScrolledIntoFeedType:(FeedType)feedType;

@end

#endif // IOS_CHROME_BROWSER_UI_COMMANDS_NEW_TAB_PAGE_COMMANDS_H_
Expand Up @@ -15,8 +15,8 @@ class WebState;
@class ContentSuggestionsViewController;
@protocol DiscoverFeedDelegate;
@class FeedMetricsRecorder;
@protocol NewTabPageCommands;
@protocol NewTabPageControllerDelegate;
@protocol NewTabPageDelegate;
class NotificationPromoWhatsNew;
@class NTPHomeMediator;
@protocol ThumbStripSupporting;
Expand Down Expand Up @@ -58,8 +58,8 @@ class NotificationPromoWhatsNew;
// mediator for non NTP logic.
@property(nonatomic, strong) NTPHomeMediator* ntpMediator;

// Command handler for NTP related commands.
@property(nonatomic, weak) id<NewTabPageCommands> ntpCommandHandler;
// Delegate for NTP related actions.
@property(nonatomic, weak) id<NewTabPageDelegate> ntpDelegate;

// Metrics recorder for the feed events related to ContentSuggestions.
@property(nonatomic, strong) FeedMetricsRecorder* feedMetricsRecorder;
Expand Down
Expand Up @@ -55,8 +55,8 @@
#import "ios/chrome/browser/ui/menu/browser_action_factory.h"
#import "ios/chrome/browser/ui/menu/menu_histograms.h"
#import "ios/chrome/browser/ui/ntp/feed_metrics_recorder.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_commands.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_constants.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_delegate.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_feature.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_header_constants.h"
#import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h"
Expand Down Expand Up @@ -340,8 +340,8 @@ - (void)viewDidDisappear {
}

- (void)returnToRecentTabWasAdded {
[self.ntpCommandHandler updateDiscoverFeedLayout];
[self.ntpCommandHandler setContentOffsetToTop];
[self.ntpDelegate updateDiscoverFeedLayout];
[self.ntpDelegate setContentOffsetToTop];
}

- (UIEdgeInsets)safeAreaInsetsForDiscoverFeed {
Expand All @@ -364,7 +364,7 @@ - (void)view:(UIView*)view didDropURL:(const GURL&)URL atPoint:(CGPoint)point {
#pragma mark - ContentSuggestionsHeaderCommands

- (void)updateForHeaderSizeChange {
[self.ntpCommandHandler updateDiscoverFeedLayout];
[self.ntpDelegate updateDiscoverFeedLayout];
}

#pragma mark - Public methods
Expand Down
4 changes: 3 additions & 1 deletion ios/chrome/browser/ui/ntp/BUILD.gn
Expand Up @@ -6,9 +6,10 @@ source_set("ntp") {
sources = [
"discover_feed_delegate.h",
"discover_feed_preview_delegate.h",
"new_tab_page_commands.h",
"new_tab_page_configuring.h",
"new_tab_page_content_delegate.h",
"new_tab_page_controller_delegate.h",
"new_tab_page_delegate.h",
"new_tab_page_omnibox_positioning.h",
]
configs += [ "//build/config/compiler:enable_arc" ]
Expand Down Expand Up @@ -39,6 +40,7 @@ source_set("constants") {
"new_tab_page_header_constants.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
public_deps = [ "//ios/chrome/browser/discover_feed:constants" ]
}

source_set("coordinator") {
Expand Down
21 changes: 21 additions & 0 deletions ios/chrome/browser/ui/ntp/new_tab_page_configuring.h
@@ -0,0 +1,21 @@
// Copyright 2022 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 IOS_CHROME_BROWSER_UI_NTP_NEW_TAB_PAGE_CONFIGURING_H_
#define IOS_CHROME_BROWSER_UI_NTP_NEW_TAB_PAGE_CONFIGURING_H_

#include "ios/chrome/browser/discover_feed/feed_constants.h"

// Protocol containing the properties to configure the NTP.
@protocol NewTabPageConfiguring

// Currently selected feed.
@property(nonatomic, assign) FeedType selectedFeed;

// Whether the NTP should initially be scrolled into the feed.
@property(nonatomic, assign) BOOL shouldScrollIntoFeed;

@end

#endif // IOS_CHROME_BROWSER_UI_NTP_NEW_TAB_PAGE_CONFIGURING_H_
5 changes: 4 additions & 1 deletion ios/chrome/browser/ui/ntp/new_tab_page_coordinator.h
Expand Up @@ -7,7 +7,9 @@

#import "ios/chrome/browser/ui/coordinators/chrome_coordinator.h"

#import "ios/chrome/browser/discover_feed/feed_constants.h"
#import "ios/chrome/browser/ui/ntp/logo_animation_controller.h"
#import "ios/chrome/browser/ui/ntp/new_tab_page_configuring.h"

namespace web {
class WebState;
Expand All @@ -20,7 +22,8 @@ class WebState;

// Coordinator handling the NTP.
@interface NewTabPageCoordinator
: ChromeCoordinator <LogoAnimationControllerOwnerOwner>
: ChromeCoordinator <LogoAnimationControllerOwnerOwner,
NewTabPageConfiguring>

// ViewController associated with this coordinator.
@property(nonatomic, strong, readonly) UIViewController* viewController;
Expand Down

0 comments on commit b71ad7a

Please sign in to comment.