mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Teach ResourceManager to load from files
This commit is contained in:
parent
51a6e835d2
commit
c01899233a
3 changed files with 38 additions and 2 deletions
29
src/servo/resource/file_loader.rs
Normal file
29
src/servo/resource/file_loader.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
export factory;
|
||||
|
||||
import comm::{chan, methods};
|
||||
import task::spawn;
|
||||
import resource_task::{ProgressMsg, Payload, Done};
|
||||
import std::net::url::url;
|
||||
import io::file_reader;
|
||||
import result::{result, ok, err};
|
||||
|
||||
const READ_SIZE: uint = 1024;
|
||||
|
||||
fn factory(url: url, progress_chan: chan<ProgressMsg>) {
|
||||
assert url.scheme == ~"file";
|
||||
|
||||
do spawn {
|
||||
alt file_reader(url.path) {
|
||||
ok(reader) {
|
||||
while !reader.eof() {
|
||||
let data = reader.read_bytes(READ_SIZE);
|
||||
progress_chan.send(Payload(data));
|
||||
}
|
||||
progress_chan.send(Done(ok(())));
|
||||
}
|
||||
err(*) {
|
||||
progress_chan.send(Done(err(())));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -4,7 +4,9 @@ A task that takes a URL and streams back the binary data
|
|||
|
||||
*/
|
||||
|
||||
export ControlMsg, ProgressMsg, ResourceTask, ResourceManager, LoaderTaskFactory;
|
||||
export ControlMsg, Load, Exit;
|
||||
export ProgressMsg, Payload, Done;
|
||||
export ResourceTask, ResourceManager, LoaderTaskFactory;
|
||||
|
||||
import comm::{chan, port, methods};
|
||||
import task::{spawn, spawn_listener};
|
||||
|
@ -27,7 +29,10 @@ type ResourceTask = chan<ControlMsg>;
|
|||
type LoaderTaskFactory = fn~(url: url, chan<ProgressMsg>);
|
||||
|
||||
fn ResourceTask() -> ResourceTask {
|
||||
create_resource_task_with_loaders(~[])
|
||||
let loaders = ~[
|
||||
(~"file", file_loader::factory)
|
||||
];
|
||||
create_resource_task_with_loaders(loaders)
|
||||
}
|
||||
|
||||
fn create_resource_task_with_loaders(+loaders: ~[(~str, LoaderTaskFactory)]) -> ResourceTask {
|
||||
|
@ -63,6 +68,7 @@ class ResourceManager {
|
|||
|
||||
alt self.get_loader_factory(url) {
|
||||
some(loader_factory) {
|
||||
#debug("resource_task: loading url: %s", url::to_str(url));
|
||||
loader_factory(url, progress_chan);
|
||||
}
|
||||
none {
|
||||
|
|
|
@ -112,6 +112,7 @@ mod engine;
|
|||
|
||||
mod resource {
|
||||
mod resource_task;
|
||||
mod file_loader;
|
||||
}
|
||||
|
||||
import servo_text = text;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue