| 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 #import "ios/web/webui/crw_web_ui_manager.h" | 5 #import "ios/web/webui/crw_web_ui_manager.h" |
| 6 | 6 |
| 7 #include "base/json/string_escape.h" | 7 #include "base/json/string_escape.h" |
| 8 #include "base/mac/bind_objc_block.h" | 8 #include "base/mac/bind_objc_block.h" |
| 9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "ios/web/webui/mojo_js_constants.h" | 23 #include "ios/web/webui/mojo_js_constants.h" |
| 24 #include "ios/web/webui/url_fetcher_block_adapter.h" | 24 #include "ios/web/webui/url_fetcher_block_adapter.h" |
| 25 #include "mojo/public/js/constants.h" | 25 #include "mojo/public/js/constants.h" |
| 26 #import "net/base/mac/url_conversions.h" | 26 #import "net/base/mac/url_conversions.h" |
| 27 | 27 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) | 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." | 29 #error "This file requires ARC support." |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 // Prefix for history.requestFavicon JavaScript message. | 33 // Prefix for JavaScript messages. |
| 34 const char kScriptCommandPrefix[] = "webui"; | 34 const char kScriptCommandPrefix[] = "webui"; |
| 35 } | 35 } |
| 36 | 36 |
| 37 @interface CRWWebUIManager () <CRWWebUIPageBuilderDelegate> | 37 @interface CRWWebUIManager () <CRWWebUIPageBuilderDelegate> |
| 38 | 38 |
| 39 // Current web state. | 39 // Current web state. |
| 40 @property(nonatomic, readonly) web::WebStateImpl* webState; | 40 @property(nonatomic, readonly) web::WebStateImpl* webState; |
| 41 | 41 |
| 42 // Composes WebUI page for webUIURL and invokes completionHandler with the | 42 // Composes WebUI page for webUIURL and invokes completionHandler with the |
| 43 // result. | 43 // result. |
| 44 - (void)loadWebUIPageForURL:(const GURL&)webUIURL | 44 - (void)loadWebUIPageForURL:(const GURL&)webUIURL |
| 45 completionHandler:(void (^)(NSString*))completionHandler; | 45 completionHandler:(void (^)(NSString*))completionHandler; |
| 46 | 46 |
| 47 // Retrieves resource for URL and invokes completionHandler with the result. | 47 // Retrieves resource for URL and invokes completionHandler with the result. |
| 48 - (void)fetchResourceWithURL:(const GURL&)URL | 48 - (void)fetchResourceWithURL:(const GURL&)URL |
| 49 completionHandler:(void (^)(NSData*))completionHandler; | 49 completionHandler:(void (^)(NSData*))completionHandler; |
| 50 | 50 |
| 51 // Handles JavaScript message from the WebUI page. | 51 // Handles JavaScript message from the WebUI page. |
| 52 - (BOOL)handleWebUIJSMessage:(const base::DictionaryValue&)message; | 52 - (BOOL)handleWebUIJSMessage:(const base::DictionaryValue&)message; |
| 53 | 53 |
| 54 // Handles webui.requestFavicon JavaScript message from the WebUI page. | |
| 55 - (BOOL)handleRequestFavicon:(const base::ListValue*)arguments; | |
| 56 | |
| 57 // Handles webui.loadMojo JavaScript message from the WebUI page. | 54 // Handles webui.loadMojo JavaScript message from the WebUI page. |
| 58 - (BOOL)handleLoadMojo:(const base::ListValue*)arguments; | 55 - (BOOL)handleLoadMojo:(const base::ListValue*)arguments; |
| 59 | 56 |
| 60 // Executes mojo script and signals |webui.loadMojo| finish. | 57 // Executes mojo script and signals |webui.loadMojo| finish. |
| 61 - (void)executeMojoScript:(const std::string&)mojoScript | 58 - (void)executeMojoScript:(const std::string&)mojoScript |
| 62 forModuleName:(const std::string&)moduleName | 59 forModuleName:(const std::string&)moduleName |
| 63 loadID:(const std::string&)loadID; | 60 loadID:(const std::string&)loadID; |
| 64 | 61 |
| 65 // Removes favicon callback from web state. | 62 // Removes favicon callback from web state. |
| 66 - (void)resetWebState; | 63 - (void)resetWebState; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 if (!message.GetList("arguments", &arguments)) { | 187 if (!message.GetList("arguments", &arguments)) { |
| 191 DLOG(WARNING) << "JS message parameter not found: arguments"; | 188 DLOG(WARNING) << "JS message parameter not found: arguments"; |
| 192 return NO; | 189 return NO; |
| 193 } | 190 } |
| 194 | 191 |
| 195 if (!arguments) { | 192 if (!arguments) { |
| 196 DLOG(WARNING) << "No arguments provided to " << command; | 193 DLOG(WARNING) << "No arguments provided to " << command; |
| 197 return NO; | 194 return NO; |
| 198 } | 195 } |
| 199 | 196 |
| 200 if (command == "webui.requestFavicon") | |
| 201 return [self handleRequestFavicon:arguments]; | |
| 202 if (command == "webui.loadMojo") | 197 if (command == "webui.loadMojo") |
| 203 return [self handleLoadMojo:arguments]; | 198 return [self handleLoadMojo:arguments]; |
| 204 | 199 |
| 205 DLOG(WARNING) << "Unknown webui command received: " << command; | 200 DLOG(WARNING) << "Unknown webui command received: " << command; |
| 206 return NO; | 201 return NO; |
| 207 } | 202 } |
| 208 | 203 |
| 209 - (BOOL)handleRequestFavicon:(const base::ListValue*)arguments { | |
| 210 std::string favicon; | |
| 211 if (!arguments->GetString(0, &favicon)) { | |
| 212 DLOG(WARNING) << "JS message parameter not found: Favicon URL"; | |
| 213 return NO; | |
| 214 } | |
| 215 GURL faviconURL(favicon); | |
| 216 DCHECK(faviconURL.is_valid()); | |
| 217 // Retrieve favicon resource and set favicon background image via JavaScript. | |
| 218 base::WeakNSObject<CRWWebUIManager> weakSelf(self); | |
| 219 void (^faviconHandler)(NSData*) = ^void(NSData* data) { | |
| 220 base::scoped_nsobject<CRWWebUIManager> strongSelf(weakSelf); | |
| 221 if (!strongSelf) | |
| 222 return; | |
| 223 NSString* base64EncodedResource = [data base64EncodedStringWithOptions:0]; | |
| 224 NSString* dataURLString = [NSString | |
| 225 stringWithFormat:@"data:image/png;base64,%@", base64EncodedResource]; | |
| 226 NSString* faviconURLString = base::SysUTF8ToNSString(faviconURL.spec()); | |
| 227 NSString* script = | |
| 228 [NSString stringWithFormat:@"chrome.setFaviconBackground('%@', '%@');", | |
| 229 faviconURLString, dataURLString]; | |
| 230 [strongSelf webState]->ExecuteJavaScript(base::SysNSStringToUTF16(script)); | |
| 231 }; | |
| 232 [self fetchResourceWithURL:faviconURL completionHandler:faviconHandler]; | |
| 233 return YES; | |
| 234 } | |
| 235 | |
| 236 - (BOOL)handleLoadMojo:(const base::ListValue*)arguments { | 204 - (BOOL)handleLoadMojo:(const base::ListValue*)arguments { |
| 237 std::string moduleName; | 205 std::string moduleName; |
| 238 if (!arguments->GetString(0, &moduleName)) { | 206 if (!arguments->GetString(0, &moduleName)) { |
| 239 DLOG(WARNING) << "JS message parameter not found: Module name"; | 207 DLOG(WARNING) << "JS message parameter not found: Module name"; |
| 240 return NO; | 208 return NO; |
| 241 } | 209 } |
| 242 std::string loadID; | 210 std::string loadID; |
| 243 if (!arguments->GetString(1, &loadID)) { | 211 if (!arguments->GetString(1, &loadID)) { |
| 244 DLOG(WARNING) << "JS message parameter not found: Load ID"; | 212 DLOG(WARNING) << "JS message parameter not found: Load ID"; |
| 245 return NO; | 213 return NO; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 301 |
| 334 - (std::unique_ptr<web::URLFetcherBlockAdapter>) | 302 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
| 335 fetcherForURL:(const GURL&)URL | 303 fetcherForURL:(const GURL&)URL |
| 336 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { | 304 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { |
| 337 return std::unique_ptr<web::URLFetcherBlockAdapter>( | 305 return std::unique_ptr<web::URLFetcherBlockAdapter>( |
| 338 new web::URLFetcherBlockAdapter( | 306 new web::URLFetcherBlockAdapter( |
| 339 URL, _webState->GetBrowserState()->GetRequestContext(), handler)); | 307 URL, _webState->GetBrowserState()->GetRequestContext(), handler)); |
| 340 } | 308 } |
| 341 | 309 |
| 342 @end | 310 @end |
| OLD | NEW |