Mechanize/Browser.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 120 CC 6 SCAR 15.5
CC
6
Cov
0%
new
12.0
quit
6.0
debug
6.0
CRAP 4.7
1 package Mozilla::Mechanize::Browser;
2  
3 use strict;
4 use warnings;
5  
6 use Glib qw(FALSE G_PRIORITY_LOW);
7 use Gtk2 '-init';
8 use Mozilla::Mechanize::Browser::Gtk2MozEmbed;
9  
10 ## xxx: hack until I wrap nsIWebProgressListener or something in Mozilla::DOM
11 my %STATE = (
12 START => 1,
13 REDIRECTING => 2,
14 TRANSFERRING => 4,
15 NEGOTIATING => 8,
16 STOP => 16,
17 IS_REQUEST => 65536,
18 IS_DOCUMENT => 131072,
19 IS_NETWORK => 262144,
20 IS_WINDOW => 524288,
21 RESTORING => 16777216,
22 IS_INSECURE => 4,
23 IS_BROKEN => 1,
24 IS_SECURE => 2,
25 SECURE_HIGH => 262144,
26 SECURE_MED => 65536,
27 SECURE_LOW => 131072,
28 IDENTITY_EV_TOPLEVEL => 1048576,
29 );
30  
31 sub new {
32 my $pkg = shift;
33 my $opts = shift;
34 my $debug = delete $opts->{debug};
35  
36 my $self = {
37 netstopped => 0,
38 debug => $debug,
39 };
40 bless($self, $pkg);
41  
42 $self->debug('Browser->new, opts: ' . join(', ', map("$_=$opts->{$_}", keys(%$opts))));
43  
44 my $window = Mozilla::Mechanize::Browser::Gtk2MozEmbed->new();
45 $window->set_default_size($opts->{width}, $opts->{height});
46 $window->iconify() unless $opts->{visible};
47 $window->fullscreen() if $opts->{fullscreen};
48  
49 # XXX: maybe this isn't necessary (or even working)
50 $window->signal_connect(delete_event => sub {
51 $self->debug('delete_event signal');
52  
53 # XXX: how do you check to make sure that the main loop
54 # is actually running before calling main_quit?
55 # Something in Glib::MainLoop, maybe.
56 Gtk2->main_quit;
57 return FALSE;
58 });
59  
60 my $embed = $window->{embed};
61 $self->{embed} = $embed;
62 $self->{window} = $window;
63  
64 $embed->signal_connect(net_start => sub { net_start_cb($self, @_) });
65  
66 # Any time a new page loads, this adds a "single-shot" idle callback
67 # that stops the main loop. Thanks to muppet for the idea.
68 $embed->signal_connect(net_stop => sub { net_stop_cb($self, @_) });
69  
70 $embed->signal_connect(net_state => sub { net_state_cb($self, @_) });
71 $embed->signal_connect(net_state_all => sub { net_state_all_cb($self, @_) });
72 $embed->signal_connect(progress => sub { progress_cb($self, @_) });
73 $embed->signal_connect(progress_all => sub { progress_all_cb($self, @_) });
74  
75 # Start off with a blank page
76 $embed->load_url('about:blank');
77 $window->show_all();
78  
79 $self->debug('Browser->new, main');
80 Gtk2->main; # quits after net_stop event fires
81  
82 return $self;
83 }
84  
85 =head2 $moz->embedded
86  
87 Return a reference to the embedded (L<Gtk2::MozEmbed|Gtk2::MozEmbed>)
88 widget.
89  
90 =cut
91  
92 sub embedded { $_[0]->{embed} }
93  
94  
95 sub quit {
96 my $self = shift;
97 warn "Browser->quit\n" if $self->{debug};
98  
99 $self->{window}->destroy();
100 }
101  
102  
103 sub debug {
104 my ($self, $msg) = @_;
105 if ($self->{debug}) {
106 my (undef, $file, $line) = caller();
107 print STDERR "$msg at $file line $line\n";
108 }
109 }
110  
111 # When there is a change in the progress of loading a document.
112 # The cur value indicates how much of the document has been downloaded.
113 # The max value indicates the length of the document. If the value of
114 # max is less than one the full length of the document can not be determined.
115 sub progress_cb {
116 my ($browser, $embed, $cur, $max) = @_;
117  
118 $browser->debug("progress signal: cur=$cur, max=$max");
119 }
120  
121 sub progress_all_cb {
122 my ($browser, $embed, $uri, $cur, $max) = @_;
123  
124 $browser->debug("progress_all signal: cur=$cur, max=$max, uri=$uri\n"
125 . " ref uri=", ref($uri));
126 }
127  
128 sub net_start_cb {
129 my ($browser, $data) = @_;
130  
131 $browser->debug("net_start signal");
132 $browser->{netstopped} = 0;
133 FALSE;
134 }
135  
136 sub net_stop_cb {
137 my ($browser, $embed, $data) = @_;
138  
139 $browser->debug("net_stop signal");
140 Glib::Idle->add(
141 sub {
142 $browser->debug("net_stop Idle, main_quit");
143 $browser->{netstopped} = 1;
144  
145 Gtk2->main_quit;
146 FALSE; # uninstall
147 }, undef, G_PRIORITY_LOW
148 );
149 FALSE; # let any other handlers run
150 }
151  
152 # I found it did something like this in
153 # http://code.google.com/p/google-gadgets-for-linux/source/browse/trunk/extensions/gtkmoz_browser_element/browser_child.cc
154 my $current_doc_loaded_mask = $STATE{STOP} | $STATE{IS_REQUEST};
155 sub net_state_cb {
156 my ($browser, $embed, $state_flags, $data) = @_;
157 $browser->debug("net_state_cb signal: flags=$state_flags, status=$data");
158  
159 if (($state_flags & $current_doc_loaded_mask) == $current_doc_loaded_mask) {
160 $browser->debug("current_doc loaded!");
161  
162 net_stop_cb($browser, $embed, $data);
163 }
164 }
165  
166 sub net_state_all_cb {
167 my ($browser, $embed, $uri, $state_flags, $data) = @_;
168  
169 $browser->debug("net_state_all: uri=$uri, flags=$state_flags, status=$data"
170 . "ref uri=", ref($uri));
171 }
172  
173  
174  
175 1;
176  
177 __END__