use ObjectURI more consistently
[gnash.git] / libcore / asobj / LoadVars_as.cpp
blob60f7f383810dcd5d53057f92c1ecef97adee7a05
1 // LoadVars.cpp: ActionScript "LoadVars" class (HTTP variables), for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
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
24 #include "log.h"
25 #include "fn_call.h"
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
32 #include "VM.h"
33 #include "namedStrings.h"
34 #include "PropertyList.h"
35 #include "Global_as.h"
37 #include <list>
38 #include <boost/algorithm/string/case_conv.hpp>
39 #include <functional>
41 //#define DEBUG_LOADS 1
43 namespace gnash {
45 namespace {
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)
55 void
56 loadvars_class_init(as_object& where, const ObjectURI& uri)
58 registerBuiltinClass(where, loadvars_ctor, attachLoadVarsInterface, 0, uri);
61 namespace {
63 void
64 attachLoadVarsInterface(as_object& o)
66 Global_as& gl = getGlobal(o);
67 VM& vm = getVM(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);
85 as_value
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
94 as_value src;
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);
101 else {
102 VM& vm = getVM(fn);
103 // TODO: use NSV
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);
111 return as_value();
114 as_value
115 loadvars_onLoad(const fn_call& /*fn*/)
117 //GNASH_REPORT_FUNCTION;
118 return as_value();
122 as_value
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());
149 as_value
150 loadvars_ctor(const fn_call& fn)
153 IF_VERBOSE_ASCODING_ERRORS(
154 if (fn.nargs) {
155 std::ostringstream ss;
156 fn.dump_args(ss);
157 log_aserror("new LoadVars(%s) - arguments discarded",
158 ss.str());
162 return as_value(); // will keep alive
165 } // anonymous namespace
166 } // end of gnash namespace