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

Side by Side Diff: ios/chrome/browser/itunes_links/itunes_links_observer.mm

Issue 2176503003: Intercept iTunes links and open iTunes controller. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 4 years, 5 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
(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 #import "base/mac/scoped_nsobject.h"
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698