~bwy/+junk/gnash-temp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// System.cpp:  ActionScript "System" class, for Gnash.
//
//   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
//   Foundation, Inc
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

#include "movie_root.h" // interface callback
#include "log.h"
#include "System_as.h"
#include "fn_call.h"
#include "smart_ptr.h" // for boost intrusive_ptr
#include "Global_as.h"
#include "builtin_function.h"
#include "NativeFunction.h" 
#include "VM.h" // for getPlayerVersion() 
#include "GnashAlgorithm.h"
#include "RunResources.h"

#include <sstream>

namespace gnash {


// Forward declarations.
namespace {

    inline std::string trueFalse(bool x) { return x ? "t" : "f"; }

    template<typename T> inline void convertValue(const std::string& in,
            T& val);

    const std::string& systemLanguage(as_object& proto);

    as_value system_security_allowdomain(const fn_call& fn);
    as_value system_security_allowinsecuredomain(const fn_call& fn);
    as_value system_security_loadpolicyfile(const fn_call& fn);
    as_value system_setClipboard(const fn_call& fn);
    as_value system_showsettings(const fn_call& fn);
    as_value system_exactsettings(const fn_call& fn);
    as_value system_usecodepage(const fn_call& fn);
    void attachSystemSecurityInterface(as_object& o);
    void attachSystemCapabilitiesInterface(as_object& o);
    void attachSystemInterface(as_object& proto);
    
    // AS3 functions.
    as_value system_gc(const fn_call& fn);
    as_value system_pause(const fn_call& fn);
    as_value system_resume(const fn_call& fn);

	// List of domains that can access/modify local data
	std::vector<std::string> _allowDataAccess;
}


void
system_class_init(as_object& where, const ObjectURI& uri)
{
    registerBuiltinObject(where, attachSystemInterface, uri);
}


void
registerSystemNative(as_object& where)
{
    VM& vm = getVM(where);
    
    vm.registerNative(system_security_allowdomain, 12, 0);
    vm.registerNative(system_showsettings, 2107, 0);
    
    // From http://osflash.org/flashcoders/undocumented/asnative
    
    // Run once in startup script then deleted...
    // System.Capabilities.Query 11, 0    
    
    // System.Product.isRunning 2201, 0
    // System.Product.isInstalled 2201, 1
    // System.Product.launch 2201, 2
    // System.Product.download 2201, 3    
}


/// Get the current System.security allowDataAccess list of domains allowed to
/// access/modify local data
//
/// @return a std::vector of strings containing urls that can access local data
const std::vector<std::string>&
getAllowDataAccess()
{
	return _allowDataAccess;
}


/// Adds a string containing url or ip info to the allowDataAccess list of
/// domains that can access/modify local data
//
/// @param url a std::string containing the domain name
bool
addAllowDataAccess( const std::string& url )
{
	size_t s = _allowDataAccess.size();
	_allowDataAccess.push_back( url );	

	if( s+1 == _allowDataAccess.size()) return true;

	return false;
}


namespace {

void
attachSystemSecurityInterface(as_object& o)
{
    VM& vm = getVM(o);
    o.init_member("allowDomain", vm.getNative(12, 0));

    Global_as& gl = getGlobal(o);
    // TODO: only available when SWF >= 7 
    o.init_member("allowInsecureDomain",
            gl.createFunction(system_security_allowinsecuredomain));
    o.init_member("loadPolicyFile",
            gl.createFunction(system_security_loadpolicyfile));
}

void
attachSystemCapabilitiesInterface(as_object& o)
{
	RcInitFile& rcfile = RcInitFile::getDefaultInstance();

    //
    // Filesystem, access, miscellaneous hardware information
    //

    // "Windows XP", "Windows 2000", "Windows NT", "Windows 98/ME",
    // "Windows 95", "Windows CE", "Linux", "MacOS"
    // Override in gnashrc
    VM& vm = getVM(o);
    
    const std::string os = vm.getOSName();

    const std::string language = systemLanguage(o);

    // FIXME: these need to be implemented properly 
    // Does the NetStream object natively support SSL?
	const bool hasTLS = true;

    // Microphone and camera access disabled
	const bool avHardwareDisable = false;
	
	// Not sure: seems to be whether the movie can 'float' above web pages,
	// and is useful for disabling certain annoying adverts.
	const bool windowlessDisable = false;

	const bool hasPrinting = true;
	const bool hasAccessibility = true;
	const bool isDebugger = false;
	const bool localFileReadDisable = false;

    //
    // Display information (needs active GUI)
    //

    const movie_root& m = vm.getRoot();

    int screenResolutionX;
    convertValue(m.callInterface("System.capabilities.screenResolutionX"),
            screenResolutionX);
    int screenResolutionY;
    convertValue(m.callInterface("System.capabilities.screenResolutionY"),
            screenResolutionY);
    int screenDPI;
    convertValue(m.callInterface("System.capabilities.screenDPI"), screenDPI);
        
    // Documented to be a number, but is in fact a string.
    const std::string pixelAspectRatio = 
        m.callInterface("System.capabilities.pixelAspectRatio");

    // "StandAlone", "External", "PlugIn", "ActiveX" (get from GUI)
    const std::string playerType =
        m.callInterface("System.capabilities.playerType");

    const std::string screenColor =
        m.callInterface("System.capabilities.screenColor");

    //
    // Media
    //
        
    // Is audio available?
    const bool hasAudio = (vm.getRoot().runResources().soundHandler());

    // FIXME: these need to be implemented properly. They are mostly
    // self-explanatory.
    const bool hasAudioEncoder = true;
    const bool hasEmbeddedVideo = true;
    const bool hasIME = true;
    const bool hasMP3 = true;
    const bool hasScreenBroadcast = true;
    const bool hasScreenPlayback = true;
    const bool hasStreamingAudio = true;
    const bool hasStreamingVideo = true;
    const bool hasVideoEncoder = true;

    //
    // Player version
    //

    // "LNX 9,0,22,0", "MAC 8,0,99,0"
    // Override in gnashrc
    const std::string version = vm.getPlayerVersion();

    // "Macromedia Windows", "Macromedia Linux", "Macromedia MacOS"
    // Override in gnashrc
    const std::string manufacturer = rcfile.getFlashSystemManufacturer();
    
    // serverString
	// A URL-encoded string to send system info to a server.
	// Boolean values are represented as t or f.		
	// Privacy concerns should probably be addressed by 	
	// allowing this string to be sent or not; individual	
	// values that might affect privacy can be overridden	
	// in gnashrc.
	
	// hasIME seems not to be included in the server string, though
	// it is documented to have a server string of IME.
	// Linux player version 9 has no hasIME property (but no need
	// to emulate that.)
	
	// TLS and hasTLS are documented for AS3, player version 9.
	//
	// WD is included in the server string for player version 9,
	// but not documented. It corresponds to the equally undocumented
	// windowlessDisable.
	
	// This should be the standard order of parameters in the server
	// string.
	std::ostringstream serverString;
	serverString << "A="    << trueFalse(hasAudio)
			<< "&SA="	<< trueFalse(hasStreamingAudio)
			<< "&SV="	<< trueFalse(hasStreamingVideo)
			<< "&EV="	<< trueFalse(hasEmbeddedVideo)
			<< "&MP3="	<< trueFalse(hasMP3)						
			<< "&AE="	<< trueFalse(hasAudioEncoder)
			<< "&VE="	<< trueFalse(hasVideoEncoder)
			<< "&ACC="	<< trueFalse(hasAccessibility)
			<< "&PR="	<< trueFalse(hasPrinting)
			<< "&SP="	<< trueFalse(hasScreenPlayback) 
			<< "&SB="	<< trueFalse(hasScreenBroadcast) 
			<< "&DEB="	<< trueFalse(isDebugger)
			<< "&V="    << URL::encode(version)
			<< "&M="    << URL::encode(manufacturer)
			<< "&R="    << screenResolutionX << "x" << screenResolutionY
			<< "&DP="	<< screenDPI
			<< "&COL="	<< screenColor					
			<< "&AR="   << pixelAspectRatio
			<< "&OS="   << URL::encode(os)
			<< "&L="    << language			
			<< "&PT="   << playerType
			<< "&AVD="	<< trueFalse(avHardwareDisable) 
			<< "&LFD="	<< trueFalse(localFileReadDisable)
			<< "&WD="   << trueFalse(windowlessDisable)
			<< "&TLS="	<< trueFalse(hasTLS);
	
    const int flags = PropFlags::dontDelete
                    | PropFlags::dontEnum
                    | PropFlags::readOnly;

    o.init_member("version", version, flags);
    o.init_member("playerType", playerType, flags);
    o.init_member("os", os, flags);
    o.init_member("manufacturer", manufacturer, flags);
    o.init_member("language", language, flags);
    o.init_member("hasAudio", hasAudio, flags);
    o.init_member("screenResolutionX", screenResolutionX, flags);
    o.init_member("screenResolutionY", screenResolutionY, flags);
    o.init_member("screenColor", screenColor, flags);
    o.init_member("screenDPI", screenDPI, flags);
    o.init_member("pixelAspectRatio", pixelAspectRatio, flags);
    o.init_member("serverString", serverString.str(), flags);
    o.init_member("avHardwareDisable", avHardwareDisable, flags);
    o.init_member("hasAudioEncoder", hasAudioEncoder, flags);
    o.init_member("hasEmbeddedVideo", hasEmbeddedVideo, flags);
    o.init_member("hasIME", hasIME, flags);
    o.init_member("hasMP3", hasMP3, flags);
    o.init_member("hasPrinting", hasPrinting, flags);
    o.init_member("hasScreenBroadcast", hasScreenBroadcast, flags);
    o.init_member("hasScreenPlayback", hasScreenPlayback, flags);
    o.init_member("hasStreamingAudio", hasStreamingAudio, flags);
    o.init_member("hasStreamingVideo", hasStreamingVideo, flags);
    o.init_member("hasVideoEncoder", hasVideoEncoder, flags);
    o.init_member("hasAccessibility", hasAccessibility, flags);
    o.init_member("isDebugger", isDebugger, flags);
    o.init_member("localFileReadDisable", localFileReadDisable, flags);
    o.init_member("hasTLS", hasTLS, flags);
    o.init_member("windowlessDisable", windowlessDisable, flags);
}

/// Convert a string to the type passed in, making sure the target variable
/// is initialized.
template<typename T>
inline void
convertValue(const std::string& in, T& val)
{
    std::istringstream is(in);
    if (!(is >> val)) val = T();
} 

void
attachSystemInterface(as_object& proto)
{
    Global_as& gl = getGlobal(proto);

    string_table& st = getStringTable(proto);
    registerBuiltinObject(proto, attachSystemSecurityInterface,
            ObjectURI(st.find("security"), 0));
    registerBuiltinObject(proto, attachSystemCapabilitiesInterface,
            ObjectURI(st.find("capabilities"), 0));

	proto.init_member("setClipboard", 
            gl.createFunction(system_setClipboard));
	
    VM& vm = getVM(proto);
	proto.init_member("showSettings", vm.getNative(2107, 0));
	proto.init_property("useCodepage", &system_usecodepage,
            &system_usecodepage);

    const int flags = PropFlags::dontDelete
                    | PropFlags::dontEnum
                    | PropFlags::readOnly
                    | PropFlags::onlySWF6Up;

    proto.init_property("exactSettings", &system_exactsettings,
            &system_exactsettings, flags);

}

// This function returns false if no arguments were passed, true if any
// arguments were passed at all, even if they are not strings. There is
// currently no known way of accessing the list of allowed domains.
as_value
system_security_allowdomain(const fn_call& fn)
{
	// NOTE: This is the AS2 version of allowDomain, the AS3 version is located
	// in Security_as.cpp
    if (!fn.nargs) {
        IF_VERBOSE_ASCODING_ERRORS(
            log_aserror("System.security.allowDomain requires at least one "
                "argument.");
        );
        return as_value(false);
    }

    LOG_ONCE(log_unimpl ("System.security.allowDomain currently stores "
                "domains but does nothing else.")); 
	for (unsigned int i = 0; i < fn.nargs; ++i) {
		addAllowDataAccess(fn.arg(i).to_string());
	}
    return as_value(true);
}


as_value
system_security_allowinsecuredomain(const fn_call& /*fn*/)
{
    LOG_ONCE(log_unimpl ("System.security.allowInsecureDomain") );
    return as_value();
}


as_value
system_security_loadpolicyfile(const fn_call& /*fn*/)
{
    LOG_ONCE(log_unimpl ("System.security.loadPolicyFile") );
    return as_value();
}

as_value
system_setClipboard(const fn_call& /*fn*/)
{
    LOG_ONCE(log_unimpl ("System.setClipboard") );
    return as_value();
}

as_value
system_showsettings(const fn_call& /*fn*/)
{
    LOG_ONCE(log_unimpl ("System.showSettings") );
    return as_value();
}

as_value
system_gc(const fn_call& /*fn*/)
{
    log_unimpl (__FUNCTION__);
    return as_value();
}

as_value
system_pause(const fn_call& /*fn*/)
{
    log_unimpl (__FUNCTION__);
    return as_value();
}

as_value
system_resume(const fn_call& /*fn*/)
{
    log_unimpl (__FUNCTION__);
    return as_value();
}


// FIXME: should return true if shared object files
// are stored under an exact domain name (www.gnashdev.org or
// gnashdev.org); false if both are stored under gnashdev.org.
// Can be set.
as_value
system_exactsettings(const fn_call& fn)
{

    // Getter
    if (fn.nargs == 0) {
        // Is always true until we implement it.
        return as_value(true);   
    }
    
    // Setter
    else {
        LOG_ONCE(log_unimpl ("System.exactSettings") );
        return as_value();
    }
}


// FIXME: if true, SWF6+ should treat DisplayObjects as Latin
// charset variants. If false (default), as UtrueFalse-8.
// Can be set.
as_value
system_usecodepage(const fn_call& fn)
{
    // Getter
    if (fn.nargs == 0)
    {
        // Is always false until we implement it.
        return as_value(false);   
    }
    
    // Setter
    else 
    {
        LOG_ONCE(log_unimpl ("System.useCodepage") );
        return as_value();
    }
}



const std::string&
systemLanguage(as_object& proto)
{
	// Two-letter language code ('en', 'de') corresponding to ISO 639-1
	// Chinese can be either zh-CN or zh-TW. English used to have a 
	// country (GB, US) qualifier, but that was dropped in version 7 of
    // the player.
 	// This method relies on getting a POSIX-style language code of the form
	// "zh_TW.utf8", "zh_CN" or "it" from the VM.
	// It is obviously very easy to extend support to all language codes, but
	// some scripts rely on there being only 20 possible languages. It could
	// be a run time option if it's important enough to care.

	static std::string lang = getVM(proto).getSystemLanguage();
	
	const char* languages[] = {"en", "fr", "ko", "ja", "sv",
				"de", "es", "it", "zh", "pt",
				"pl", "hu", "cs", "tr", "fi",
				"da", "nl", "no", "ru"};
	
	const size_t size = arraySize(languages);
	
	if (std::find(languages, languages + size, lang.substr(0, 2)) !=
            languages + size)
	{
		if (lang.substr(0,2) == "zh")
		{
			// Chinese is the only language since the pp version 7
			// to need an additional qualifier.
			if (lang.substr(2, 3) == "_TW") lang = "zh-TW";
			else if (lang.substr(2, 3) == "_CN") lang = "zh-CN";
			else lang = "xu";
		}
		else
		{
			// All other matching cases: retain just the first
			// two DisplayObjects.
			lang.erase(2);
		}
	}
	else
	{
		// Unknown language. We also return this if
		// getSystemLanguage() returns something unexpected. 
		lang = "xu";
	}

	return lang;

}

} // anonymous namespace
} // gnash namespace