Commit 52f3b67e authored by Nazerke's avatar Nazerke Committed by Commit Bot

[iOS][tabmodel] Remove TabModel use in UserActivityHandler.

This CL removes the use of TabModel in UserActivityHandler which was used
for getting WebStateList; and uses a browser to get WebStateList from
BrowserInterfaceProvider.

Change-Id: I2422071eccd7a5429b195e1b21be551f3bf48fce
Bug: 1049845, 1045575
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2153027Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Nazerke Kalidolda <nazerke@google.com>
Cr-Commit-Position: refs/heads/master@{#762788}
parent 3750b106
......@@ -386,8 +386,7 @@ initWithBrowserLauncher:(id<BrowserLauncher>)browserLauncher
[UserActivityHandler
handleStartupParametersWithTabOpener:tabOpener
startupInformation:_startupInformation
interfaceProvider:_browserLauncher
.interfaceProvider];
browserState:currentInterface.browserState];
} else if ([tabOpener shouldOpenNTPTabOnActivationOfTabModel:currentInterface
.tabModel]) {
// Opens an NTP if needed.
......
......@@ -66,11 +66,10 @@ namespace {
typedef BOOL (^DecisionBlock)(id self);
// A block that takes the arguments of UserActivityHandler's
// +handleStartupParametersWithTabOpener.
typedef void (^HandleStartupParam)(
id self,
id<TabOpening> tabOpener,
id<StartupInformation> startupInformation,
id<BrowserInterfaceProvider> interfaceProvider);
typedef void (^HandleStartupParam)(id self,
id<TabOpening> tabOpener,
id<StartupInformation> startupInformation,
ChromeBrowserState* browserState);
class FakeAppDistributionProvider : public AppDistributionProvider {
public:
......@@ -226,20 +225,19 @@ class AppStateTest : public BlockCleanupTest {
void swizzleHandleStartupParameters(
id<TabOpening> expectedTabOpener,
id<BrowserInterfaceProvider> expectedInterfaceProvider) {
handle_startup_swizzle_block_ =
^(id self, id<TabOpening> tabOpener,
id<StartupInformation> startupInformation,
id<BrowserInterfaceProvider> interfaceProvider) {
ASSERT_EQ(startup_information_mock_, startupInformation);
ASSERT_EQ(expectedTabOpener, tabOpener);
ASSERT_EQ(expectedInterfaceProvider, interfaceProvider);
};
ChromeBrowserState* expectedBrowserState) {
handle_startup_swizzle_block_ = ^(id self, id<TabOpening> tabOpener,
id<StartupInformation> startupInformation,
ChromeBrowserState* browserState) {
ASSERT_EQ(startup_information_mock_, startupInformation);
ASSERT_EQ(expectedTabOpener, tabOpener);
ASSERT_EQ(expectedBrowserState, browserState);
};
handle_startup_swizzler_.reset(new ScopedBlockSwizzler(
[UserActivityHandler class],
@selector(handleStartupParametersWithTabOpener:
startupInformation:interfaceProvider:),
startupInformation:browserState:),
handle_startup_swizzle_block_));
}
......@@ -584,7 +582,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) {
interfaceProvider.mainInterface.browserState = getBrowserState();
// Swizzle Startup Parameters.
swizzleHandleStartupParameters(tabOpener, interfaceProvider);
swizzleHandleStartupParameters(tabOpener, getBrowserState());
UIWindow* window = [[UIWindow alloc] init];
AppState* appState = getAppStateWithOpenNTPAndIncognitoBlock(NO, window);
......
......@@ -66,7 +66,7 @@ const char* const kUMAMobileSessionStartFromAppsHistogram =
// TODO(crbug.com/935019): Exacly the same copy of this code is present in
// +[UserAcrtivityHandler
// handleStartupParametersWithTabOpener:startupInformation:interfaceProvider:]
// handleStartupParametersWithTabOpener:startupInformation:browserState:]
GURL URL;
GURL virtualURL;
......
......@@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>
@protocol BrowserInterfaceProvider;
class ChromeBrowserState;
@protocol StartupInformation;
@protocol TabOpening;
......@@ -42,8 +43,7 @@
+ (void)handleStartupParametersWithTabOpener:(id<TabOpening>)tabOpener
startupInformation:
(id<StartupInformation>)startupInformation
interfaceProvider:
(id<BrowserInterfaceProvider>)interfaceProvider;
browserState:(ChromeBrowserState*)browserState;
@end
......
......@@ -23,10 +23,13 @@
#import "ios/chrome/app/spotlight/actions_spotlight_manager.h"
#import "ios/chrome/app/spotlight/spotlight_util.h"
#include "ios/chrome/app/startup/chrome_app_startup_parameters.h"
#import "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/main/browser_list.h"
#import "ios/chrome/browser/main/browser_list_factory.h"
#include "ios/chrome/browser/metrics/first_user_action_recorder.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/u2f/u2f_tab_helper.h"
#import "ios/chrome/browser/ui/main/browser_interface_provider.h"
#import "ios/chrome/browser/url_loading/image_search_param_generator.h"
......@@ -58,7 +61,7 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
startupInformation:(id<StartupInformation>)startupInformation;
// Routes Universal 2nd Factor (U2F) callback to the correct Tab.
+ (void)routeU2FURL:(const GURL&)URL
interfaceProvider:(id<BrowserInterfaceProvider>)interfaceProvider;
browserState:(ChromeBrowserState*)browserState;
@end
@implementation UserActivityHandler
......@@ -233,7 +236,8 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
[UserActivityHandler
handleStartupParametersWithTabOpener:tabOpener
startupInformation:startupInformation
interfaceProvider:interfaceProvider];
browserState:interfaceProvider.currentInterface
.browserState];
}
completionHandler(handledShortcutItem);
}
......@@ -248,8 +252,7 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
+ (void)handleStartupParametersWithTabOpener:(id<TabOpening>)tabOpener
startupInformation:
(id<StartupInformation>)startupInformation
interfaceProvider:
(id<BrowserInterfaceProvider>)interfaceProvider {
browserState:(ChromeBrowserState*)browserState {
DCHECK([startupInformation startupParameters]);
// Do not load the external URL if the user has not accepted the terms of
// service. This corresponds to the case when the user installed Chrome,
......@@ -261,8 +264,7 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
// Check if it's an U2F call. If so, route it to correct tab.
// If not, open or reuse tab in main BVC.
if (U2FTabHelper::IsU2FUrl(externalURL)) {
[UserActivityHandler routeU2FURL:externalURL
interfaceProvider:interfaceProvider];
[UserActivityHandler routeU2FURL:externalURL browserState:browserState];
// It's OK to clear startup parameters here because routeU2FURL works
// synchronously.
[startupInformation setStartupParameters:nil];
......@@ -295,8 +297,7 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
if (startupInformation.startupParameters.imageSearchData) {
TemplateURLService* templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(
interfaceProvider.mainInterface.browserState);
ios::TemplateURLServiceFactory::GetForBrowserState(browserState);
NSData* imageData = startupInformation.startupParameters.imageSearchData;
web::NavigationManager::WebLoadParams webLoadParams =
......@@ -308,8 +309,7 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
NSString* query = startupInformation.startupParameters.textQuery;
TemplateURLService* templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(
interfaceProvider.mainInterface.browserState);
ios::TemplateURLServiceFactory::GetForBrowserState(browserState);
const TemplateURL* defaultURL =
templateURLService->GetDefaultSearchProvider();
......@@ -388,25 +388,31 @@ NSString* const kShortcutQRScanner = @"OpenQRScanner";
}
+ (void)routeU2FURL:(const GURL&)URL
interfaceProvider:(id<BrowserInterfaceProvider>)interfaceProvider {
browserState:(ChromeBrowserState*)browserState {
DCHECK(browserState);
// Retrieve the designated TabID from U2F URL.
NSString* tabID = U2FTabHelper::GetTabIdFromU2FUrl(URL);
if (!tabID) {
return;
}
// Iterate through mainTabModel and OTRTabModel to find the corresponding tab.
NSArray* tabModels = @[
interfaceProvider.mainInterface.tabModel,
interfaceProvider.incognitoInterface.tabModel
];
for (TabModel* tabModel in tabModels) {
WebStateList* webStateList = tabModel.webStateList;
// Iterate through regular Browser and OTR Browser to find the corresponding
// tab.
BrowserList* browserList =
BrowserListFactory::GetForBrowserState(browserState);
std::set<Browser*> regularBrowsers = browserList->AllRegularBrowsers();
std::set<Browser*> otrBrowsers = browserList->AllIncognitoBrowsers();
std::set<Browser*> browsers(regularBrowsers);
browsers.insert(otrBrowsers.begin(), otrBrowsers.end());
for (Browser* browser : browsers) {
WebStateList* webStateList = browser->GetWebStateList();
for (int index = 0; index < webStateList->count(); ++index) {
web::WebState* webState = webStateList->GetWebStateAt(index);
NSString* currentTabID = TabIdTabHelper::FromWebState(webState)->tab_id();
if ([currentTabID isEqualToString:tabID]) {
U2FTabHelper::FromWebState(webState)->EvaluateU2FResult(URL);
return;
}
}
}
......
......@@ -12,6 +12,7 @@
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_command_line.h"
#import "base/test/task_environment.h"
#include "components/handoff/handoff_utility.h"
#import "ios/chrome/app/app_startup_parameters.h"
#include "ios/chrome/app/application_delegate/fake_startup_information.h"
......@@ -23,9 +24,12 @@
#include "ios/chrome/app/main_controller.h"
#include "ios/chrome/app/spotlight/actions_spotlight_manager.h"
#import "ios/chrome/app/spotlight/spotlight_util.h"
#import "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/chrome_switches.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/main/browser_list.h"
#import "ios/chrome/browser/main/browser_list_factory.h"
#import "ios/chrome/browser/main/test_browser.h"
#import "ios/chrome/browser/u2f/u2f_tab_helper.h"
#import "ios/chrome/browser/ui/main/test/stub_browser_interface_provider.h"
#import "ios/chrome/browser/url_loading/url_loading_params.h"
......@@ -70,43 +74,6 @@ class FakeU2FTabHelper : public U2FTabHelper {
DISALLOW_COPY_AND_ASSIGN(FakeU2FTabHelper);
};
#pragma mark - TabModel Mock
// TabModel mock for using in UserActivity tests.
@interface UserActivityHandlerTabModelMock : NSObject
@end
@implementation UserActivityHandlerTabModelMock {
FakeWebStateListDelegate _webStateListDelegate;
std::unique_ptr<WebStateList> _webStateList;
}
- (instancetype)init {
if ((self = [super init])) {
_webStateList = std::make_unique<WebStateList>(&_webStateListDelegate);
}
return self;
}
- (web::WebState*)addWebState {
auto testWebState = std::make_unique<web::TestWebState>();
TabIdTabHelper::CreateForWebState(testWebState.get());
FakeU2FTabHelper::CreateForWebState(testWebState.get());
web::WebState* returnWebState = testWebState.get();
_webStateList->InsertWebState(0, std::move(testWebState),
WebStateList::INSERT_NO_FLAGS,
WebStateOpener());
return returnWebState;
}
- (WebStateList*)webStateList {
return _webStateList.get();
}
@end
#pragma mark - Test class.
// A block that takes as arguments the caller and the arguments from
......@@ -129,7 +96,7 @@ class UserActivityHandlerTest : public PlatformTest {
user_activity_handler_swizzler_.reset(new ScopedBlockSwizzler(
[UserActivityHandler class],
@selector(handleStartupParametersWithTabOpener:
startupInformation:interfaceProvider:),
startupInformation:browserState:),
swizzle_block_));
}
......@@ -560,14 +527,17 @@ TEST_F(UserActivityHandlerTest, HandleStartupParamsWithExternalFile) {
MockTabOpener* tabOpener = [[MockTabOpener alloc] init];
// The test will fail is a method of this object is called.
id interfaceProviderMock =
[OCMockObject mockForProtocol:@protocol(BrowserInterfaceProvider)];
// id interfaceProviderMock =
// [OCMockObject mockForProtocol:@protocol(BrowserInterfaceProvider)];
StubBrowserInterfaceProvider* interfaceProvider =
[[StubBrowserInterfaceProvider alloc] init];
// Action.
[UserActivityHandler
handleStartupParametersWithTabOpener:tabOpener
startupInformation:startupInformationMock
interfaceProvider:interfaceProviderMock];
browserState:interfaceProvider.currentInterface
.browserState];
[tabOpener completionBlock]();
// Tests.
......@@ -599,14 +569,17 @@ TEST_F(UserActivityHandlerTest, HandleStartupParamsNonU2F) {
MockTabOpener* tabOpener = [[MockTabOpener alloc] init];
// The test will fail is a method of this object is called.
id interfaceProviderMock =
[OCMockObject mockForProtocol:@protocol(BrowserInterfaceProvider)];
// id interfaceProviderMock =
// [OCMockObject mockForProtocol:@protocol(BrowserInterfaceProvider)];
StubBrowserInterfaceProvider* interfaceProvider =
[[StubBrowserInterfaceProvider alloc] init];
// Action.
[UserActivityHandler
handleStartupParametersWithTabOpener:tabOpener
startupInformation:startupInformationMock
interfaceProvider:interfaceProviderMock];
browserState:interfaceProvider.currentInterface
.browserState];
[tabOpener completionBlock]();
// Tests.
......@@ -620,10 +593,32 @@ TEST_F(UserActivityHandlerTest, HandleStartupParamsNonU2F) {
// Tests that handleStartupParameters with a U2F url opens in the correct tab.
TEST_F(UserActivityHandlerTest, HandleStartupParamsU2F) {
// Setup.
UserActivityHandlerTabModelMock* mockTabModel =
[[UserActivityHandlerTabModelMock alloc] init];
web::WebState* web_state = [mockTabModel addWebState];
id tabModel = static_cast<id>(mockTabModel);
base::test::TaskEnvironment task_enviroment_;
TestChromeBrowserState::Builder test_cbs_builder;
std::unique_ptr<ChromeBrowserState> browser_state_ = test_cbs_builder.Build();
FakeWebStateListDelegate _webStateListDelegate;
std::unique_ptr<WebStateList> web_state_list_ =
std::make_unique<WebStateList>(&_webStateListDelegate);
auto testWebState = std::make_unique<web::TestWebState>();
TabIdTabHelper::CreateForWebState(testWebState.get());
FakeU2FTabHelper::CreateForWebState(testWebState.get());
web::WebState* web_state = testWebState.get();
web_state_list_->InsertWebState(0, std::move(testWebState),
WebStateList::INSERT_NO_FLAGS,
WebStateOpener());
std::unique_ptr<Browser> browser_ = std::make_unique<TestBrowser>(
browser_state_.get(), web_state_list_.get());
std::unique_ptr<Browser> otr_browser_ = std::make_unique<TestBrowser>(
browser_state_->GetOffTheRecordChromeBrowserState(),
web_state_list_.get());
BrowserList* browser_list_ =
BrowserListFactory::GetForBrowserState(browser_state_.get());
browser_list_->AddBrowser(browser_.get());
std::string urlRepresentation = base::StringPrintf(
"chromium://u2f-callback?isU2F=1&tabID=%s",
......@@ -642,8 +637,9 @@ TEST_F(UserActivityHandlerTest, HandleStartupParamsU2F) {
StubBrowserInterfaceProvider* interfaceProvider =
[[StubBrowserInterfaceProvider alloc] init];
interfaceProvider.mainInterface.tabModel = tabModel;
interfaceProvider.incognitoInterface.tabModel = tabModel;
interfaceProvider.mainInterface.browserState = browser_state_.get();
interfaceProvider.incognitoInterface.browserState =
browser_state_->GetOffTheRecordChromeBrowserState();
MockTabOpener* tabOpener = [[MockTabOpener alloc] init];
......@@ -651,7 +647,8 @@ TEST_F(UserActivityHandlerTest, HandleStartupParamsU2F) {
[UserActivityHandler
handleStartupParametersWithTabOpener:tabOpener
startupInformation:startupInformationMock
interfaceProvider:interfaceProvider];
browserState:interfaceProvider.currentInterface
.browserState];
// Tests.
EXPECT_OCMOCK_VERIFY(startupInformationMock);
......@@ -687,15 +684,15 @@ TEST_F(UserActivityHandlerTest, PerformActionForShortcutItemWithRealShortcut) {
// The test will fail is a method of those objects is called.
id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)];
id interfaceProviderMock =
[OCMockObject mockForProtocol:@protocol(BrowserInterfaceProvider)];
StubBrowserInterfaceProvider* interfaceProvider =
[[StubBrowserInterfaceProvider alloc] init];
// Action.
[UserActivityHandler performActionForShortcutItem:shortcut
completionHandler:getCompletionHandler()
tabOpener:tabOpenerMock
startupInformation:fakeStartupInformation
interfaceProvider:interfaceProviderMock];
interfaceProvider:interfaceProvider];
// Tests.
EXPECT_EQ(gurlNewTab,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment