Commit ebbf83c3 authored by mrefaat's avatar mrefaat Committed by Commit Bot

Disable iTunes StoerKit handling for incognito

Bug: 837670
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I0a45e4717bd2c974408c9247054dea6a0ee13a02
Reviewed-on: https://chromium-review.googlesource.com/1033028
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554837}
parent d7577c3d
...@@ -28,9 +28,11 @@ source_set("unit_tests") { ...@@ -28,9 +28,11 @@ source_set("unit_tests") {
":itunes_links", ":itunes_links",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/store_kit", "//ios/chrome/browser/store_kit",
"//ios/chrome/test/fakes", "//ios/chrome/test/fakes",
"//ios/web/public", "//ios/web/public",
"//ios/web/public/test:test",
"//ios/web/public/test/fakes", "//ios/web/public/test/fakes",
"//testing/gtest", "//testing/gtest",
"//third_party/ocmock", "//third_party/ocmock",
......
...@@ -32,10 +32,11 @@ enum class ITunesUrlsStoreKitHandlingResult { ...@@ -32,10 +32,11 @@ enum class ITunesUrlsStoreKitHandlingResult {
}; };
// TabHelper which handles navigation to iTunes links. // TabHelper which handles navigation to iTunes links.
// If a navigation to web page for a product in iTunes App Store happens, this // If a navigation to web page for a product in iTunes App Store happens while
// helper will use StoreKitTabHelper to present the information of that product. // in non off the record browsing mode, this helper will use StoreKitTabHelper
// The goal of this class is to workaround a bug where appstore website serves // to present the information of that product. The goal of this class is to
// the wrong content for itunes.apple.com pages, see http://crbug.com/623016. // workaround a bug where appstore website serves the wrong content for
// itunes.apple.com pages, see http://crbug.com/623016.
class ITunesLinksHandlerTabHelper class ITunesLinksHandlerTabHelper
: public web::WebStateObserver, : public web::WebStateObserver,
public web::WebStateUserData<ITunesLinksHandlerTabHelper> { public web::WebStateUserData<ITunesLinksHandlerTabHelper> {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/metrics/user_metrics_action.h" #include "base/metrics/user_metrics_action.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/store_kit/store_kit_tab_helper.h" #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/navigation_item.h" #import "ios/web/public/navigation_item.h"
#import "ios/web/public/navigation_manager.h" #import "ios/web/public/navigation_manager.h"
#import "ios/web/public/web_state/navigation_context.h" #import "ios/web/public/web_state/navigation_context.h"
...@@ -88,12 +89,17 @@ class ITunesLinksHandlerWebStatePolicyDecider ...@@ -88,12 +89,17 @@ class ITunesLinksHandlerWebStatePolicyDecider
bool ShouldAllowResponse(NSURLResponse* response, bool ShouldAllowResponse(NSURLResponse* response,
bool for_main_frame) override { bool for_main_frame) override {
// Don't allow rendering responses from URLs that can be handled by // Don't allow rendering responses from URLs that can be handled by
// iTunesLinksHandler unless it's on iframe. // iTunesLinksHandler unless it's on iframe or the browsing mode is off the
return !for_main_frame || !CanHandleUrl(net::GURLWithNSURL(response.URL)); // record.
return web_state()->GetBrowserState()->IsOffTheRecord() ||
!for_main_frame || !CanHandleUrl(net::GURLWithNSURL(response.URL));
} }
bool ShouldAllowRequest(NSURLRequest* request, bool ShouldAllowRequest(NSURLRequest* request,
ui::PageTransition transition) override { ui::PageTransition transition) override {
// Only consider blocking the request if it's not of the record mode.
if (web_state()->GetBrowserState()->IsOffTheRecord())
return true;
web::NavigationItem* pending_item = web::NavigationItem* pending_item =
web_state()->GetNavigationManager()->GetPendingItem(); web_state()->GetNavigationManager()->GetPendingItem();
...@@ -148,6 +154,10 @@ ITunesLinksHandlerTabHelper::ITunesLinksHandlerTabHelper( ...@@ -148,6 +154,10 @@ ITunesLinksHandlerTabHelper::ITunesLinksHandlerTabHelper(
void ITunesLinksHandlerTabHelper::DidFinishNavigation( void ITunesLinksHandlerTabHelper::DidFinishNavigation(
web::WebState* web_state, web::WebState* web_state,
web::NavigationContext* navigation_context) { web::NavigationContext* navigation_context) {
// Don't handle iTunse URL in off the record mode.
if (web_state->GetBrowserState()->IsOffTheRecord())
return;
GURL url = navigation_context->GetUrl(); GURL url = navigation_context->GetUrl();
// Whenever a navigation to iTunes product url is finished, launch StoreKit. // Whenever a navigation to iTunes product url is finished, launch StoreKit.
if (ITunesLinksHandlerWebStatePolicyDecider::CanHandleUrl(url)) { if (ITunesLinksHandlerWebStatePolicyDecider::CanHandleUrl(url)) {
......
...@@ -8,11 +8,13 @@ ...@@ -8,11 +8,13 @@
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/test/histogram_tester.h" #include "base/test/histogram_tester.h"
#import "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/store_kit/store_kit_tab_helper.h" #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h"
#import "ios/chrome/test/fakes/fake_store_kit_launcher.h" #import "ios/chrome/test/fakes/fake_store_kit_launcher.h"
#import "ios/web/public/test/fakes/fake_navigation_context.h" #import "ios/web/public/test/fakes/fake_navigation_context.h"
#import "ios/web/public/test/fakes/test_navigation_manager.h" #import "ios/web/public/test/fakes/test_navigation_manager.h"
#import "ios/web/public/test/fakes/test_web_state.h" #import "ios/web/public/test/fakes/test_web_state.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#import "ios/web/public/web_state/web_state_policy_decider.h" #import "ios/web/public/web_state/web_state_policy_decider.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -30,7 +32,10 @@ const char kITunesURLsHandlingResultHistogram[] = ...@@ -30,7 +32,10 @@ const char kITunesURLsHandlingResultHistogram[] =
class ITunesLinksHandlerTabHelperTest : public PlatformTest { class ITunesLinksHandlerTabHelperTest : public PlatformTest {
protected: protected:
ITunesLinksHandlerTabHelperTest() ITunesLinksHandlerTabHelperTest()
: fake_launcher_([[FakeStoreKitLauncher alloc] init]) { : fake_launcher_([[FakeStoreKitLauncher alloc] init]),
chrome_browser_state_(TestChromeBrowserState::Builder().Build()) {
web_state_.SetBrowserState(
chrome_browser_state_->GetOriginalChromeBrowserState());
StoreKitTabHelper::CreateForWebState(&web_state_); StoreKitTabHelper::CreateForWebState(&web_state_);
std::unique_ptr<web::TestNavigationManager> test_navigation_manager = std::unique_ptr<web::TestNavigationManager> test_navigation_manager =
std::make_unique<web::TestNavigationManager>(); std::make_unique<web::TestNavigationManager>();
...@@ -79,9 +84,11 @@ class ITunesLinksHandlerTabHelperTest : public PlatformTest { ...@@ -79,9 +84,11 @@ class ITunesLinksHandlerTabHelperTest : public PlatformTest {
return web_state_.ShouldAllowResponse(response, main_frame); return web_state_.ShouldAllowResponse(response, main_frame);
} }
web::TestWebThreadBundle thread_bundle_;
web::TestNavigationManager* navigation_manager_; web::TestNavigationManager* navigation_manager_;
FakeStoreKitLauncher* fake_launcher_; FakeStoreKitLauncher* fake_launcher_;
web::TestWebState web_state_; web::TestWebState web_state_;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
base::HistogramTester histogram_tester_; base::HistogramTester histogram_tester_;
}; };
...@@ -109,6 +116,15 @@ TEST_F(ITunesLinksHandlerTabHelperTest, NonMatchingUrlsDoesntLaunchStoreKit) { ...@@ -109,6 +116,15 @@ TEST_F(ITunesLinksHandlerTabHelperTest, NonMatchingUrlsDoesntLaunchStoreKit) {
histogram_tester_.ExpectTotalCount(kITunesURLsHandlingResultHistogram, 0); histogram_tester_.ExpectTotalCount(kITunesURLsHandlingResultHistogram, 0);
} }
// Verifies that iTunes URLs are not handled when in off the record mode.
TEST_F(ITunesLinksHandlerTabHelperTest, NoHandlingInOffTheRecordMode) {
std::string url = "http://itunes.apple.com/us/app/app_name/id123";
EXPECT_TRUE(VerifyStoreKitLaunched(url));
web_state_.SetBrowserState(
chrome_browser_state_->GetOffTheRecordChromeBrowserState());
EXPECT_FALSE(VerifyStoreKitLaunched(url));
}
// Verifies that navigating to URLs for a product hosted on iTunes AppStore // Verifies that navigating to URLs for a product hosted on iTunes AppStore
// with supported media type launches storekit. // with supported media type launches storekit.
TEST_F(ITunesLinksHandlerTabHelperTest, MatchingUrlsLaunchesStoreKit) { TEST_F(ITunesLinksHandlerTabHelperTest, MatchingUrlsLaunchesStoreKit) {
......
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