File Coverage

blib/lib/Serengeti/Backend/Native.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Serengeti::Backend::Native;
2              
3 1     1   30783 use strict;
  1         3  
  1         46  
4 1     1   5 use warnings;
  1         3  
  1         34  
5              
6 1     1   1006 use Module::Load qw();
  1         1778  
  1         67  
7              
8 1     1   2057 use JavaScript;
  0            
  0            
9             use Scalar::Util qw(weaken refaddr);
10              
11             require Exporter;
12              
13             use Serengeti::Backend::Native::Document;
14             use Serengeti::Backend::Native::HTMLCollection;
15             use Serengeti::Backend::Native::HTMLElement;
16             use Serengeti::Backend::Native::Window;
17              
18             use Serengeti::NotificationCenter;
19             use Serengeti::Notifications;
20              
21             our @ISA = qw(Exporter);
22              
23             our $UserAgent =
24             "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; " .
25             "en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8";
26              
27             our %DefaultHeaders = (
28             "Accept" => "text/html,application/xhtml+xml," .
29             "application/xml;q=0.9,*/*;q=0.8",
30             "Accept-Language" => "en-us,en;q=0.5",
31             "Accept-Encoding" => "gzip,deflate",
32             "Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
33             );
34              
35             our $Transport = "Serengeti::Backend::Native::Transport::Curl";
36              
37             our @EXPORT = qw();
38             our @EXPORT_OK = qw($UserAgent %DefaultHeaders);
39              
40             use accessors::ro qw(transport session current_document);
41              
42             sub setup_document_jsapi {
43             my ($self, $ctx) = @_;
44              
45             use Data::Dumper qw(Dumper);
46            
47             Serengeti::Backend::Native::Document->setup_jsapi($ctx);
48             Serengeti::Backend::Native::HTMLElement->setup_jsapi($ctx);
49            
50             $ctx->bind_class(
51             name => "NodeList",
52             package => "XML::XPathEngine::NodeSet",
53             methods => {
54             item => sub { shift->get_node(shift() + 1); },
55             },
56             getter => sub {
57             my ($node_list, $property) = @_;
58             return $node_list->size() if $property eq "length";
59             if ($property >= 0 && $property < $node_list->size() ) {
60             return $node_list->get_node($property + 1);
61             }
62              
63             return;
64             },
65             flags => JS_CLASS_NO_INSTANCE,
66             );
67            
68             $ctx->bind_class(
69             name => "HTMLCollection",
70             package => "Serengeti::Backend::Native::HTMLCollection",
71             flags => JS_CLASS_NO_INSTANCE,
72             getter => \&Serengeti::Backend::Native::HTMLCollection::get_property,
73             );
74            
75             1;
76             }
77              
78             sub setup_window_jsapi {
79             my ($self, $ctx) = @_;
80            
81             Serengeti::Backend::Native::Window->setup_jsapi($self, $ctx);
82            
83             1;
84             }
85              
86             sub new {
87             my ($pkg) = @_;
88            
89             Module::Load::load $Transport;
90            
91             my $self = bless {
92             transport => $Transport->new(),
93             session => undef,
94             }, $pkg;
95            
96             return $self;
97             }
98              
99             sub _handle_response {
100             my ($self, $response, $url, $options) = @_;
101              
102             my $document = Serengeti::Backend::Native::Document->new(
103             $response->decoded_content(),
104             {
105             location => URI->new($url),
106             browser => $self,
107             }
108             );
109            
110             $self->{current_document} = $document;
111            
112             $options = {} unless ref $options eq "HASH";
113            
114             unless ($options->{no_broadcast}) {
115             # Notify listeners that we've got a new global doc if it's
116             # not requested by a frame
117             Serengeti::NotificationCenter->post_notification(
118             $self, DOCUMENT_CHANGED_NOTIFICATION, $document
119             );
120             }
121            
122             return $document;
123             }
124              
125             sub get {
126             my ($self, $url, $query_data, $options) = @_;
127            
128             my $response = $self->transport->get($url, $query_data, $options);
129            
130             return $self->_handle_response($response, $url, $options);
131             }
132              
133             sub post {
134             my ($self, $url, $form_data, $options) = @_;
135            
136             my $response = $self->transport->post($url, $form_data, $options);
137            
138             return $self->_handle_response($response);
139             }
140              
141              
142             1;
143             __END__