1 // LoadVars.cpp: ActionScript "LoadVars" class (HTTP variables), for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "LoadableObject.h"
22 #include "LoadVars_as.h"
23 #include "as_object.h" // for inheritance
26 #include "Global_as.h"
27 #include "smart_ptr.h" // GNASH_USE_GC
28 #include "builtin_function.h" // need builtin_function
29 #include "NativeFunction.h" // need builtin_function
30 #include "as_function.h" // for calling event handlers
31 #include "as_value.h" // for setting up a fn_call
33 #include "namedStrings.h"
34 #include "PropertyList.h"
35 #include "Global_as.h"
38 #include <boost/algorithm/string/case_conv.hpp>
41 //#define DEBUG_LOADS 1
47 as_value
loadvars_tostring(const fn_call
& fn
);
48 as_value
loadvars_ctor(const fn_call
& fn
);
49 as_value
loadvars_onLoad(const fn_call
& fn
);
50 as_value
loadvars_onData(const fn_call
& fn
);
51 void attachLoadVarsInterface(as_object
& o
);
54 // extern (used by Global.cpp)
56 loadvars_class_init(as_object
& where
, const ObjectURI
& uri
)
58 registerBuiltinClass(where
, loadvars_ctor
, attachLoadVarsInterface
, 0, uri
);
64 attachLoadVarsInterface(as_object
& o
)
66 Global_as
& gl
= getGlobal(o
);
69 const int flags
= as_object::DefaultFlags
| PropFlags::onlySWF6Up
;
71 o
.init_member("decode", vm
.getNative(301, 3), flags
);
72 o
.init_member("load", vm
.getNative(301, 0), flags
);
73 o
.init_member("send", vm
.getNative(301, 1), flags
);
74 o
.init_member("sendAndLoad", vm
.getNative(301, 2), flags
);
76 /// This handles getBytesLoaded, getBytesTotal, and addRequestHeader
77 attachLoadableInterface(o
, flags
);
79 o
.init_member("toString", gl
.createFunction(loadvars_tostring
), flags
);
80 o
.init_member("onData", gl
.createFunction(loadvars_onData
), flags
);
81 o
.init_member("onLoad", gl
.createFunction(loadvars_onLoad
), flags
);
82 o
.init_member("contentType", "application/x-www-form-urlencoded", flags
);
86 loadvars_onData(const fn_call
& fn
)
89 as_object
* thisPtr
= fn
.this_ptr
;
90 if (!thisPtr
) return as_value();
92 // See http://gitweb.freedesktop.org/?p=swfdec/swfdec.git;a=blob;f=libswfdec/swfdec_initialize.as
95 if (fn
.nargs
) src
= fn
.arg(0);
97 if (src
.is_undefined()) {
98 thisPtr
->set_member(NSV::PROP_LOADED
, false);
99 callMethod(thisPtr
, NSV::PROP_ON_LOAD
, false);
104 const ObjectURI
& decodeKey
= getURI(vm
, "decode");
106 thisPtr
->set_member(NSV::PROP_LOADED
, true);
107 callMethod(thisPtr
, decodeKey
, src
);
108 callMethod(thisPtr
, NSV::PROP_ON_LOAD
, true);
115 loadvars_onLoad(const fn_call
& /*fn*/)
117 //GNASH_REPORT_FUNCTION;
123 loadvars_tostring(const fn_call
& fn
)
125 as_object
* ptr
= ensure
<ValidThis
>(fn
);
127 SortedPropertyList vars
= enumerateProperties(*ptr
);
129 as_object
* global
= &getGlobal(*ptr
);
130 std::ostringstream o
;
132 const int ver
= getSWFVersion(fn
);
133 string_table
& st
= getStringTable(fn
);
135 // LoadVars.toString() calls _global.escape().
136 for (SortedPropertyList::const_reverse_iterator it
= vars
.rbegin(),
137 itEnd
= vars
.rend(); it
!= itEnd
; ++it
) {
139 if (it
!= vars
.rbegin()) o
<< "&";
140 const std::string
& var
=
141 callMethod(global
, NSV::PROP_ESCAPE
, it
->first
.toString(st
)).to_string();
142 const std::string
& val
= callMethod(global
, NSV::PROP_ESCAPE
,
143 it
->second
.to_string(ver
)).to_string();
144 o
<< var
<< "=" << val
;
146 return as_value(o
.str());
150 loadvars_ctor(const fn_call
& fn
)
153 IF_VERBOSE_ASCODING_ERRORS(
155 std::ostringstream ss
;
157 log_aserror("new LoadVars(%s) - arguments discarded",
162 return as_value(); // will keep alive
165 } // anonymous namespace
166 } // end of gnash namespace