aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llwebbrowserctrl.cpp
blob: 7fedc4c25583b6aa7cbfd4312cad03b1f3a5f8a9 (plain)
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/**
 * @file llwebbrowserctrl.cpp
 * @brief Web browser UI control
 *
 * $LicenseInfo:firstyear=2006&license=viewergpl$
 * 
 * Copyright (c) 2006-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#if LL_LIBXUL_ENABLED

#include "llwebbrowserctrl.h"

// viewer includes
#include "llfloaterworldmap.h"
#include "llurldispatcher.h"
#include "llviewborder.h"
#include "llviewerwindow.h"
#include "llfloaterhtml.h"
#include "llweb.h"
#include "viewer.h"

// linden library includes
#include "llfocusmgr.h"

// Setting the mozilla buffer width to 2048 exactly doesn't work, since it pads its rowbytes a bit, pushing the texture width over 2048.
// 2000 should give enough headroom for any amount of padding it cares to add.
const S32 MAX_DIMENSION = 2000;
const S32 MAX_TEXTURE_DIMENSION = 2048;

LLWebBrowserCtrl::LLWebBrowserCtrl( const std::string& name, const LLRect& rect ) :
	LLUICtrl( name, rect, FALSE, NULL, NULL ),
	mTextureDepthBytes( 4 ),
	mEmbeddedBrowserWindowId( 0 ),
	mBorder(NULL),
	mFrequentUpdates( true ),
	mOpenLinksInExternalBrowser( false ),
	mOpenLinksInInternalBrowser( false ),
	mOpenSLURLsInMap( true ),
	mOpenSLURLsViaTeleport( false ),
	mHomePageUrl( "" ),
	mIgnoreUIScale( true ),
	mAlwaysRefresh( false )
{
	S32 screen_width = mIgnoreUIScale ? llround((F32)mRect.getWidth() * LLUI::sGLScaleFactor.mV[VX]) : llround((F32)mRect.getWidth() * gViewerWindow->getDisplayScale().mV[VX]);
	S32 screen_height = mIgnoreUIScale ? llround((F32)mRect.getHeight() * LLUI::sGLScaleFactor.mV[VY]) : llround((F32)mRect.getHeight() * gViewerWindow->getDisplayScale().mV[VY]);

	// create a new browser window
	{
#if LL_LINUX
		// Yuck, Mozilla init plays with the locale - push/pop
		// the locale to protect it, as exotic/non-C locales
		// causes our code lots of general critical weirdness
		// and crashness. (SL-35450)
		std::string saved_locale = setlocale(LC_ALL, NULL);
#endif // LL_LINUX
		mEmbeddedBrowserWindowId = LLMozLib::getInstance()->createBrowserWindow( gViewerWindow->getPlatformWindow(), screen_width, screen_height );
#if LL_LINUX
		setlocale(LC_ALL, saved_locale.c_str() );
#endif // LL_LINUX
	}

	// change color to black so transisitons aren't so noticable (this should be in XML eventually)
	LLMozLib::getInstance()->setBackgroundColor( mEmbeddedBrowserWindowId, 0x00, 0x00, 0x00 );

	// observe the browser so we can trap HREF events)
	LLMozLib::getInstance()->addObserver( mEmbeddedBrowserWindowId, this );

	// create a new texture (based on LLDynamic texture) that will be used to display the output
	mWebBrowserImage = new LLWebBrowserTexture( screen_width, screen_height, this, mEmbeddedBrowserWindowId );

	LLRect border_rect( 0, mRect.getHeight() + 2, mRect.getWidth() + 2, 0 );
	mBorder = new LLViewBorder( "web control border", border_rect, LLViewBorder::BEVEL_IN );
	addChild( mBorder );
}

////////////////////////////////////////////////////////////////////////////////
// note: this is now a singleton and destruction happens via initClass() now
LLWebBrowserCtrl::~LLWebBrowserCtrl()
{
	LLMozLib::getInstance()->remObserver( mEmbeddedBrowserWindowId, this );

	LLMozLib::getInstance()->destroyBrowserWindow( mEmbeddedBrowserWindowId );

	if ( mWebBrowserImage )
	{
		delete mWebBrowserImage;
		mWebBrowserImage = 0;
	};
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::addObserver( LLWebBrowserCtrlObserver* subjectIn )
{
	return mEventEmitter.addObserver( subjectIn );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::remObserver( LLWebBrowserCtrlObserver* subjectIn )
{
	return mEventEmitter.remObserver( subjectIn );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::setBorderVisible( BOOL border_visible )
{
	if ( mBorder )
	{
		mBorder->setVisible( border_visible );
	};
};

////////////////////////////////////////////////////////////////////////////////
// set flag that forces the embedded browser to open links in the external system browser
void LLWebBrowserCtrl::setOpenInExternalBrowser( bool valIn )
{
	mOpenLinksInExternalBrowser = valIn;
};

////////////////////////////////////////////////////////////////////////////////
// set flag that forces the embedded browser to open links in the internal browser floater
void LLWebBrowserCtrl::setOpenInInternalBrowser( bool valIn )
{
	mOpenLinksInInternalBrowser = valIn;
};

////////////////////////////////////////////////////////////////////////////////
// open secondlife:// links in map automatically or not
void LLWebBrowserCtrl::setOpenSLURLsInMap( bool valIn )
{
	mOpenSLURLsInMap = valIn;
};

////////////////////////////////////////////////////////////////////////////////
// teleport directly to secondlife:// links
void LLWebBrowserCtrl::setOpenSLURLsViaTeleport( bool valIn )
{
	mOpenSLURLsViaTeleport = valIn;
};

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserCtrl::handleHover( S32 x, S32 y, MASK mask )
{
	convertInputCoords(x, y);
	LLMozLib::getInstance()->mouseMove( mEmbeddedBrowserWindowId, x, y );

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserCtrl::handleScrollWheel( S32 x, S32 y, S32 clicks )
{
	LLMozLib::getInstance()->scrollByLines( mEmbeddedBrowserWindowId, clicks );

	// note: this isn't really necessary right now since the page is updated
	// on a timer but if that becomes too burdensome and the page is only updated
	// once after load then this will be necessary
	LLMozLib::getInstance()->grabBrowserWindow( mEmbeddedBrowserWindowId );

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserCtrl::handleMouseUp( S32 x, S32 y, MASK mask )
{
	convertInputCoords(x, y);
	LLMozLib::getInstance()->mouseUp( mEmbeddedBrowserWindowId, x, y );

	gViewerWindow->setMouseCapture( NULL );

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserCtrl::handleMouseDown( S32 x, S32 y, MASK mask )
{
	convertInputCoords(x, y);
	LLMozLib::getInstance()->mouseDown( mEmbeddedBrowserWindowId, x, y );

	gViewerWindow->setMouseCapture( this );

	setFocus( TRUE );

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::onFocusReceived()
{
	LLMozLib::getInstance()->focusBrowser( mEmbeddedBrowserWindowId, true );

	LLUICtrl::onFocusReceived();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::onFocusLost()
{
	LLMozLib::getInstance()->focusBrowser( mEmbeddedBrowserWindowId, false );

	gViewerWindow->focusClient();

	LLUICtrl::onFocusLost();
}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserCtrl::handleKey( KEY key, MASK mask, BOOL called_from_parent )
{
	unsigned long nskey;

	// First, turn SL internal keycode enum into Mozilla keycode enum

	// We don't have to deal with printable characters here - they should
	// go through handleUnicodeChar().  This table could be more complete
	// than it is, but I think this covers all of the important
	// non-printables.
	switch (key)
	{
	case KEY_BACKSPACE:
		nskey = LL_DOM_VK_BACK_SPACE; break;
	case KEY_TAB:
		nskey = LL_DOM_VK_TAB; break;
	case KEY_RETURN:
		nskey = LL_DOM_VK_RETURN; break;
	case KEY_PAD_RETURN:
		nskey = LL_DOM_VK_ENTER; break;
	case KEY_ESCAPE:
		nskey = LL_DOM_VK_ESCAPE; break;
	case KEY_PAGE_UP:
		nskey = LL_DOM_VK_PAGE_UP; break;
	case KEY_PAGE_DOWN:
		nskey = LL_DOM_VK_PAGE_DOWN; break;
	case KEY_END:
		nskey = LL_DOM_VK_END; break;
	case KEY_HOME:
		nskey = LL_DOM_VK_HOME; break;
	case KEY_LEFT:
		nskey = LL_DOM_VK_LEFT; break;
	case KEY_UP:
		nskey = LL_DOM_VK_UP; break;
	case KEY_RIGHT:
		nskey = LL_DOM_VK_RIGHT; break;
	case KEY_DOWN:
		nskey = LL_DOM_VK_DOWN; break;
	case KEY_INSERT:
		nskey = LL_DOM_VK_INSERT; break;
	case KEY_DELETE:
		nskey = LL_DOM_VK_DELETE; break;
	default:
		llinfos << "Don't know how to map LL keycode " << U32(key)
			<< " to DOM key.  Ignoring." << llendl;
		return FALSE; // don't know how to map this key.
	}

	LLMozLib::getInstance()->keyPress( mEmbeddedBrowserWindowId, nskey );

	return TRUE;
}

BOOL LLWebBrowserCtrl::handleUnicodeChar(llwchar uni_char, BOOL called_from_parent)
{
	// only accept 'printable' characters, sigh...
	if (uni_char >= 32 // discard 'control' characters
	    && uni_char != 127) // SDL thinks this is 'delete' - yuck.
	{
		LLMozLib::getInstance()->unicodeInput( mEmbeddedBrowserWindowId, uni_char );
	}

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::onVisibilityChange ( BOOL new_visibility )
{
	// set state of frequent updates automatically if visibility changes
	if ( new_visibility )
	{
		mFrequentUpdates = true;
	}
	else
	{
		mFrequentUpdates = false;
	}
	LLUICtrl::onVisibilityChange(new_visibility);
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::reshape( S32 width, S32 height, BOOL called_from_parent )
{
	S32 screen_width = mIgnoreUIScale ? llround((F32)width * LLUI::sGLScaleFactor.mV[VX]) : llround((F32)width * gViewerWindow->getDisplayScale().mV[VX]);
	S32 screen_height = mIgnoreUIScale ? llround((F32)height * LLUI::sGLScaleFactor.mV[VY]) : llround((F32)height * gViewerWindow->getDisplayScale().mV[VY]);

	// when floater is minimized, these sizes are negative
	if ( screen_height > 0 && screen_width > 0 )
	{
		mWebBrowserImage->resize( screen_width, screen_height );
	}

	LLUICtrl::reshape( width, height, called_from_parent );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::navigateBack()
{
	LLMozLib::getInstance()->navigateBack( mEmbeddedBrowserWindowId );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::navigateForward()
{
	LLMozLib::getInstance()->navigateForward( mEmbeddedBrowserWindowId );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::canNavigateBack()
{
	return LLMozLib::getInstance()->canNavigateBack( mEmbeddedBrowserWindowId );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::canNavigateForward()
{
	return LLMozLib::getInstance()->canNavigateForward( mEmbeddedBrowserWindowId );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::set404RedirectUrl(  std::string redirect_url )
{
	return LLMozLib::getInstance()->set404RedirectUrl( mEmbeddedBrowserWindowId, redirect_url );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLWebBrowserCtrl::clr404RedirectUrl()
{
	return LLMozLib::getInstance()->clr404RedirectUrl( mEmbeddedBrowserWindowId );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::navigateTo( std::string urlIn )
{
	const std::string protocol( "secondlife://" );
	const std::string protocol2( "sl://" );

	// don't browse to anything that starts with secondlife://
	if ( urlIn.length() >= protocol.length() )
	{
		if ( LLString::compareInsensitive( urlIn.substr( 0, protocol.length() ).c_str(), protocol.c_str() ) != 0 )
		{
			LLMozLib::getInstance()->navigateTo( mEmbeddedBrowserWindowId, urlIn );
		}
	}
	else if ( urlIn.length() >= protocol2.length() )
	{
		if ( LLString::compareInsensitive( urlIn.substr( 0, protocol2.length() ).c_str(), protocol2.c_str() ) != 0 )
		{
			LLMozLib::getInstance()->navigateTo( mEmbeddedBrowserWindowId, urlIn );
		}
	}
	else
	{
		LLMozLib::getInstance()->navigateTo( mEmbeddedBrowserWindowId, urlIn );
	}
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::navigateHome()
{
	if( mHomePageUrl.length() )
	{
		LLMozLib::getInstance()->navigateTo( mEmbeddedBrowserWindowId, mHomePageUrl );
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::setHomePageUrl( const std::string urlIn )
{
	mHomePageUrl = urlIn;
}

////////////////////////////////////////////////////////////////////////////////
//
std::string LLWebBrowserCtrl::getHomePageUrl()
{
	return 	mHomePageUrl;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserCtrl::draw()
{
	if ( ! getVisible() )
		return;

	// NOTE: optimization needed here - probably only need to do this once
	// unless tearoffs change the parent which they probably do.
	LLUICtrl* ptr = (LLUICtrl*)findRootMostFocusRoot();
	if ( ptr && ptr->hasFocus() )
	{
		setFrequentUpdates( true );
	}
	else
	{
		setFrequentUpdates( false );
	};

	// alpha off for this
	LLGLSUIDefault gls_ui;
	LLGLDisable gls_alphaTest( GL_ALPHA_TEST );

	glPushMatrix();
	{
		if (mIgnoreUIScale)
		{
			glLoadIdentity();
			// font system stores true screen origin, need to scale this by UI scale factor
			// to get render origin for this view (with unit scale)
			glTranslatef(floorf(LLFontGL::sCurOrigin.mX * LLUI::sGLScaleFactor.mV[VX]), 
						floorf(LLFontGL::sCurOrigin.mY * LLUI::sGLScaleFactor.mV[VY]), 
						LLFontGL::sCurOrigin.mZ);
		}

		// scale texture to fit the space using texture coords
		mWebBrowserImage->bindTexture();
		glColor4fv( LLColor4::white.mV );
		F32 max_u = ( F32 )mWebBrowserImage->getBrowserWidth() / ( F32 )mWebBrowserImage->getWidth();
		F32 max_v = ( F32 )mWebBrowserImage->getBrowserHeight() / ( F32 )mWebBrowserImage->getHeight();

		// draw the browser
		glBlendFunc( GL_ONE, GL_ZERO );
		glBegin( GL_QUADS );
		{
			// render using web browser reported width and height, instead of trying to invert GL scale
			glTexCoord2f( max_u, 0.f );
			glVertex2i( mWebBrowserImage->getBrowserWidth(), mWebBrowserImage->getBrowserHeight() );

			glTexCoord2f( 0.f, 0.f );
			glVertex2i( 0, mWebBrowserImage->getBrowserHeight() );

			glTexCoord2f( 0.f, max_v );
			glVertex2i( 0, 0 );

			glTexCoord2f( max_u, max_v );
			glVertex2i( mWebBrowserImage->getBrowserWidth(), 0 );
		}
		glEnd();
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA  );
	}
	glPopMatrix();

	// highlight if keyboard focus here. (TODO: this needs some work)
	if ( mBorder->getVisible() )
		mBorder->setKeyboardFocusHighlight( gFocusMgr.childHasKeyboardFocus( this ) );


	LLUICtrl::draw();
}

void LLWebBrowserCtrl::convertInputCoords(S32& x, S32& y)
{
	x = mIgnoreUIScale ? llround((F32)x * LLUI::sGLScaleFactor.mV[VX]) : llround((F32)x * gViewerWindow->getDisplayScale().mV[VX]);
	y = mIgnoreUIScale ? llround((F32)(mRect.getHeight() - y) * LLUI::sGLScaleFactor.mV[VY]) : llround((F32)(mRect.getHeight() - y) * gViewerWindow->getDisplayScale().mV[VY]);
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onNavigateBegin( const EventType& eventIn )
{
	LLWebBrowserCtrlEvent event;
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onNavigateBegin, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onNavigateComplete( const EventType& eventIn )
{
	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event;
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onNavigateComplete, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onUpdateProgress( const EventType& eventIn )
{
	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event( eventIn.getIntValue() );
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onUpdateProgress, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onStatusTextChange( const EventType& eventIn )
{
	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event( eventIn.getStringValue() );
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onStatusTextChange, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onLocationChange( const EventType& eventIn )
{
	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event( eventIn.getStringValue() );
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onLocationChange, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onClickLinkHref( const EventType& eventIn )
{
	const std::string protocol( "http://" );

	if( mOpenLinksInExternalBrowser )
	{
		if ( eventIn.getStringValue().length() )
		{
			if ( LLString::compareInsensitive( eventIn.getStringValue().substr( 0, protocol.length() ).c_str(), protocol.c_str() ) == 0 )
			{
				LLWeb::loadURL( eventIn.getStringValue() );
			};
		};
	}
	else
	if( mOpenLinksInInternalBrowser )
	{
		if ( eventIn.getStringValue().length() )
		{
			if ( LLString::compareInsensitive( eventIn.getStringValue().substr( 0, protocol.length() ).c_str(), protocol.c_str() ) == 0 )
			{
				LLFloaterHtml::getInstance()->show( eventIn.getStringValue(), "Second Life Browser");
			};
		};
	};

	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event( eventIn.getStringValue() );
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onClickLinkHref, event );
}

////////////////////////////////////////////////////////////////////////////////
// virtual
void LLWebBrowserCtrl::onClickLinkSecondLife( const EventType& eventIn )
{
	std::string url = eventIn.getStringValue();
	LLURLDispatcher::dispatch(url);

	// chain this event on to observers of an instance of LLWebBrowserCtrl
	LLWebBrowserCtrlEvent event( eventIn.getStringValue() );
	mEventEmitter.update( &LLWebBrowserCtrlObserver::onClickLinkSecondLife, event );
}

////////////////////////////////////////////////////////////////////////////////
//
LLWebBrowserTexture::LLWebBrowserTexture( S32 width, S32 height, LLWebBrowserCtrl* browserCtrl, int browserWindowId ) :
	LLDynamicTexture( 512, 512, 4, ORDER_FIRST, TRUE ),
	mLastBrowserDepth( 0 ),
	mWebBrowserCtrl( browserCtrl ),
	mEmbeddedBrowserWindowId( browserWindowId )
{
	mElapsedTime.start();

	resize( width, height );
}

////////////////////////////////////////////////////////////////////////////////
//
LLWebBrowserTexture::~LLWebBrowserTexture()
{
	mElapsedTime.stop();
}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLWebBrowserTexture::render()
{
	// frequent updates turned on?
	if ( mWebBrowserCtrl->getFrequentUpdates() || mWebBrowserCtrl->getAlwaysRefresh() )
	{
		// only update mozilla/texture occasionally
		if ( mElapsedTime.getElapsedTimeF32() > ( 1.0f / 15.0f ) )
		{
			mElapsedTime.reset();

			const unsigned char* pixels = LLMozLib::getInstance()->grabBrowserWindow( mEmbeddedBrowserWindowId );

			S32 actual_rowspan = LLMozLib::getInstance()->getBrowserRowSpan( mEmbeddedBrowserWindowId );
			S32 browser_depth = LLMozLib::getInstance()->getBrowserDepth( mEmbeddedBrowserWindowId );

			// these are both invalid conditions and should never happen but SL-27583 indicates it does
			if ( actual_rowspan < 1 || browser_depth < 2 )
				return FALSE;

			// width can change after it's rendered - (Mozilla bug# 24721)
			S32 pagebuffer_width = actual_rowspan / browser_depth;

			// Browser depth hasn't changed.  
			if(mLastBrowserDepth == browser_depth)
			{
				// Just grab the pixels.
				mTexture->setSubImage( pixels,
										pagebuffer_width, mBrowserHeight,
											0, 0, pagebuffer_width, mBrowserHeight );
			}
			else
			{
				// Browser depth has changed -- need to recreate texture to match.
				resize(mBrowserWidth, mBrowserHeight);
			};
		};
	};

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//
S32 LLWebBrowserTexture::getBrowserWidth()
{
	return mBrowserWidth;
}

////////////////////////////////////////////////////////////////////////////////
//
S32 LLWebBrowserTexture::getBrowserHeight()
{
	return mBrowserHeight;
}


////////////////////////////////////////////////////////////////////////////////
//
void LLWebBrowserTexture::resize( S32 new_width, S32 new_height )
{
	F32 scale_ratio = 1.f;
	if (new_width > MAX_DIMENSION)
	{
		scale_ratio = (F32)MAX_DIMENSION / (F32)new_width;
	}
	if (new_height > MAX_DIMENSION)
	{
		scale_ratio = llmin(scale_ratio, (F32)MAX_DIMENSION / (F32)new_height);
	}

	mBrowserWidth = llround(scale_ratio * (F32)new_width);
	mBrowserHeight = llround(scale_ratio * (F32)new_height);

	LLMozLib::getInstance()->setSize( mEmbeddedBrowserWindowId, mBrowserWidth, mBrowserHeight );

	const unsigned char* pixels = LLMozLib::getInstance()->grabBrowserWindow( mEmbeddedBrowserWindowId );

	S32 actual_rowspan = LLMozLib::getInstance()->getBrowserRowSpan( mEmbeddedBrowserWindowId );
	S32 browser_depth = LLMozLib::getInstance()->getBrowserDepth( mEmbeddedBrowserWindowId );

	// these are both invalid conditions and should never happen but SL-27583 indicates it does
	if ( actual_rowspan < 1 || browser_depth < 2 )
		return;

	releaseGLTexture();
	
	S32 pagebuffer_width = actual_rowspan / browser_depth;

	// calculate the next power of 2 bigger than reqquested size for width and height
	for ( mWidth = 1; mWidth < pagebuffer_width; mWidth <<= 1 )
	{
		if ( mWidth >= MAX_TEXTURE_DIMENSION )
		{
			break;
		};
	};

	for ( mHeight = 1; mHeight < mBrowserHeight; mHeight <<= 1 )
	{
		if ( mHeight >= MAX_TEXTURE_DIMENSION )
		{
			break;
		};
	};
	
	LLGLint internal_format;
	LLGLenum primary_format;
	LLGLenum type_format;
	BOOL	 swap_bytes = FALSE;

	switch(browser_depth)
	{
		default:
		case 4:
			internal_format = GL_RGBA8;
			primary_format = GL_BGRA_EXT;
		#if LL_DARWIN
			#if LL_BIG_ENDIAN
				type_format = GL_UNSIGNED_INT_8_8_8_8_REV;
			#else
				type_format = GL_UNSIGNED_INT_8_8_8_8;
			#endif
		#else	// windows or linux
			type_format = GL_UNSIGNED_BYTE;
		#endif
		break;
		
		case 2:
		#if LL_DARWIN
			internal_format = GL_RGBA8;
			primary_format = GL_BGRA_EXT;
			type_format = GL_UNSIGNED_SHORT_1_5_5_5_REV;
			#if LL_LITTLE_ENDIAN
				swap_bytes = TRUE;
			#endif
		#else	// windows or linux
			// MBW -- XXX -- This is just a guess on my part.  Someone needs to verify which GL texture format matches the 16-bit format used on windows.
			internal_format = GL_RGB8;
			primary_format = GL_RGB;
			type_format = GL_UNSIGNED_SHORT_5_6_5;
		#endif
		break;
	}
	
	// will create mWidth * mHeight sized texture, using BGR ordering
	LLDynamicTexture::generateGLTexture(internal_format, primary_format, type_format, swap_bytes);
	
	mTexture->setSubImage( pixels,
		pagebuffer_width, mBrowserHeight,
			0, 0, pagebuffer_width, mBrowserHeight );
	
	mLastBrowserDepth = browser_depth;
}

LLView* LLWebBrowserCtrl::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory)
{
	LLString name("web_browser");
	node->getAttributeString("name", name);

	LLString start_url("start_url");
	node->getAttributeString("start_url", start_url );

	BOOL border_visible = true;
	node->getAttributeBOOL("border_visible", border_visible);

	LLRect rect;
	createRect(node, rect, parent, LLRect());

	LLWebBrowserCtrl* web_browser = new LLWebBrowserCtrl( name, rect );

	BOOL ignore_ui_scale = web_browser->getIgnoreUIScale();
	node->getAttributeBOOL("ignore_ui_scale", ignore_ui_scale);
	web_browser->setIgnoreUIScale((bool)ignore_ui_scale);

	web_browser->initFromXML(node, parent);

	web_browser->setHomePageUrl( start_url );

	web_browser->setBorderVisible( border_visible );

	web_browser->navigateHome();

	return web_browser;
}

#endif // LL_LIBXUL_ENABLED