summaryrefslogtreecommitdiff
path: root/indra/newview/llfloaterwebcontent.cpp
blob: 138ddeabdac0fa1fac4dc331fd961d20694830ea (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
/** 
 * @file llfloaterwebcontent.cpp
 * @brief floater for displaying web content - e.g. profiles and search (eventually)
 *
 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llfloaterwebcontent.h"

#include "llfloaterreg.h"
#include "llparcel.h"
#include "llpluginclassmedia.h"
#include "lluictrlfactory.h"
#include "llmediactrl.h"
#include "llviewerwindow.h"
#include "llviewercontrol.h"
#include "llviewerparcelmgr.h"
#include "llweb.h"
#include "llui.h"
#include "roles_constants.h"

#include "llurlhistory.h"
#include "llviewermedia.h"
#include "llviewerparcelmedia.h"
#include "llcombobox.h"
#include "llwindow.h"
#include "lllayoutstack.h"
#include "llcheckboxctrl.h"

#include "llnotifications.h"

// TEMP
#include "llsdutil.h"

LLFloaterWebContent::LLFloaterWebContent(const LLSD& key)
	: LLFloater(key)
{
}

//static 
void LLFloaterWebContent::create(const std::string &url, const std::string& target, const std::string& uuid)
{
	lldebugs << "url = " << url << ", target = " << target << ", uuid = " << uuid << llendl;
	
	std::string tag = target;
	
	if(target.empty() || target == "_blank")
	{
		if(!uuid.empty())
		{
			tag = uuid;
		}
		else
		{
			// create a unique tag for this instance
			LLUUID id;
			id.generate();
			tag = id.asString();
		}
	}
	
	S32 browser_window_limit = gSavedSettings.getS32("MediaBrowserWindowLimit");
	
	if(LLFloaterReg::findInstance("web_content", tag) != NULL)
	{
		// There's already a web browser for this tag, so we won't be opening a new window.
	}
	else if(browser_window_limit != 0)
	{
		// showInstance will open a new window.  Figure out how many web browsers are already open, 
		// and close the least recently opened one if this will put us over the limit.
		
		LLFloaterReg::const_instance_list_t &instances = LLFloaterReg::getFloaterList("web_content");
		lldebugs << "total instance count is " << instances.size() << llendl;
		
		for(LLFloaterReg::const_instance_list_t::const_iterator iter = instances.begin(); iter != instances.end(); iter++)
		{
			lldebugs << "    " << (*iter)->getKey() << llendl;
		}
		
		if(instances.size() >= (size_t)browser_window_limit)
		{
			// Destroy the least recently opened instance
			(*instances.begin())->closeFloater();
		}
	}

	LLFloaterWebContent *browser = dynamic_cast<LLFloaterWebContent*> (LLFloaterReg::showInstance("web_content", tag));
	llassert(browser);
	if(browser)
	{
		browser->mUUID = uuid;

		// tell the browser instance to load the specified URL
		browser->openMedia(url, target);
		LLViewerMedia::proxyWindowOpened(target, uuid);
	}
}

//static 
void LLFloaterWebContent::closeRequest(const std::string &uuid)
{
	LLFloaterReg::const_instance_list_t& inst_list = LLFloaterReg::getFloaterList("web_content");
	lldebugs << "instance list size is " << inst_list.size() << ", incoming uuid is " << uuid << llendl;
	for (LLFloaterReg::const_instance_list_t::const_iterator iter = inst_list.begin(); iter != inst_list.end(); ++iter)
	{
		LLFloaterWebContent* i = dynamic_cast<LLFloaterWebContent*>(*iter);
		lldebugs << "    " << i->mUUID << llendl;
		if (i && i->mUUID == uuid)
		{
			i->closeFloater(false);
			return;
 		}
 	}
}

//static 
void LLFloaterWebContent::geometryChanged(const std::string &uuid, S32 x, S32 y, S32 width, S32 height)
{
	LLFloaterReg::const_instance_list_t& inst_list = LLFloaterReg::getFloaterList("web_content");
	lldebugs << "instance list size is " << inst_list.size() << ", incoming uuid is " << uuid << llendl;
	for (LLFloaterReg::const_instance_list_t::const_iterator iter = inst_list.begin(); iter != inst_list.end(); ++iter)
	{
		LLFloaterWebContent* i = dynamic_cast<LLFloaterWebContent*>(*iter);
		lldebugs << "    " << i->mUUID << llendl;
		if (i && i->mUUID == uuid)
		{
			i->geometryChanged(x, y, width, height);
			return;
		}
	}
}
	
void LLFloaterWebContent::geometryChanged(S32 x, S32 y, S32 width, S32 height)
{	
	// Make sure the layout of the browser control is updated, so this calculation is correct.
	LLLayoutStack::updateClass();
		
	// TODO: need to adjust size and constrain position to make sure floaters aren't moved outside the window view, etc.
	LLCoordWindow window_size;
	getWindow()->getSize(&window_size);

	// Adjust width and height for the size of the chrome on the web Browser window.
	width += getRect().getWidth() - mWebBrowser->getRect().getWidth();
	height += getRect().getHeight() - mWebBrowser->getRect().getHeight();
	
	LLRect geom;
	geom.setOriginAndSize(x, window_size.mY - (y + height), width, height);

	lldebugs << "geometry change: " << geom << llendl;
	
	handleReshape(geom,false);
}

void LLFloaterWebContent::openMedia(const std::string& web_url, const std::string& target)
{
	mWebBrowser->setHomePageUrl(web_url);
	mWebBrowser->setTarget(target);
	mWebBrowser->navigateTo(web_url);
	setCurrentURL(web_url);
}

void LLFloaterWebContent::draw()
{
//	getChildView("go")->setEnabled(!mAddressCombo->getValue().asString().empty());

	getChildView("back")->setEnabled(mWebBrowser->canNavigateBack());
	getChildView("forward")->setEnabled(mWebBrowser->canNavigateForward());

	LLFloater::draw();
}

BOOL LLFloaterWebContent::postBuild()
{
	mWebBrowser = getChild<LLMediaCtrl>("webbrowser");
	mWebBrowser->addObserver(this);

	childSetAction("back", onClickBack, this);
	childSetAction("forward", onClickForward, this);
	childSetAction("reload", onClickRefresh, this);
	childSetAction("go", onClickGo, this);

	return TRUE;
}

//virtual
void LLFloaterWebContent::onClose(bool app_quitting)
{
	LLViewerMedia::proxyWindowClosed(mUUID);
	destroy();
}

void LLFloaterWebContent::handleMediaEvent(LLPluginClassMedia* self, EMediaEvent event)
{
	if(event == MEDIA_EVENT_LOCATION_CHANGED)
	{
		setCurrentURL(self->getLocation());
	}
	else if(event == MEDIA_EVENT_NAVIGATE_COMPLETE)
	{
		// This is the event these flags are sent with.
		getChildView("back")->setEnabled(self->getHistoryBackAvailable());
		getChildView("forward")->setEnabled(self->getHistoryForwardAvailable());
	}
	else if(event == MEDIA_EVENT_CLOSE_REQUEST)
	{
		// The browser instance wants its window closed.
		closeFloater();
	}
	else if(event == MEDIA_EVENT_GEOMETRY_CHANGE)
	{
		geometryChanged(self->getGeometryX(), self->getGeometryY(), self->getGeometryWidth(), self->getGeometryHeight());
	}
}

void LLFloaterWebContent::setCurrentURL(const std::string& url)
{
	mCurrentURL = url;

	getChildView("back")->setEnabled(mWebBrowser->canNavigateBack());
	getChildView("forward")->setEnabled(mWebBrowser->canNavigateForward());
	getChildView("reload")->setEnabled(TRUE);
}

//static 
void LLFloaterWebContent::onClickRefresh(void* user_data)
{
	LLFloaterWebContent* self = (LLFloaterWebContent*)user_data;

	self->mWebBrowser->navigateTo(self->mCurrentURL);
}

//static 
void LLFloaterWebContent::onClickForward(void* user_data)
{
	LLFloaterWebContent* self = (LLFloaterWebContent*)user_data;

	self->mWebBrowser->navigateForward();
}

//static 
void LLFloaterWebContent::onClickBack(void* user_data)
{
	LLFloaterWebContent* self = (LLFloaterWebContent*)user_data;

	self->mWebBrowser->navigateBack();
}

//static 
void LLFloaterWebContent::onClickGo(void* user_data)
{
//	LLFloaterWebContent* self = (LLFloaterWebContent*)user_data;

//	self->mWebBrowser->navigateTo(self->mAddressCombo->getValue().asString());
}