| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 NSString* const kJSTagSuffix = @"\"></script>"; | 21 NSString* const kJSTagSuffix = @"\"></script>"; |
| 22 // Prefix for stylesheet tags. Used to locate CSS subresources. | 22 // Prefix for stylesheet tags. Used to locate CSS subresources. |
| 23 NSString* const kCSSTagPrefix = @"<link rel=\"stylesheet\" href=\""; | 23 NSString* const kCSSTagPrefix = @"<link rel=\"stylesheet\" href=\""; |
| 24 // Suffix for stylesheet tags. Used to locate CSS subresources. | 24 // Suffix for stylesheet tags. Used to locate CSS subresources. |
| 25 NSString* const kCSSTagSuffix = @"\">"; | 25 NSString* const kCSSTagSuffix = @"\">"; |
| 26 // Template for creating inlined JavaScript tags. | 26 // Template for creating inlined JavaScript tags. |
| 27 NSString* const kWebUIScriptTextTemplate = @"<script>%@</script>"; | 27 NSString* const kWebUIScriptTextTemplate = @"<script>%@</script>"; |
| 28 // Template for creating inlined CSS tags. | 28 // Template for creating inlined CSS tags. |
| 29 NSString* const kWebUIStyleTextTemplate = @"<style>%@</style>"; | 29 NSString* const kWebUIStyleTextTemplate = @"<style>%@</style>"; |
| 30 // URL placeholder for WebUI messaging JavaScript. | 30 // URL placeholder for WebUI messaging JavaScript. |
| 31 NSString* const kWebUICoreJSURL = @"chrome://resources/js/ios/core.js"; | 31 NSString* const kWebUIJSURL = @"chrome://resources/js/ios/web_ui.js"; |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 @interface CRWWebUIPageBuilder () | 34 @interface CRWWebUIPageBuilder () |
| 35 | 35 |
| 36 // Builds WebUI page for URL with HTML as default resource. | 36 // Builds WebUI page for URL with HTML as default resource. |
| 37 - (void)buildWebUIPageForHTML:(NSString*)HTML | 37 - (void)buildWebUIPageForHTML:(NSString*)HTML |
| 38 webUIURL:(const GURL&)URL | 38 webUIURL:(const GURL&)URL |
| 39 completionHandler:(web::WebUIPageCompletion)completionHandler; | 39 completionHandler:(web::WebUIPageCompletion)completionHandler; |
| 40 | 40 |
| 41 // Loads |resourceURL| by invoking _delegate. | 41 // Loads |resourceURL| by invoking _delegate. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 pendingSubresourceCount--; | 153 pendingSubresourceCount--; |
| 154 // When subresource loading is complete, flatten the default resource | 154 // When subresource loading is complete, flatten the default resource |
| 155 // and invoke the completion handler. | 155 // and invoke the completion handler. |
| 156 if (!pendingSubresourceCount) { | 156 if (!pendingSubresourceCount) { |
| 157 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; | 157 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; |
| 158 completionHandler(webUIHTML); | 158 completionHandler(webUIHTML); |
| 159 } | 159 } |
| 160 } copy] autorelease]; | 160 } copy] autorelease]; |
| 161 | 161 |
| 162 for (NSString* URLString in subresourceURLStrings) { | 162 for (NSString* URLString in subresourceURLStrings) { |
| 163 // chrome://resources/js/ios/core.js is skipped because it is | 163 // chrome://resources/js/ios/web_ui.js is skipped because it is |
| 164 // retrieved via webUIJavaScript rather than the net stack. | 164 // retrieved via webUIJavaScript rather than the net stack. |
| 165 if ([URLString isEqualToString:kWebUICoreJSURL]) { | 165 if ([URLString isEqualToString:kWebUIJSURL]) { |
| 166 pendingSubresourceCount--; | 166 pendingSubresourceCount--; |
| 167 if (!pendingSubresourceCount) { | 167 if (!pendingSubresourceCount) { |
| 168 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; | 168 [weakSelf flattenHTML:webUIHTML withSubresources:subresources]; |
| 169 completionHandler(webUIHTML); | 169 completionHandler(webUIHTML); |
| 170 } | 170 } |
| 171 continue; | 171 continue; |
| 172 } | 172 } |
| 173 GURL URL(pageURL.Resolve(base::SysNSStringToUTF8(URLString))); | 173 GURL URL(pageURL.Resolve(base::SysNSStringToUTF8(URLString))); |
| 174 // If the resolved URL is different from URLString, replace URLString in | 174 // If the resolved URL is different from URLString, replace URLString in |
| 175 // webUIHTML so that it will be located appropriately when flattening | 175 // webUIHTML so that it will be located appropriately when flattening |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 stringByAppendingString:sourceTag]; | 272 stringByAppendingString:sourceTag]; |
| 273 [HTML replaceOccurrencesOfString:sourceTag | 273 [HTML replaceOccurrencesOfString:sourceTag |
| 274 withString:extendedTag | 274 withString:extendedTag |
| 275 options:0 | 275 options:0 |
| 276 range:NSMakeRange(0, [HTML length])]; | 276 range:NSMakeRange(0, [HTML length])]; |
| 277 } | 277 } |
| 278 | 278 |
| 279 - (void)flattenHTML:(NSMutableString*)HTML | 279 - (void)flattenHTML:(NSMutableString*)HTML |
| 280 withSubresources:(std::map<GURL, std::string>)subresources { | 280 withSubresources:(std::map<GURL, std::string>)subresources { |
| 281 // Add core.js script to resources. | 281 // Add core.js script to resources. |
| 282 GURL webUIJSURL("chrome://resources/js/ios/core.js"); | 282 // TODO(ios): Move inclusion of this resource into WebUI implementation |
| 283 // rather than forking each HTML file (crbug.com/487000). |
| 284 GURL webUIJSURL("chrome://resources/js/ios/web_ui.js"); |
| 283 subresources[webUIJSURL] = base::SysNSStringToUTF8([self webUIJavaScript]); | 285 subresources[webUIJSURL] = base::SysNSStringToUTF8([self webUIJavaScript]); |
| 284 for (auto it = subresources.begin(); it != subresources.end(); it++) { | 286 for (auto it = subresources.begin(); it != subresources.end(); it++) { |
| 285 NSString* linkTemplate = @""; | 287 NSString* linkTemplate = @""; |
| 286 NSString* textTemplate = @""; | 288 NSString* textTemplate = @""; |
| 287 if ([self isCSSSubresourceURL:it->first]) { | 289 if ([self isCSSSubresourceURL:it->first]) { |
| 288 linkTemplate = | 290 linkTemplate = |
| 289 [NSString stringWithFormat:@"%@%%@%@", kCSSTagPrefix, kCSSTagSuffix]; | 291 [NSString stringWithFormat:@"%@%%@%@", kCSSTagPrefix, kCSSTagSuffix]; |
| 290 textTemplate = kWebUIStyleTextTemplate; | 292 textTemplate = kWebUIStyleTextTemplate; |
| 291 } else { // JavaScript. | 293 } else { // JavaScript. |
| 292 linkTemplate = | 294 linkTemplate = |
| (...skipping 15 matching lines...) Expand all Loading... |
| 308 - (NSString*)webUIJavaScript { | 310 - (NSString*)webUIJavaScript { |
| 309 NSBundle* bundle = base::mac::FrameworkBundle(); | 311 NSBundle* bundle = base::mac::FrameworkBundle(); |
| 310 NSString* path = [bundle pathForResource:@"web_ui" ofType:@"js"]; | 312 NSString* path = [bundle pathForResource:@"web_ui" ofType:@"js"]; |
| 311 DCHECK(path) << "web_ui.js file not found"; | 313 DCHECK(path) << "web_ui.js file not found"; |
| 312 return [NSString stringWithContentsOfFile:path | 314 return [NSString stringWithContentsOfFile:path |
| 313 encoding:NSUTF8StringEncoding | 315 encoding:NSUTF8StringEncoding |
| 314 error:nil]; | 316 error:nil]; |
| 315 } | 317 } |
| 316 | 318 |
| 317 @end | 319 @end |
| OLD | NEW |