Firefox/Application/API40.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 124 CC 4 SCAR 14.8
CC
4
Cov
0%
autoclose_tab
12.0
addTab
6.0
updateitems
2.0
CRAP 4.4
1 package Firefox::Application::API40;
2 use strict;
3 use parent 'Firefox::Application';
4 use MozRepl::RemoteObject qw(as_list);
5 our $VERSION = '0.80';
6  
7 =head1 NAME
8  
9 Firefox::Application::API40 - API wrapper for Firefox 4+
10  
11 =head1 SYNOPSIS
12  
13 use Firefox::Application;
14 my $ff = Firefox::Application->new(
15 # Force the Firefox 4 API
16 api => 'Firefox::Application::API40',
17 );
18  
19 =head1 METHODS
20  
21 =head2 C<< $api->updateitems( %args ) >>
22  
23 for my $item ($api->updateitems) {
24 print sprintf "Name: %s\n", $item->{name};
25 print sprintf "Version: %s\n", $item->{version};
26 print sprintf "GUID: %s\n", $item->{id};
27 };
28  
29 Returns the list of updateable items. Under Firefox 4,
30 can be restricted by the C<type> option.
31  
32 =over 4
33  
34 =item * C<type> - type of items to fetch
35  
36 C<ANY> - fetch any item
37  
38 C<ADDON> - fetch extensions
39  
40 C<LOCALE> - fetch locales
41  
42 C<THEME> - fetch themes
43  
44 =back
45  
46 This method is asynchronous in Firefox 4, but is run
47 synchronously by Firefox::Application, polling every 0.1s.
48 Currently, no special support for AnyEvent is implemented.
49  
50 =cut
51  
52 our %addon_types = (
53 ADDON => 'extension',
54 THEME => 'theme',
55 );
56  
57 sub updateitems {
58 my ($self, %options) = @_;
59 my $repl = delete $options{ repl } || $self->repl;
60 my $type = $options{type} || $options{ ANY };
61
62 my $done;
63 my @res;
64 my $cb = sub {
65 my ($addons) = @_;
66 $done++; # This should be $cv->send ...
67 @res = @$addons;
68 };
69
70 my $addons_js = $repl->declare(sprintf( <<'JS', $type), 'list');
71 function(types,cb) {
72 Components.utils.import("resource://gre/modules/AddonManager.jsm");
73 AddonManager.getAddonsByTypes(types, cb);
74 return 1;
75 };
76 JS
77 $addons_js->([$type], $cb);
78 while (! $done) {
79 # AnyEvent!
80 $self->repl->poll;
81 };
82 @res
83 };
84  
85 =head2 C<< $ff->addons( %args ) >>
86  
87 for my $addon ($ff->addons) {
88 print sprintf "Name: %s\n", $addon->{name};
89 print sprintf "Version: %s\n", $addon->{version};
90 print sprintf "GUID: %s\n", $addon->{id};
91 };
92  
93 Returns the list of installed addons as C<Addon>s.
94 See <https://developer.mozilla.org/en/Addons/Add-on_Manager/Addon>
95 depending.
96  
97 =cut
98  
99 sub addons {
100 my $self = shift;
101 $self->updateitems(type => 'extension', @_);
102 };
103  
104 sub themes {
105 my $self = shift;
106 $self->updateitems(type => 'theme', @_);
107 };
108  
109 sub locales {
110 my $self = shift;
111 $self->updateitems(type => 'locale', @_);
112 };
113  
114 =head2 C<< $ff->addTab( %options ) >>
115  
116 my $new = $ff->addTab();
117  
118 Creates a new tab and returns it.
119 The tab will be automatically closed upon program exit.
120  
121 The Firefox 4 API is asynchronous. The method is forced
122 into a synchronous call here.
123  
124 =cut
125  
126 sub addTab {
127 my ($self, %options) = @_;
128 my $repl = $options{ repl } || $self->repl;
129  
130 my $tab = $self->browser( $repl )->addTab();
131  
132 if (not exists $options{ autoclose } or $options{ autoclose }) {
133 $self->autoclose_tab($tab)
134 };
135
136 $tab
137 };
138  
139 sub closeTab {
140 my ($self,$tab,$repl) = @_;
141 $repl ||= $self->repl;
142 my $close_tab = $repl->declare(<<'JS');
143 function(tab) {
144 if(tab.collapsed) { return };
145 var be = Components.classes["@mozilla.org/appshell/window-mediator;1"]
146 .getService(Components.interfaces.nsIWindowMediator)
147 .getEnumerator("navigator:browser");
148 while (be.hasMoreElements()) {
149 var browserWin = be.getNext();
150 var tabbrowser = browserWin.gBrowser;
151 if( tabbrowser ) {
152 for( var i=0; i< tabbrowser.tabs.length; i++) {
153 if( tabbrowser.tabs.item( i ) === tab ) {
154 tabbrowser.removeTab(tab);
155 break;
156 };
157 };
158 };
159 };
160 }
161 JS
162 return $close_tab->($tab);
163 }
164  
165 sub autoclose_tab {
166 my ($self,$tab,$close) = @_;
167 $close = 1
168 if( 2 == @_ );
169 my $release = join "\n",
170 # Find the window our tab lives in
171 q<if(!self.collapsed){>,
172 q<var be = Components.classes["@mozilla.org/appshell/window-mediator;1"]>,
173 q<.getService(Components.interfaces.nsIWindowMediator)>,
174 q<.getEnumerator("navigator:browser");>,
175 q<while (be.hasMoreElements()) {>,
176 q<var browserWin = be.getNext();>,
177 q<var tabbrowser = browserWin.gBrowser;>,
178 q<if( tabbrowser ) {>,
179 q!for( var i=0; i< tabbrowser.tabs.length; i++) {!,
180 q<if( tabbrowser.tabs.item( i ) === self ) {>,
181 q<tabbrowser.removeTab(self);>,
182 q<break;>,
183 q<};>,
184 q<};>,
185 q<};>,
186 q<};>,
187 q<};>,
188 ;
189 if( $close ) {
190 $tab->__release_action($release);
191 } else {
192 $tab->__release_action('');
193 };
194 };
195 =head2 C<< $ff->selectedTab( %options ) >>
196  
197 my $curr = $ff->selectedTab();
198  
199 Sets the currently active tab.
200  
201 =cut
202  
203 sub selectedTab {
204 my ($self,%options) = @_;
205 my $repl = delete $options{ repl } || $self->repl;
206 return $self->browser( $repl )->{selectedTab};
207 }
208  
209 sub openTabs {
210 my ($self,$repl) = @_;
211 $repl ||= $self->repl;
212 my $open_tabs = $repl->declare(<<'JS', 'list');
213 function() {
214 var idx = 0;
215 var tabs = [];
216
217 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
218 .getService(Components.interfaces.nsIWindowMediator);
219 var en = wm.getEnumerator('navigator:browser');
220 while( en.hasMoreElements() ) {
221 var win= en.getNext();
222 var tabbrowser = win.gBrowser;
223 var numTabs = tabbrowser.browsers.length;
224 for (var index = 0; index < numTabs; index++) {
225 var tab = tabbrowser.tabContainer.childNodes[index];
226 var d = tab.linkedBrowser.contentWindow.document;
227 tabs.push({
228 location: d.location.href,
229 document: d,
230 title: d.title,
231 "id": d.id,
232 index: idx++,
233 panel: tab.linkedPanel,
234 tab: tab,
235 });
236 };
237 };
238  
239 return tabs;
240 }
241 JS
242 $open_tabs->();
243 }
244  
245 =head2 C<< $api->element_query( \@elements, \%attributes ) >>
246  
247 my $query = $element_query(['input', 'select', 'textarea'],
248 { name => 'foo' });
249  
250 Returns the XPath query that searches for all elements with C<tagName>s
251 in C<@elements> having the attributes C<%attributes>. The C<@elements>
252 will form an C<or> condition, while the attributes will form an C<and>
253 condition.
254  
255 =cut
256  
257 sub element_query {
258 my ($self, $elements, $attributes) = @_;
259 my $query =
260 './/*[(' .
261 join( ' or ',
262 map {
263 sprintf qq{local-name(.)="%s"}, lc $_
264 } @$elements
265 )
266 . ') and '
267 . join( " and ",
268 map { sprintf q{@%s="%s"}, $_, $attributes->{$_} }
269 sort keys(%$attributes)
270 )
271 . ']';
272 };
273  
274 1;
275  
276 =head1 AUTHOR
277  
278 Max Maischein C<corion@cpan.org>
279  
280 =head1 COPYRIGHT (c)
281  
282 Copyright 2009-2013 by Max Maischein C<corion@cpan.org>.
283  
284 =head1 LICENSE
285  
286 This module is released under the same terms as Perl itself.
287  
288 =cut