Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/chrome/browser/itunes_links/itunes_links_observer.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/mac/scoped_nsobject.h" | |
|
sdefresne
2016/07/25 12:05:37
#import
jif
2016/07/25 12:58:27
Done.
| |
| 11 #include "base/strings/sys_string_conversions.h" | |
| 12 #import "ios/chrome/browser/storekit_launcher.h" | |
| 13 #import "ios/web/public/web_state/web_state_observer_bridge.h" | |
| 14 #include "ios/web/public/web_state/web_state.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 @interface ITunesLinksObserver () | |
| 18 | |
| 19 // If |URL| points to a product on itunes.apple.com, returns the product ID. | |
| 20 // Otherwise, returns nil. | |
| 21 // Examples of URLs pointing to products on itunes.apple.com can be found in | |
| 22 // itunes_links_observer_unittest.mm. | |
| 23 + (NSString*)productIDFromURL:(const GURL&)URL; | |
| 24 | |
| 25 @end | |
| 26 | |
| 27 @implementation ITunesLinksObserver { | |
| 28 base::WeakNSProtocol<id<StoreKitLauncher>> _storeKitLauncher; | |
| 29 std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge; | |
| 30 } | |
| 31 | |
| 32 - (instancetype)initWithWebState:(web::WebState*)webState { | |
| 33 self = [super init]; | |
| 34 if (self) { | |
| 35 _webStateObserverBridge.reset( | |
| 36 new web::WebStateObserverBridge(webState, self)); | |
| 37 } | |
| 38 return self; | |
| 39 } | |
| 40 | |
| 41 - (instancetype)init { | |
| 42 NOTREACHED(); | |
| 43 return nil; | |
| 44 } | |
| 45 | |
| 46 + (NSString*)productIDFromURL:(const GURL&)URL { | |
| 47 if (!URL.SchemeIsHTTPOrHTTPS() || !URL.DomainIs("itunes.apple.com")) | |
| 48 return nil; | |
| 49 std::string fileName = URL.ExtractFileName(); | |
| 50 // The first 2 characters must be "id", followed by the app ID. | |
| 51 if (fileName.length() < 3 || fileName.substr(0, 2) != "id") | |
| 52 return nil; | |
| 53 std::string productID = fileName.substr(2); | |
| 54 return base::SysUTF8ToNSString(productID); | |
| 55 } | |
| 56 | |
| 57 #pragma mark - CRWWebStateObserver | |
| 58 | |
| 59 - (void)webStateDidLoadPage:(web::WebState*)webState { | |
| 60 GURL URL = webState->GetLastCommittedURL(); | |
| 61 NSString* productID = [ITunesLinksObserver productIDFromURL:URL]; | |
| 62 if (productID) | |
| 63 [_storeKitLauncher openAppStore:productID]; | |
| 64 } | |
| 65 | |
| 66 - (void)setStoreKitLauncher:(id<StoreKitLauncher>)storeKitLauncher { | |
| 67 _storeKitLauncher.reset(storeKitLauncher); | |
| 68 } | |
| 69 | |
| 70 @end | |
| OLD | NEW |