OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #include "config.h" |
| 6 #include "core/app/ModuleLoader.h" |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "core/app/Application.h" |
| 10 #include "core/app/Module.h" |
| 11 #include "core/dom/Document.h" |
| 12 #include "core/dom/DocumentParser.h" |
| 13 #include "core/html/HTMLDocument.h" |
| 14 #include "wtf/text/WTFString.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 ModuleLoader::Client::~Client() { |
| 19 } |
| 20 |
| 21 ModuleLoader::ModuleLoader(Client* client, |
| 22 Application* application, |
| 23 const KURL& url) |
| 24 : state_(LOADING), |
| 25 client_(client), |
| 26 application_(application), |
| 27 fetcher_(adoptPtr(new MojoFetcher(this, url))), |
| 28 weak_factory_(this) { |
| 29 } |
| 30 |
| 31 ModuleLoader::~ModuleLoader() { |
| 32 } |
| 33 |
| 34 void ModuleLoader::OnReceivedResponse(mojo::URLResponsePtr response) { |
| 35 if (response->error || response->status_code >= 400) { |
| 36 String message = String::format( |
| 37 "Failed to load resource: Server responded with a status of %d (%s)", |
| 38 response->status_code, response->status_line.data()); |
| 39 RefPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create( |
| 40 NetworkMessageSource, ErrorMessageLevel, message, response->url.data()); |
| 41 application_->document()->addMessage(consoleMessage); |
| 42 state_ = COMPLETE; |
| 43 client_->OnModuleLoadComplete(this, nullptr); |
| 44 return; |
| 45 } |
| 46 |
| 47 WeakPtr<Document> context = application_->document()->contextDocument(); |
| 48 ASSERT(context.get()); |
| 49 KURL url(ParsedURLString, String::fromUTF8(response->url)); |
| 50 DocumentInit init = DocumentInit(url, 0, context, 0) |
| 51 .withRegistrationContext(context->registrationContext()); |
| 52 |
| 53 RefPtr<Document> document = HTMLDocument::create(init); |
| 54 document->startParsing()->parse(response->body.Pass(), |
| 55 base::Bind(&ModuleLoader::OnParsingComplete, weak_factory_.GetWeakPtr())); |
| 56 |
| 57 module_ = Module::create( |
| 58 context.get(), application_, document.release(), url.string()); |
| 59 } |
| 60 |
| 61 void ModuleLoader::OnParsingComplete() { |
| 62 state_ = COMPLETE; |
| 63 client_->OnModuleLoadComplete(this, module_.get()); |
| 64 } |
| 65 |
| 66 } // namespace blink |
OLD | NEW |