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 #include "ios/web/public/web_state/web_state.h" | |
|
sdefresne
2017/03/06 19:44:03
#import (this forward-declares UIView, so it is an
pkl (ping after 24h if needed)
2017/03/07 01:17:53
Done.
| |
| 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)); |
| 40 _webState = webState; | |
| 40 } | 41 } |
| 41 return self; | 42 return self; |
| 42 } | 43 } |
| 43 | 44 |
| 44 - (instancetype)init { | 45 - (instancetype)init { |
| 45 NOTREACHED(); | 46 NOTREACHED(); |
| 46 return nil; | 47 return nil; |
| 47 } | 48 } |
| 48 | 49 |
| 49 + (NSString*)productIDFromURL:(const GURL&)URL { | 50 + (NSString*)productIDFromURL:(const GURL&)URL { |
| 50 if (!URL.SchemeIsHTTPOrHTTPS() || !URL.DomainIs("itunes.apple.com")) | 51 if (!URL.SchemeIsHTTPOrHTTPS() || !URL.DomainIs("itunes.apple.com")) |
| 51 return nil; | 52 return nil; |
| 52 std::string fileName = URL.ExtractFileName(); | 53 std::string fileName = URL.ExtractFileName(); |
| 53 // The first 2 characters must be "id", followed by the app ID. | 54 // The first 2 characters must be "id", followed by the app ID. |
| 54 if (fileName.length() < 3 || fileName.substr(0, 2) != "id") | 55 if (fileName.length() < 3 || fileName.substr(0, 2) != "id") |
| 55 return nil; | 56 return nil; |
| 56 std::string productID = fileName.substr(2); | 57 std::string productID = fileName.substr(2); |
| 57 return base::SysUTF8ToNSString(productID); | 58 return base::SysUTF8ToNSString(productID); |
| 58 } | 59 } |
| 59 | 60 |
| 60 #pragma mark - CRWWebStateObserver | 61 #pragma mark - WebStateObserverBridge |
| 61 | 62 |
| 62 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | 63 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 63 GURL URL = webState->GetLastCommittedURL(); | 64 GURL URL = webState->GetLastCommittedURL(); |
| 64 NSString* productID = [ITunesLinksObserver productIDFromURL:URL]; | 65 NSString* productID = [ITunesLinksObserver productIDFromURL:URL]; |
| 65 if (productID) | 66 if (productID) { |
| 66 [_storeKitLauncher openAppStore:productID]; | 67 StoreKitTabHelper* tab_helper = StoreKitTabHelper::FromWebState(_webState); |
| 68 if (tab_helper) | |
| 69 tab_helper->OpenAppStore(productID); | |
| 70 } | |
| 67 } | 71 } |
| 68 | 72 |
| 69 - (void)setStoreKitLauncher:(id<StoreKitLauncher>)storeKitLauncher { | 73 - (void)setStoreKitLauncher:(id<StoreKitLauncher>)storeKitLauncher { |
| 70 _storeKitLauncher = storeKitLauncher; | 74 StoreKitTabHelper* tab_helper = StoreKitTabHelper::FromWebState(_webState); |
| 75 if (tab_helper) | |
| 76 tab_helper->SetLauncher(storeKitLauncher); | |
| 71 } | 77 } |
| 72 | 78 |
| 73 @end | 79 @end |
| OLD | NEW |