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/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
10 #include "base/metrics/user_metrics_action.h" | 10 #include "base/metrics/user_metrics_action.h" |
11 #include "components/infobars/core/infobar_manager.h" | 11 #include "components/infobars/core/infobar_manager.h" |
12 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | |
12 #import "ios/chrome/browser/installation_notifier.h" | 13 #import "ios/chrome/browser/installation_notifier.h" |
13 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" | 14 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" |
15 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_utils.h" | |
14 #import "ios/chrome/browser/open_url_util.h" | 16 #import "ios/chrome/browser/open_url_util.h" |
15 #import "ios/chrome/browser/tabs/tab.h" | 17 #import "ios/chrome/browser/tabs/tab.h" |
16 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 18 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
17 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h" | 19 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h" |
18 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types .h" | 20 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_types .h" |
19 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h" | 21 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h" |
20 #import "ios/web/navigation/navigation_manager_impl.h" | 22 #include "ios/web/public/web_state/web_state.h" |
Eugene But (OOO till 7-30)
2017/01/26 01:04:38
s/include/import
pkl (ping after 24h if needed)
2017/01/26 02:26:28
Since web_state.h defines a C++ class, isn't #incl
Eugene But (OOO till 7-30)
2017/01/26 03:10:02
web_state.h is an Objective-C header (it has @clas
| |
21 #include "ios/web/public/navigation_item.h" | |
22 #import "net/base/mac/url_conversions.h" | 23 #import "net/base/mac/url_conversions.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
24 | 25 |
25 using base::UserMetricsAction; | 26 using base::UserMetricsAction; |
26 | 27 |
27 @interface NativeAppNavigationController () | 28 @interface NativeAppNavigationController () |
28 // Shows a native app infobar by looking at the page's URL and by checking | 29 // Shows a native app infobar by looking at the page's URL and by checking |
29 // wheter that infobar should be bypassed or not. | 30 // wheter that infobar should be bypassed or not. |
30 - (void)showInfoBarIfNecessary; | 31 - (void)showInfoBarIfNecessary; |
31 | 32 |
32 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled| | 33 // Returns a pointer to the NSMutableSet of |_appsPossiblyBeingInstalled| |
33 - (NSMutableSet*)appsPossiblyBeingInstalled; | 34 - (NSMutableSet*)appsPossiblyBeingInstalled; |
34 | 35 |
35 // Records what type of infobar was opened. | 36 // Records what type of infobar was opened. |
36 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type | 37 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type |
37 onLinkNavigation:(BOOL)isLinkNavigation; | 38 onLinkNavigation:(BOOL)isLinkNavigation; |
38 | 39 |
39 // Returns whether the current state is Link Navigation in the sense of Native | |
40 // App Launcher, i.e. a navigation caused by an explicit user action in the | |
41 // rectangle of the web content area. | |
42 - (BOOL)isLinkNavigation; | |
43 | |
44 @end | 40 @end |
45 | 41 |
46 @implementation NativeAppNavigationController { | 42 @implementation NativeAppNavigationController { |
43 // WebState provides access to the *TabHelper objects. This will eventually | |
44 // replace the need to have |_tab| in this object. | |
45 web::WebState* _webState; | |
47 // A reference to the URLRequestContextGetter needed to fetch icons. | 46 // A reference to the URLRequestContextGetter needed to fetch icons. |
48 scoped_refptr<net::URLRequestContextGetter> _requestContextGetter; | 47 scoped_refptr<net::URLRequestContextGetter> _requestContextGetter; |
49 // Tab hosting the infobar. | 48 // DEPRECATED: Tab hosting the infobar and is also used for accessing Tab |
49 // states such as navigation manager and whether it is a pre-rendered tab. | |
50 // Use |webState| whenever possible. | |
50 __unsafe_unretained Tab* _tab; // weak | 51 __unsafe_unretained Tab* _tab; // weak |
51 base::scoped_nsprotocol<id<NativeAppMetadata>> _metadata; | 52 base::scoped_nsprotocol<id<NativeAppMetadata>> _metadata; |
52 // A set of appIds encoded as NSStrings. | 53 // A set of appIds encoded as NSStrings. |
53 base::scoped_nsobject<NSMutableSet> _appsPossiblyBeingInstalled; | 54 base::scoped_nsobject<NSMutableSet> _appsPossiblyBeingInstalled; |
54 } | 55 } |
55 | 56 |
56 // This prevents incorrect initialization of this object. | |
57 - (id)init { | |
58 NOTREACHED(); | |
59 return nil; | |
60 } | |
61 | |
62 // Designated initializer. Use this instead of -init. | 57 // Designated initializer. Use this instead of -init. |
63 - (id)initWithRequestContextGetter:(net::URLRequestContextGetter*)context | 58 - (instancetype)initWithWebState:(web::WebState*)webState |
64 tab:(Tab*)tab { | 59 requestContextGetter:(net::URLRequestContextGetter*)context |
60 tab:(Tab*)tab { | |
65 self = [super init]; | 61 self = [super init]; |
66 if (self) { | 62 if (self) { |
67 DCHECK(context); | 63 DCHECK(context); |
68 _requestContextGetter = context; | 64 _requestContextGetter = context; |
69 // Allows |tab| to be nil for unit testing. | 65 DCHECK(webState); |
66 _webState = webState; | |
67 // Allows |tab| to be nil for unit testing. If not nil, it should have the | |
68 // same webState. | |
69 DCHECK(!tab || [tab webState] == webState); | |
70 _tab = tab; | 70 _tab = tab; |
71 _appsPossiblyBeingInstalled.reset([[NSMutableSet alloc] init]); | 71 _appsPossiblyBeingInstalled.reset([[NSMutableSet alloc] init]); |
72 } | 72 } |
73 return self; | 73 return self; |
74 } | 74 } |
75 | 75 |
76 - (void)copyStateFrom:(NativeAppNavigationController*)controller { | 76 - (void)copyStateFrom:(NativeAppNavigationController*)controller { |
77 DCHECK(controller); | 77 DCHECK(controller); |
78 _appsPossiblyBeingInstalled.reset([[NSMutableSet alloc] | 78 _appsPossiblyBeingInstalled.reset([[NSMutableSet alloc] |
79 initWithSet:[controller appsPossiblyBeingInstalled]]); | 79 initWithSet:[controller appsPossiblyBeingInstalled]]); |
(...skipping 14 matching lines...) Expand all Loading... | |
94 [[InstallationNotifier sharedInstance] unregisterForNotifications:self]; | 94 [[InstallationNotifier sharedInstance] unregisterForNotifications:self]; |
95 [super dealloc]; | 95 [super dealloc]; |
96 } | 96 } |
97 | 97 |
98 - (NSMutableSet*)appsPossiblyBeingInstalled { | 98 - (NSMutableSet*)appsPossiblyBeingInstalled { |
99 return _appsPossiblyBeingInstalled; | 99 return _appsPossiblyBeingInstalled; |
100 } | 100 } |
101 | 101 |
102 - (void)showInfoBarIfNecessary { | 102 - (void)showInfoBarIfNecessary { |
103 // Find a potential matching native app. | 103 // Find a potential matching native app. |
104 GURL pageURL = _tab.webState->GetLastCommittedURL(); | 104 GURL pageURL = _webState->GetLastCommittedURL(); |
105 _metadata.reset( | 105 _metadata.reset( |
106 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager() | 106 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager() |
107 newNativeAppForURL:pageURL]); | 107 newNativeAppForURL:pageURL]); |
108 if (!_metadata || [_metadata shouldBypassInfoBars]) | 108 if (!_metadata || [_metadata shouldBypassInfoBars]) |
109 return; | 109 return; |
110 | 110 |
111 // Select the infobar type. | 111 // Select the infobar type. |
112 NativeAppControllerType type; | 112 NativeAppControllerType type; |
113 if ([_metadata canOpenURL:pageURL]) { // App is installed. | 113 if ([_metadata canOpenURL:pageURL]) { // App is installed. |
114 type = [self isLinkNavigation] && ![_metadata shouldAutoOpenLinks] | 114 type = native_app_launcher::IsLinkNavigation(_webState) && |
115 ![_metadata shouldAutoOpenLinks] | |
115 ? NATIVE_APP_OPEN_POLICY_CONTROLLER | 116 ? NATIVE_APP_OPEN_POLICY_CONTROLLER |
116 : NATIVE_APP_LAUNCHER_CONTROLLER; | 117 : NATIVE_APP_LAUNCHER_CONTROLLER; |
117 } else { // App is not installed. | 118 } else { // App is not installed. |
118 // Check if the user already opened the store for this app. | 119 // Check if the user already opened the store for this app. |
119 if ([_appsPossiblyBeingInstalled containsObject:[_metadata appId]]) | 120 if ([_appsPossiblyBeingInstalled containsObject:[_metadata appId]]) |
120 return; | 121 return; |
121 type = NATIVE_APP_INSTALLER_CONTROLLER; | 122 type = NATIVE_APP_INSTALLER_CONTROLLER; |
122 } | 123 } |
123 // Inform the metadata that an infobar of |type| will be shown so that metrics | 124 // Inform the metadata that an infobar of |type| will be shown so that metrics |
124 // and ignored behavior can be handled. | 125 // and ignored behavior can be handled. |
125 [_metadata willBeShownInInfobarOfType:type]; | 126 [_metadata willBeShownInInfobarOfType:type]; |
126 // Display the proper infobar. | 127 // Display the proper infobar. |
127 infobars::InfoBarManager* infoBarManager = [_tab infoBarManager]; | 128 infobars::InfoBarManager* infoBarManager = |
129 InfoBarManagerImpl::FromWebState(_webState); | |
128 NativeAppInfoBarDelegate::Create(infoBarManager, self, pageURL, type); | 130 NativeAppInfoBarDelegate::Create(infoBarManager, self, pageURL, type); |
129 [self recordInfobarDisplayedOfType:type | 131 [self recordInfobarDisplayedOfType:type |
130 onLinkNavigation:[self isLinkNavigation]]; | 132 onLinkNavigation:native_app_launcher::IsLinkNavigation( |
Eugene But (OOO till 7-30)
2017/01/26 01:04:38
nit: Do you want to use local variable for native_
pkl (ping after 24h if needed)
2017/01/26 02:26:28
I think so since WebState is not going to change b
| |
133 _webState)]; | |
131 } | 134 } |
132 | 135 |
133 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type | 136 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type |
134 onLinkNavigation:(BOOL)isLinkNavigation { | 137 onLinkNavigation:(BOOL)isLinkNavigation { |
135 switch (type) { | 138 switch (type) { |
136 case NATIVE_APP_INSTALLER_CONTROLLER: | 139 case NATIVE_APP_INSTALLER_CONTROLLER: |
137 base::RecordAction( | 140 base::RecordAction( |
138 isLinkNavigation | 141 isLinkNavigation |
139 ? UserMetricsAction("MobileGALInstallInfoBarLinkNavigation") | 142 ? UserMetricsAction("MobileGALInstallInfoBarLinkNavigation") |
140 : UserMetricsAction("MobileGALInstallInfoBarDirectNavigation")); | 143 : UserMetricsAction("MobileGALInstallInfoBarDirectNavigation")); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 registerForInstallationNotifications:self | 176 registerForInstallationNotifications:self |
174 withSelector:@selector(appDidInstall:) | 177 withSelector:@selector(appDidInstall:) |
175 forScheme:[_metadata anyScheme]]; | 178 forScheme:[_metadata anyScheme]]; |
176 NSString* appIdString = [self appId]; | 179 NSString* appIdString = [self appId]; |
177 // Defensively early return if native app metadata returns an nil string for | 180 // Defensively early return if native app metadata returns an nil string for |
178 // appId which can cause subsequent -addObject: to throw exceptions. | 181 // appId which can cause subsequent -addObject: to throw exceptions. |
179 if (![appIdString length]) | 182 if (![appIdString length]) |
180 return; | 183 return; |
181 DCHECK(![_appsPossiblyBeingInstalled containsObject:appIdString]); | 184 DCHECK(![_appsPossiblyBeingInstalled containsObject:appIdString]); |
182 [_appsPossiblyBeingInstalled addObject:appIdString]; | 185 [_appsPossiblyBeingInstalled addObject:appIdString]; |
186 // TODO(crbug.com/684063): Preferred method is to add a helper object to | |
187 // WebState and use the helper object to launch Store Kit. | |
183 [_tab openAppStore:appIdString]; | 188 [_tab openAppStore:appIdString]; |
184 } | 189 } |
185 | 190 |
186 - (void)launchApp:(const GURL&)URL { | 191 - (void)launchApp:(const GURL&)URL { |
187 // TODO(crbug.com/353957): Pass the ChromeIdentity to | 192 // TODO(crbug.com/353957): Pass the ChromeIdentity to |
188 // -launchURLWithURL:identity: | 193 // -launchURLWithURL:identity: |
189 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]); | 194 GURL launchURL([_metadata launchURLWithURL:URL identity:nil]); |
190 if (launchURL.is_valid()) { | 195 if (launchURL.is_valid()) { |
191 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil); | 196 OpenUrlWithCompletionHandler(net::NSURLWithGURL(launchURL), nil); |
192 } | 197 } |
193 } | 198 } |
194 | 199 |
195 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction { | 200 - (void)updateMetadataWithUserAction:(NativeAppActionType)userAction { |
196 [_metadata updateWithUserAction:userAction]; | 201 [_metadata updateWithUserAction:userAction]; |
197 } | 202 } |
198 | 203 |
199 #pragma mark - | 204 #pragma mark - |
200 #pragma mark CRWWebControllerObserver methods | 205 #pragma mark CRWWebControllerObserver methods |
201 | 206 |
202 - (void)pageLoaded:(CRWWebController*)webController { | 207 - (void)pageLoaded:(CRWWebController*)webController { |
203 if (![_tab isPrerenderTab]) | 208 if (![_tab isPrerenderTab]) |
204 [self showInfoBarIfNecessary]; | 209 [self showInfoBarIfNecessary]; |
205 } | 210 } |
206 | 211 |
207 - (void)webControllerWillClose:(CRWWebController*)webController { | 212 - (void)webControllerWillClose:(CRWWebController*)webController { |
208 [webController removeObserver:self]; | 213 [webController removeObserver:self]; |
209 } | 214 } |
210 | 215 |
211 - (BOOL)isLinkNavigation { | |
212 if (![_tab navigationManager]) | |
213 return NO; | |
214 web::NavigationItem* userItem = [_tab navigationManager]->GetLastUserItem(); | |
215 if (!userItem) | |
216 return NO; | |
217 ui::PageTransition currentTransition = userItem->GetTransitionType(); | |
218 return PageTransitionCoreTypeIs(currentTransition, | |
219 ui::PAGE_TRANSITION_LINK) || | |
220 PageTransitionCoreTypeIs(currentTransition, | |
221 ui::PAGE_TRANSITION_AUTO_BOOKMARK); | |
222 } | |
223 | |
224 - (void)appDidInstall:(NSNotification*)notification { | 216 - (void)appDidInstall:(NSNotification*)notification { |
225 [self removeAppFromNotification:notification]; | 217 [self removeAppFromNotification:notification]; |
226 [self showInfoBarIfNecessary]; | 218 [self showInfoBarIfNecessary]; |
227 } | 219 } |
228 | 220 |
229 - (void)removeAppFromNotification:(NSNotification*)notification { | 221 - (void)removeAppFromNotification:(NSNotification*)notification { |
230 DCHECK([[notification object] isKindOfClass:[InstallationNotifier class]]); | 222 DCHECK([[notification object] isKindOfClass:[InstallationNotifier class]]); |
231 NSString* schemeOfInstalledApp = [notification name]; | 223 NSString* schemeOfInstalledApp = [notification name]; |
232 __block NSString* appIDToRemove = nil; | 224 __block NSString* appIDToRemove = nil; |
233 [_appsPossiblyBeingInstalled | 225 [_appsPossiblyBeingInstalled |
234 enumerateObjectsUsingBlock:^(id appID, BOOL* stop) { | 226 enumerateObjectsUsingBlock:^(id appID, BOOL* stop) { |
235 NSURL* appURL = | 227 NSURL* appURL = |
236 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager() | 228 [ios::GetChromeBrowserProvider()->GetNativeAppWhitelistManager() |
237 schemeForAppId:appID]; | 229 schemeForAppId:appID]; |
238 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) { | 230 if ([[appURL scheme] isEqualToString:schemeOfInstalledApp]) { |
239 appIDToRemove = appID; | 231 appIDToRemove = appID; |
240 *stop = YES; | 232 *stop = YES; |
241 } | 233 } |
242 }]; | 234 }]; |
243 DCHECK(appIDToRemove); | 235 DCHECK(appIDToRemove); |
244 [_appsPossiblyBeingInstalled removeObject:appIDToRemove]; | 236 [_appsPossiblyBeingInstalled removeObject:appIDToRemove]; |
245 } | 237 } |
246 | 238 |
247 @end | 239 @end |
OLD | NEW |