OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "ios/web/webui/crw_web_ui_page_builder.h" | 5 #include "ios/web/webui/crw_web_ui_page_builder.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/ios/weak_nsobject.h" | |
12 #include "base/logging.h" | 11 #include "base/logging.h" |
13 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
14 #include "base/mac/scoped_nsobject.h" | 13 #include "base/mac/scoped_nsobject.h" |
15 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
16 | 15 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." |
| 18 #endif |
| 19 |
17 namespace { | 20 namespace { |
18 // Prefix for script tags. Used to locate JavaScript subresources. | 21 // Prefix for script tags. Used to locate JavaScript subresources. |
19 NSString* const kJSTagPrefix = @"<script src=\""; | 22 NSString* const kJSTagPrefix = @"<script src=\""; |
20 // Suffix for script tags. Used to locate JavaScript subresources. | 23 // Suffix for script tags. Used to locate JavaScript subresources. |
21 NSString* const kJSTagSuffix = @"\"></script>"; | 24 NSString* const kJSTagSuffix = @"\"></script>"; |
22 // Prefix for stylesheet tags. Used to locate CSS subresources. | 25 // Prefix for stylesheet tags. Used to locate CSS subresources. |
23 NSString* const kCSSTagPrefix = @"<link rel=\"stylesheet\" href=\""; | 26 NSString* const kCSSTagPrefix = @"<link rel=\"stylesheet\" href=\""; |
24 // Suffix for stylesheet tags. Used to locate CSS subresources. | 27 // Suffix for stylesheet tags. Used to locate CSS subresources. |
25 NSString* const kCSSTagSuffix = @"\">"; | 28 NSString* const kCSSTagSuffix = @"\">"; |
26 // Template for creating inlined JavaScript tags. | 29 // Template for creating inlined JavaScript tags. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 withSubresources:(const std::map<GURL, std::string>&)subresources; | 88 withSubresources:(const std::map<GURL, std::string>&)subresources; |
86 | 89 |
87 // Returns JavaScript needed for bridging WebUI chrome.send() messages to | 90 // Returns JavaScript needed for bridging WebUI chrome.send() messages to |
88 // the core.js defined message delivery system. | 91 // the core.js defined message delivery system. |
89 - (NSString*)webUIJavaScript; | 92 - (NSString*)webUIJavaScript; |
90 | 93 |
91 @end | 94 @end |
92 | 95 |
93 @implementation CRWWebUIPageBuilder { | 96 @implementation CRWWebUIPageBuilder { |
94 // Delegate for requesting resources. | 97 // Delegate for requesting resources. |
95 base::WeakNSProtocol<id<CRWWebUIPageBuilderDelegate>> _delegate; | 98 __weak id<CRWWebUIPageBuilderDelegate> _delegate; |
96 } | 99 } |
97 | 100 |
98 #pragma mark - Public Methods | 101 #pragma mark - Public Methods |
99 | 102 |
100 - (instancetype)init { | 103 - (instancetype)init { |
101 NOTREACHED(); | 104 NOTREACHED(); |
102 return self; | 105 return self; |
103 } | 106 } |
104 | 107 |
105 - (instancetype)initWithDelegate:(id<CRWWebUIPageBuilderDelegate>)delegate { | 108 - (instancetype)initWithDelegate:(id<CRWWebUIPageBuilderDelegate>)delegate { |
106 if (self = [super init]) { | 109 if (self = [super init]) { |
107 _delegate.reset(delegate); | 110 _delegate = delegate; |
108 } | 111 } |
109 return self; | 112 return self; |
110 } | 113 } |
111 | 114 |
112 - (void)buildWebUIPageForURL:(const GURL&)webUIURL | 115 - (void)buildWebUIPageForURL:(const GURL&)webUIURL |
113 completionHandler:(web::WebUIPageCompletion)completionHandler { | 116 completionHandler:(web::WebUIPageCompletion)completionHandler { |
114 [self fetchResourceWithURL:webUIURL | 117 [self fetchResourceWithURL:webUIURL |
115 completionHandler:^(NSString* webUIHTML, const GURL& URL) { | 118 completionHandler:^(NSString* webUIHTML, const GURL& URL) { |
116 [self buildWebUIPageForHTML:webUIHTML | 119 [self buildWebUIPageForHTML:webUIHTML |
117 webUIURL:URL | 120 webUIURL:URL |
118 completionHandler:completionHandler]; | 121 completionHandler:completionHandler]; |
119 }]; | 122 }]; |
120 } | 123 } |
121 | 124 |
122 #pragma mark - Private Methods | 125 #pragma mark - Private Methods |
123 | 126 |
124 - (void)buildWebUIPageForHTML:(NSString*)HTML | 127 - (void)buildWebUIPageForHTML:(NSString*)HTML |
125 webUIURL:(const GURL&)pageURL | 128 webUIURL:(const GURL&)pageURL |
126 completionHandler:(web::WebUIPageCompletion)completionHandler { | 129 completionHandler:(web::WebUIPageCompletion)completionHandler { |
127 __block base::scoped_nsobject<NSMutableString> webUIHTML([HTML mutableCopy]); | 130 __block base::scoped_nsobject<NSMutableString> webUIHTML([HTML mutableCopy]); |
128 NSSet* subresourceURLStrings = [self URLStringsFromHTML:webUIHTML]; | 131 NSSet* subresourceURLStrings = [self URLStringsFromHTML:webUIHTML]; |
129 __block NSUInteger pendingSubresourceCount = [subresourceURLStrings count]; | 132 __block NSUInteger pendingSubresourceCount = [subresourceURLStrings count]; |
130 if (!pendingSubresourceCount) { | 133 if (!pendingSubresourceCount) { |
131 completionHandler(webUIHTML); | 134 completionHandler(webUIHTML); |
132 return; | 135 return; |
133 } | 136 } |
134 __block std::map<GURL, std::string> subresources; | 137 __block std::map<GURL, std::string> subresources; |
135 base::WeakNSObject<CRWWebUIPageBuilder> weakSelf(self); | 138 __weak CRWWebUIPageBuilder* weakSelf = self; |
136 // Completion handler for subresource loads. | 139 // Completion handler for subresource loads. |
137 __block web::WebUIDelegateCompletion subresourceHandler = nil; | 140 web::WebUIDelegateCompletion subresourceHandler = nil; |
138 subresourceHandler = [[^(NSString* subresource, const GURL& subresourceURL) { | 141 __block __weak web::WebUIDelegateCompletion weakSubresourceHandler = nil; |
| 142 subresourceHandler = [^(NSString* subresource, const GURL& subresourceURL) { |
139 // Import statements in CSS resources are also loaded. | 143 // Import statements in CSS resources are also loaded. |
140 if ([self isCSSSubresourceURL:subresourceURL]) { | 144 if ([self isCSSSubresourceURL:subresourceURL]) { |
141 NSSet* URLStrings = [weakSelf URLStringsFromCSS:subresource]; | 145 NSSet* URLStrings = [weakSelf URLStringsFromCSS:subresource]; |
142 for (NSString* URLString in URLStrings) { | 146 for (NSString* URLString in URLStrings) { |
143 GURL URL(subresourceURL.Resolve(base::SysNSStringToUTF8(URLString))); | 147 GURL URL(subresourceURL.Resolve(base::SysNSStringToUTF8(URLString))); |
144 [weakSelf addCSSTagToHTML:webUIHTML | 148 [weakSelf addCSSTagToHTML:webUIHTML |
145 forURL:URL | 149 forURL:URL |
146 sourceURL:subresourceURL]; | 150 sourceURL:subresourceURL]; |
147 ++pendingSubresourceCount; | 151 ++pendingSubresourceCount; |
148 [weakSelf fetchResourceWithURL:URL | 152 [weakSelf fetchResourceWithURL:URL |
149 completionHandler:subresourceHandler]; | 153 completionHandler:weakSubresourceHandler]; |
150 } | 154 } |
151 } | 155 } |
152 subresources[subresourceURL] = base::SysNSStringToUTF8(subresource); | 156 subresources[subresourceURL] = base::SysNSStringToUTF8(subresource); |
153 --pendingSubresourceCount; | 157 --pendingSubresourceCount; |
154 // When subresource loading is complete, flatten the default resource | 158 // When subresource loading is complete, flatten the default resource |
155 // and invoke the completion handler. | 159 // and invoke the completion handler. |
156 if (!pendingSubresourceCount) { | 160 if (!pendingSubresourceCount) { |
157 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; | 161 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; |
158 completionHandler(webUIHTML); | 162 completionHandler(webUIHTML); |
159 } | 163 } |
160 } copy] autorelease]; | 164 } copy]; |
| 165 |
| 166 weakSubresourceHandler = subresourceHandler; |
161 | 167 |
162 for (NSString* URLString in subresourceURLStrings) { | 168 for (NSString* URLString in subresourceURLStrings) { |
163 // chrome://resources/js/ios/web_ui.js is skipped because it is | 169 // chrome://resources/js/ios/web_ui.js is skipped because it is |
164 // retrieved via webUIJavaScript rather than the net stack. | 170 // retrieved via webUIJavaScript rather than the net stack. |
165 if ([URLString isEqualToString:kWebUIJSURL]) { | 171 if ([URLString isEqualToString:kWebUIJSURL]) { |
166 --pendingSubresourceCount; | 172 --pendingSubresourceCount; |
167 if (!pendingSubresourceCount) { | 173 if (!pendingSubresourceCount) { |
168 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; | 174 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; |
169 completionHandler(webUIHTML); | 175 completionHandler(webUIHTML); |
170 } | 176 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 - (NSString*)webUIJavaScript { | 320 - (NSString*)webUIJavaScript { |
315 NSBundle* bundle = base::mac::FrameworkBundle(); | 321 NSBundle* bundle = base::mac::FrameworkBundle(); |
316 NSString* path = [bundle pathForResource:@"web_ui_bundle" ofType:@"js"]; | 322 NSString* path = [bundle pathForResource:@"web_ui_bundle" ofType:@"js"]; |
317 DCHECK(path) << "web_ui_bundle.js file not found"; | 323 DCHECK(path) << "web_ui_bundle.js file not found"; |
318 return [NSString stringWithContentsOfFile:path | 324 return [NSString stringWithContentsOfFile:path |
319 encoding:NSUTF8StringEncoding | 325 encoding:NSUTF8StringEncoding |
320 error:nil]; | 326 error:nil]; |
321 } | 327 } |
322 | 328 |
323 @end | 329 @end |
OLD | NEW |