| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
.h" | 5 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
.h" |
| 6 | 6 |
| 7 #import <StoreKit/StoreKit.h> | 7 #import <StoreKit/StoreKit.h> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/user_metrics.h" | 10 #include "base/metrics/user_metrics.h" |
| 11 #include "base/metrics/user_metrics_action.h" | 11 #include "base/metrics/user_metrics_action.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" | 13 #include "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" |
| 14 #include "components/infobars/core/infobar_manager.h" | 14 #include "components/infobars/core/infobar_manager.h" |
| 15 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 15 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 16 #import "ios/chrome/browser/installation_notifier.h" | 16 #import "ios/chrome/browser/installation_notifier.h" |
| 17 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" | 17 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" |
| 18 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
_protocol.h" | 18 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
_protocol.h" |
| 19 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" | 19 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" |
| 20 #import "ios/chrome/browser/open_url_util.h" | 20 #import "ios/chrome/browser/open_url_util.h" |
| 21 #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h" |
| 22 #import "ios/chrome/browser/tabs/legacy_tab_helper.h" |
| 21 #import "ios/chrome/browser/tabs/tab.h" | 23 #import "ios/chrome/browser/tabs/tab.h" |
| 22 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 24 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 23 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad
ata.h" | 25 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad
ata.h" |
| 24 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types
.h" | 26 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types
.h" |
| 25 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white
list_manager.h" | 27 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white
list_manager.h" |
| 28 #include "ios/web/public/browser_state.h" |
| 26 #include "ios/web/public/web_state/web_state.h" | 29 #include "ios/web/public/web_state/web_state.h" |
| 27 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 30 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 28 #include "ios/web/public/web_thread.h" | 31 #include "ios/web/public/web_thread.h" |
| 29 #import "net/base/mac/url_conversions.h" | 32 #import "net/base/mac/url_conversions.h" |
| 30 | 33 |
| 31 #if !defined(__has_feature) || !__has_feature(objc_arc) | 34 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 32 #error "This file requires ARC support." | 35 #error "This file requires ARC support." |
| 33 #endif | 36 #endif |
| 34 | 37 |
| 35 using base::UserMetricsAction; | 38 using base::UserMetricsAction; |
| 36 | 39 |
| 37 @interface NativeAppNavigationController ()< | 40 @interface NativeAppNavigationController ()< |
| 38 CRWWebStateObserver, | 41 CRWWebStateObserver, |
| 39 NativeAppNavigationControllerProtocol> | 42 NativeAppNavigationControllerProtocol> |
| 40 // Shows a native app infobar by looking at the page's URL and by checking | 43 // Shows a native app infobar by looking at the page's URL and by checking |
| 41 // wheter that infobar should be bypassed or not. | 44 // wheter that infobar should be bypassed or not. |
| 42 - (void)showInfoBarIfNecessary; | 45 - (void)showInfoBarIfNecessary; |
| 43 | 46 |
| 44 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled| | 47 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled| |
| 45 - (NSMutableSet*)appsPossiblyBeingInstalled; | 48 - (NSMutableSet*)appsPossiblyBeingInstalled; |
| 46 | 49 |
| 47 // Records what type of infobar was opened. | 50 // Records what type of infobar was opened. |
| 48 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type | 51 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type |
| 49 onLinkNavigation:(BOOL)isLinkNavigation; | 52 onLinkNavigation:(BOOL)isLinkNavigation; |
| 50 | 53 |
| 51 @end | 54 @end |
| 52 | 55 |
| 53 @implementation NativeAppNavigationController { | 56 @implementation NativeAppNavigationController { |
| 54 // WebState provides access to the *TabHelper objects. This will eventually | 57 // WebState provides access to the *TabHelper objects. |
| 55 // replace the need to have |_tab| in this object. | |
| 56 web::WebState* _webState; | 58 web::WebState* _webState; |
| 57 // ImageFetcher needed to fetch the icons. | 59 // ImageFetcher needed to fetch the icons. |
| 58 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> _imageFetcher; | 60 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> _imageFetcher; |
| 59 // DEPRECATED: Tab hosting the infobar and is also used for accessing Tab | |
| 60 // states such as navigation manager and whether it is a pre-rendered tab. | |
| 61 // Use |webState| whenever possible. | |
| 62 __weak Tab* _tab; | |
| 63 id<NativeAppMetadata> _metadata; | 61 id<NativeAppMetadata> _metadata; |
| 64 // A set of appIds encoded as NSStrings. | 62 // A set of appIds encoded as NSStrings. |
| 65 NSMutableSet* _appsPossiblyBeingInstalled; | 63 NSMutableSet* _appsPossiblyBeingInstalled; |
| 66 // Allows this class to subscribe for CRWWebStateObserver callbacks. | 64 // Allows this class to subscribe for CRWWebStateObserver callbacks. |
| 67 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; | 65 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Designated initializer. Use this instead of -init. | 68 // Designated initializer. Use this instead of -init. |
| 71 - (instancetype)initWithWebState:(web::WebState*)webState | 69 - (instancetype)initWithWebState:(web::WebState*)webState { |
| 72 requestContextGetter:(net::URLRequestContextGetter*)context | |
| 73 tab:(Tab*)tab { | |
| 74 self = [super init]; | 70 self = [super init]; |
| 75 if (self) { | 71 if (self) { |
| 76 DCHECK(context); | |
| 77 _imageFetcher = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>( | |
| 78 context, web::WebThread::GetBlockingPool()); | |
| 79 DCHECK(webState); | 72 DCHECK(webState); |
| 80 _webState = webState; | 73 _webState = webState; |
| 81 // Allows |tab| to be nil for unit testing. If not nil, it should have the | 74 _imageFetcher = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>( |
| 82 // same webState. | 75 _webState->GetBrowserState()->GetRequestContext(), |
| 83 DCHECK(!tab || [tab webState] == webState); | 76 web::WebThread::GetBlockingPool()); |
| 84 _tab = tab; | |
| 85 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] init]; | 77 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] init]; |
| 86 _webStateObserver = | 78 _webStateObserver = |
| 87 base::MakeUnique<web::WebStateObserverBridge>(webState, self); | 79 base::MakeUnique<web::WebStateObserverBridge>(webState, self); |
| 88 } | 80 } |
| 89 return self; | 81 return self; |
| 90 } | 82 } |
| 91 | 83 |
| 92 - (void)copyStateFrom:(NativeAppNavigationController*)controller { | 84 - (void)copyStateFrom:(NativeAppNavigationController*)controller { |
| 93 DCHECK(controller); | 85 DCHECK(controller); |
| 94 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] | 86 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 registerForInstallationNotifications:self | 179 registerForInstallationNotifications:self |
| 188 withSelector:@selector(appDidInstall:) | 180 withSelector:@selector(appDidInstall:) |
| 189 forScheme:[_metadata anyScheme]]; | 181 forScheme:[_metadata anyScheme]]; |
| 190 NSString* appIdString = [self appId]; | 182 NSString* appIdString = [self appId]; |
| 191 // Defensively early return if native app metadata returns an nil string for | 183 // Defensively early return if native app metadata returns an nil string for |
| 192 // appId which can cause subsequent -addObject: to throw exceptions. | 184 // appId which can cause subsequent -addObject: to throw exceptions. |
| 193 if (![appIdString length]) | 185 if (![appIdString length]) |
| 194 return; | 186 return; |
| 195 DCHECK(![_appsPossiblyBeingInstalled containsObject:appIdString]); | 187 DCHECK(![_appsPossiblyBeingInstalled containsObject:appIdString]); |
| 196 [_appsPossiblyBeingInstalled addObject:appIdString]; | 188 [_appsPossiblyBeingInstalled addObject:appIdString]; |
| 197 // TODO(crbug.com/684063): Preferred method is to add a helper object to | 189 StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(_webState); |
| 198 // WebState and use the helper object to launch Store Kit. | 190 if (tabHelper) |
| 199 [_tab openAppStore:appIdString]; | 191 tabHelper->OpenAppStore(appIdString); |
| 200 } | 192 } |
| 201 | 193 |
| 202 - (void)launchApp:(const GURL&)URL { | 194 - (void)launchApp:(const GURL&)URL { |
| 203 // TODO(crbug.com/353957): Pass the ChromeIdentity to | 195 // TODO(crbug.com/353957): Pass the ChromeIdentity to |
| 204 // -launchURLWithURL:identity: | 196 // -launchURLWithURL:identity: |
| 205 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]); | 197 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]); |
| 206 if (launchURL.is_valid()) { | 198 if (launchURL.is_valid()) { |
| 207 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil); | 199 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil); |
| 208 } | 200 } |
| 209 } | 201 } |
| 210 | 202 |
| 211 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction { | 203 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction { |
| 212 [_metadata updateWithUserAction:userAction]; | 204 [_metadata updateWithUserAction:userAction]; |
| 213 } | 205 } |
| 214 | 206 |
| 215 #pragma mark - CRWWebStateObserver methods | 207 #pragma mark - CRWWebStateObserver methods |
| 216 | 208 |
| 217 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | 209 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 218 if (success && ![_tab isPrerenderTab]) | 210 Tab* tab = LegacyTabHelper::GetTabForWebState(_webState); |
| 211 if (success && ![tab isPrerenderTab]) |
| 219 [self showInfoBarIfNecessary]; | 212 [self showInfoBarIfNecessary]; |
| 220 } | 213 } |
| 221 | 214 |
| 222 - (void)webStateDestroyed:(web::WebState*)webState { | 215 - (void)webStateDestroyed:(web::WebState*)webState { |
| 223 _webState = nullptr; | 216 _webState = nullptr; |
| 224 _webStateObserver.reset(); | 217 _webStateObserver.reset(); |
| 225 } | 218 } |
| 226 | 219 |
| 227 #pragma mark - Private methods | 220 #pragma mark - Private methods |
| 228 | 221 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 243 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) { | 236 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) { |
| 244 appIDToRemove = appID; | 237 appIDToRemove = appID; |
| 245 *stop = YES; | 238 *stop = YES; |
| 246 } | 239 } |
| 247 }]; | 240 }]; |
| 248 DCHECK(appIDToRemove); | 241 DCHECK(appIDToRemove); |
| 249 [_appsPossiblyBeingInstalled removeObject:appIDToRemove]; | 242 [_appsPossiblyBeingInstalled removeObject:appIDToRemove]; |
| 250 } | 243 } |
| 251 | 244 |
| 252 @end | 245 @end |
| OLD | NEW |