Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/itunes_links/itunes_links_observer.h" | 5 #import "ios/chrome/browser/itunes_links/itunes_links_observer.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "ios/chrome/browser/storekit_launcher.h" | 11 #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h" |
| 12 #import "ios/web/public/web_state/web_state.h" | |
| 12 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 13 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 13 #include "ios/web/public/web_state/web_state.h" | |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 @interface ITunesLinksObserver () | 20 @interface ITunesLinksObserver () |
| 21 | 21 |
| 22 // If |URL| points to a product on itunes.apple.com, returns the product ID. | 22 // If |URL| points to a product on itunes.apple.com, returns the product ID. |
| 23 // Otherwise, returns nil. | 23 // Otherwise, returns nil. |
| 24 // Examples of URLs pointing to products on itunes.apple.com can be found in | 24 // Examples of URLs pointing to products on itunes.apple.com can be found in |
| 25 // itunes_links_observer_unittest.mm. | 25 // itunes_links_observer_unittest.mm. |
| 26 + (NSString*)productIDFromURL:(const GURL&)URL; | 26 + (NSString*)productIDFromURL:(const GURL&)URL; |
| 27 | 27 |
| 28 @end | 28 @end |
| 29 | 29 |
| 30 @implementation ITunesLinksObserver { | 30 @implementation ITunesLinksObserver { |
| 31 __weak id<StoreKitLauncher> _storeKitLauncher; | 31 web::WebState* _webState; |
| 32 std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge; | 32 std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge; |
| 33 } | 33 } |
| 34 | 34 |
| 35 - (instancetype)initWithWebState:(web::WebState*)webState { | 35 - (instancetype)initWithWebState:(web::WebState*)webState { |
| 36 self = [super init]; | 36 self = [super init]; |
| 37 if (self) { | 37 if (self) { |
| 38 _webStateObserverBridge.reset( | 38 _webStateObserverBridge.reset( |
| 39 new web::WebStateObserverBridge(webState, self)); | 39 new web::WebStateObserverBridge(webState, self)); |
|
sdefresne
2017/03/08 02:53:14
optional: can you change this to use base::MakeUni
pkl (ping after 24h if needed)
2017/03/08 14:27:41
I'll do a separate CL as this is unrelated to this
| |
| 40 _webState = webState; | |
| 40 } | 41 } |
| 41 return self; | 42 return self; |
| 42 } | 43 } |
| 43 | 44 |
| 44 - (instancetype)init { | |
| 45 NOTREACHED(); | |
| 46 return nil; | |
| 47 } | |
| 48 | |
| 49 + (NSString*)productIDFromURL:(const GURL&)URL { | 45 + (NSString*)productIDFromURL:(const GURL&)URL { |
| 50 if (!URL.SchemeIsHTTPOrHTTPS() || !URL.DomainIs("itunes.apple.com")) | 46 if (!URL.SchemeIsHTTPOrHTTPS() || !URL.DomainIs("itunes.apple.com")) |
| 51 return nil; | 47 return nil; |
| 52 std::string fileName = URL.ExtractFileName(); | 48 std::string fileName = URL.ExtractFileName(); |
| 53 // The first 2 characters must be "id", followed by the app ID. | 49 // The first 2 characters must be "id", followed by the app ID. |
| 54 if (fileName.length() < 3 || fileName.substr(0, 2) != "id") | 50 if (fileName.length() < 3 || fileName.substr(0, 2) != "id") |
| 55 return nil; | 51 return nil; |
| 56 std::string productID = fileName.substr(2); | 52 std::string productID = fileName.substr(2); |
| 57 return base::SysUTF8ToNSString(productID); | 53 return base::SysUTF8ToNSString(productID); |
| 58 } | 54 } |
| 59 | 55 |
| 60 #pragma mark - CRWWebStateObserver | 56 #pragma mark - WebStateObserverBridge |
| 61 | 57 |
| 62 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | 58 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 63 GURL URL = webState->GetLastCommittedURL(); | 59 GURL URL = webState->GetLastCommittedURL(); |
| 64 NSString* productID = [ITunesLinksObserver productIDFromURL:URL]; | 60 NSString* productID = [ITunesLinksObserver productIDFromURL:URL]; |
| 65 if (productID) | 61 if (productID) { |
| 66 [_storeKitLauncher openAppStore:productID]; | 62 StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(_webState); |
| 63 if (tabHelper) | |
| 64 tabHelper->OpenAppStore(productID); | |
| 65 } | |
| 67 } | 66 } |
| 68 | 67 |
| 69 - (void)setStoreKitLauncher:(id<StoreKitLauncher>)storeKitLauncher { | 68 - (void)setStoreKitLauncher:(id<StoreKitLauncher>)storeKitLauncher { |
| 70 _storeKitLauncher = storeKitLauncher; | 69 StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(_webState); |
| 70 if (tabHelper) | |
| 71 tabHelper->SetLauncher(storeKitLauncher); | |
| 71 } | 72 } |
| 72 | 73 |
| 73 @end | 74 @end |
| OLD | NEW |