Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: ios/chrome/browser/native_app_launcher/native_app_navigation_controller.mm

Issue 2731443003: Removed CRWWebController dependency from NativeAppNavigationController. (Closed)
Patch Set: Addressed review comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" 18 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h"
19 #import "ios/chrome/browser/open_url_util.h" 19 #import "ios/chrome/browser/open_url_util.h"
20 #import "ios/chrome/browser/tabs/tab.h" 20 #import "ios/chrome/browser/tabs/tab.h"
21 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" 21 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
22 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h" 22 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h"
23 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types .h" 23 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types .h"
24 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h" 24 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h"
25 #include "ios/web/public/web_state/web_state.h" 25 #include "ios/web/public/web_state/web_state.h"
26 #import "ios/web/public/web_state/web_state_observer_bridge.h"
26 #include "ios/web/public/web_thread.h" 27 #include "ios/web/public/web_thread.h"
27 #import "ios/web/web_state/ui/crw_web_controller.h"
28 #import "net/base/mac/url_conversions.h" 28 #import "net/base/mac/url_conversions.h"
29 29
30 #if !defined(__has_feature) || !__has_feature(objc_arc) 30 #if !defined(__has_feature) || !__has_feature(objc_arc)
31 #error "This file requires ARC support." 31 #error "This file requires ARC support."
32 #endif 32 #endif
33 33
34 using base::UserMetricsAction; 34 using base::UserMetricsAction;
35 35
36 @interface NativeAppNavigationController () 36 @interface NativeAppNavigationController ()<
37 CRWWebStateObserver,
38 NativeAppNavigationControllerProtocol>
37 // Shows a native app infobar by looking at the page's URL and by checking 39 // Shows a native app infobar by looking at the page's URL and by checking
38 // wheter that infobar should be bypassed or not. 40 // wheter that infobar should be bypassed or not.
39 - (void)showInfoBarIfNecessary; 41 - (void)showInfoBarIfNecessary;
40 42
41 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled| 43 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled|
42 - (NSMutableSet*)appsPossiblyBeingInstalled; 44 - (NSMutableSet*)appsPossiblyBeingInstalled;
43 45
44 // Records what type of infobar was opened. 46 // Records what type of infobar was opened.
45 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type 47 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type
46 onLinkNavigation:(BOOL)isLinkNavigation; 48 onLinkNavigation:(BOOL)isLinkNavigation;
47 49
48 @end 50 @end
49 51
50 @implementation NativeAppNavigationController { 52 @implementation NativeAppNavigationController {
51 // WebState provides access to the *TabHelper objects. This will eventually 53 // WebState provides access to the *TabHelper objects. This will eventually
52 // replace the need to have |_tab| in this object. 54 // replace the need to have |_tab| in this object.
53 web::WebState* _webState; 55 web::WebState* _webState;
54 // ImageFetcher needed to fetch the icons. 56 // ImageFetcher needed to fetch the icons.
55 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> _imageFetcher; 57 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> _imageFetcher;
56 // DEPRECATED: Tab hosting the infobar and is also used for accessing Tab 58 // DEPRECATED: Tab hosting the infobar and is also used for accessing Tab
57 // states such as navigation manager and whether it is a pre-rendered tab. 59 // states such as navigation manager and whether it is a pre-rendered tab.
58 // Use |webState| whenever possible. 60 // Use |webState| whenever possible.
59 __weak Tab* _tab; 61 __weak Tab* _tab;
60 id<NativeAppMetadata> _metadata; 62 id<NativeAppMetadata> _metadata;
61 // A set of appIds encoded as NSStrings. 63 // A set of appIds encoded as NSStrings.
62 NSMutableSet* _appsPossiblyBeingInstalled; 64 NSMutableSet* _appsPossiblyBeingInstalled;
65 // Allows this class to subscribe for CRWWebStateObserver callbacks.
66 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver;
63 } 67 }
64 68
65 // Designated initializer. Use this instead of -init. 69 // Designated initializer. Use this instead of -init.
66 - (instancetype)initWithWebState:(web::WebState*)webState 70 - (instancetype)initWithWebState:(web::WebState*)webState
67 requestContextGetter:(net::URLRequestContextGetter*)context 71 requestContextGetter:(net::URLRequestContextGetter*)context
68 tab:(Tab*)tab { 72 tab:(Tab*)tab {
69 self = [super init]; 73 self = [super init];
70 if (self) { 74 if (self) {
71 DCHECK(context); 75 DCHECK(context);
72 _imageFetcher = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>( 76 _imageFetcher = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>(
73 context, web::WebThread::GetBlockingPool()); 77 context, web::WebThread::GetBlockingPool());
74 DCHECK(webState); 78 DCHECK(webState);
75 _webState = webState; 79 _webState = webState;
76 // Allows |tab| to be nil for unit testing. If not nil, it should have the 80 // Allows |tab| to be nil for unit testing. If not nil, it should have the
77 // same webState. 81 // same webState.
78 DCHECK(!tab || [tab webState] == webState); 82 DCHECK(!tab || [tab webState] == webState);
79 _tab = tab; 83 _tab = tab;
80 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] init]; 84 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] init];
85 _webStateObserver.reset(new web::WebStateObserverBridge(webState, self));
michaeldo 2017/03/04 00:35:38 Use base::MakeUnique here instead of new? _webSta
Eugene But (OOO till 7-30) 2017/03/04 01:01:50 Done.
81 } 86 }
82 return self; 87 return self;
83 } 88 }
84 89
85 - (void)copyStateFrom:(NativeAppNavigationController*)controller { 90 - (void)copyStateFrom:(NativeAppNavigationController*)controller {
86 DCHECK(controller); 91 DCHECK(controller);
87 _appsPossiblyBeingInstalled = [[NSMutableSet alloc] 92 _appsPossiblyBeingInstalled = [[NSMutableSet alloc]
88 initWithSet:[controller appsPossiblyBeingInstalled]]; 93 initWithSet:[controller appsPossiblyBeingInstalled]];
89 for (NSString* appIdString in _appsPossiblyBeingInstalled) { 94 for (NSString* appIdString in _appsPossiblyBeingInstalled) {
90 DCHECK([appIdString isKindOfClass:[NSString class]]); 95 DCHECK([appIdString isKindOfClass:[NSString class]]);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 break; 157 break;
153 case NATIVE_APP_OPEN_POLICY_CONTROLLER: 158 case NATIVE_APP_OPEN_POLICY_CONTROLLER:
154 base::RecordAction(UserMetricsAction("MobileGALOpenPolicyInfoBar")); 159 base::RecordAction(UserMetricsAction("MobileGALOpenPolicyInfoBar"));
155 break; 160 break;
156 default: 161 default:
157 NOTREACHED(); 162 NOTREACHED();
158 break; 163 break;
159 } 164 }
160 } 165 }
161 166
162 #pragma mark - 167 #pragma mark - NativeAppNavigationControllerProtocol methods
163 #pragma mark NativeAppNavigationControllerProtocol methods
164 168
165 - (NSString*)appId { 169 - (NSString*)appId {
166 return [_metadata appId]; 170 return [_metadata appId];
167 } 171 }
168 172
169 - (NSString*)appName { 173 - (NSString*)appName {
170 return [_metadata appName]; 174 return [_metadata appName];
171 } 175 }
172 176
173 - (void)fetchSmallIconWithCompletionBlock:(void (^)(UIImage*))block { 177 - (void)fetchSmallIconWithCompletionBlock:(void (^)(UIImage*))block {
(...skipping 25 matching lines...) Expand all
199 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]); 203 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]);
200 if (launchURL.is_valid()) { 204 if (launchURL.is_valid()) {
201 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil); 205 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil);
202 } 206 }
203 } 207 }
204 208
205 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction { 209 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction {
206 [_metadata updateWithUserAction:userAction]; 210 [_metadata updateWithUserAction:userAction];
207 } 211 }
208 212
209 #pragma mark - 213 #pragma mark - CRWWebStateObserver methods
210 #pragma mark CRWWebControllerObserver methods
211 214
212 - (void)pageLoaded:(CRWWebController*)webController { 215 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
213 if (![_tab isPrerenderTab]) 216 if (success && ![_tab isPrerenderTab])
214 [self showInfoBarIfNecessary]; 217 [self showInfoBarIfNecessary];
215 } 218 }
216 219
217 - (void)webControllerWillClose:(CRWWebController*)webController { 220 - (void)webStateDestroyed:(web::WebState*)webState {
218 [webController removeObserver:self]; 221 _webState = nullptr;
michaeldo 2017/03/04 00:35:38 Shouldn't we also cleanup/reset |_webStateObserver
Eugene But (OOO till 7-30) 2017/03/04 01:01:50 Added _webStateObserver.reset(). I don't really wa
michaeldo 2017/03/04 01:26:40 good call :)
219 } 222 }
220 223
224 #pragma mark - Private methods
225
221 - (void)appDidInstall:(NSNotification*)notification { 226 - (void)appDidInstall:(NSNotification*)notification {
222 [self removeAppFromNotification:notification]; 227 [self removeAppFromNotification:notification];
223 [self showInfoBarIfNecessary]; 228 [self showInfoBarIfNecessary];
224 } 229 }
225 230
226 - (void)removeAppFromNotification:(NSNotification*)notification { 231 - (void)removeAppFromNotification:(NSNotification*)notification {
227 DCHECK([[notification object] isKindOfClass:[InstallationNotifier class]]); 232 DCHECK([[notification object] isKindOfClass:[InstallationNotifier class]]);
228 NSString* schemeOfInstalledApp = [notification name]; 233 NSString* schemeOfInstalledApp = [notification name];
229 __block NSString* appIDToRemove = nil; 234 __block NSString* appIDToRemove = nil;
230 [_appsPossiblyBeingInstalled 235 [_appsPossiblyBeingInstalled
231 enumerateObjectsUsingBlock:^(id appID, BOOL* stop) { 236 enumerateObjectsUsingBlock:^(id appID, BOOL* stop) {
232 NSURL* appURL = 237 NSURL* appURL =
233 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager() 238 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager()
234 schemeForAppId:appID]; 239 schemeForAppId:appID];
235 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) { 240 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) {
236 appIDToRemove = appID; 241 appIDToRemove = appID;
237 *stop = YES; 242 *stop = YES;
238 } 243 }
239 }]; 244 }];
240 DCHECK(appIDToRemove); 245 DCHECK(appIDToRemove);
241 [_appsPossiblyBeingInstalled removeObject:appIDToRemove]; 246 [_appsPossiblyBeingInstalled removeObject:appIDToRemove];
242 } 247 }
243 248
244 @end 249 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698