PageRenderTime 25ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/newview/llweb.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 227 lines | 153 code | 27 blank | 47 comment | 21 complexity | 2d1c9a56e92dc57f2aa1f6868ab4d109 MD5 | raw file
  1/** 
  2 * @file llweb.cpp
  3 * @brief Functions dealing with web browsers
  4 * @author James Cook
  5 *
  6 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "llviewerprecompiledheaders.h"
 29
 30#include "llweb.h"
 31
 32// Library includes
 33#include "llwindow.h"	// spawnWebBrowser()
 34
 35#include "llagent.h"
 36#include "llappviewer.h"
 37#include "llfloaterwebcontent.h"
 38#include "llfloaterreg.h"
 39#include "lllogininstance.h"
 40#include "llparcel.h"
 41#include "llsd.h"
 42#include "lltoastalertpanel.h"
 43#include "llui.h"
 44#include "lluri.h"
 45#include "llversioninfo.h"
 46#include "llviewercontrol.h"
 47#include "llviewernetwork.h"
 48#include "llviewerparcelmgr.h"
 49#include "llviewerregion.h"
 50#include "llviewerwindow.h"
 51#include "llnotificationsutil.h"
 52
 53bool on_load_url_external_response(const LLSD& notification, const LLSD& response, bool async );
 54
 55
 56class URLLoader : public LLToastAlertPanel::URLLoader
 57{
 58	virtual void load(const std::string& url , bool force_open_externally)
 59	{
 60		if (force_open_externally)
 61		{
 62			LLWeb::loadURLExternal(url);
 63		}
 64		else
 65		{
 66			LLWeb::loadURL(url);
 67		}
 68	}
 69};
 70static URLLoader sAlertURLLoader;
 71
 72
 73// static
 74void LLWeb::initClass()
 75{
 76	LLToastAlertPanel::setURLLoader(&sAlertURLLoader);
 77}
 78
 79
 80
 81
 82// static
 83void LLWeb::loadURL(const std::string& url, const std::string& target, const std::string& uuid)
 84{
 85	if(target == "_internal")
 86	{
 87		// Force load in the internal browser, as if with a blank target.
 88		loadURLInternal(url, "", uuid);
 89	}
 90	else if (gSavedSettings.getBOOL("UseExternalBrowser") || (target == "_external"))
 91	{
 92		loadURLExternal(url);
 93	}
 94	else
 95	{
 96		loadURLInternal(url, target, uuid);
 97	}
 98}
 99
100// static
101// Explicitly open a Web URL using the Web content floater
102void LLWeb::loadURLInternal(const std::string &url, const std::string& target, const std::string& uuid)
103{
104	LLFloaterWebContent::Params p;
105	p.url(url).target(target).id(uuid);
106	LLFloaterReg::showInstance("web_content", p);
107}
108
109// static
110void LLWeb::loadURLExternal(const std::string& url, const std::string& uuid)
111{
112	loadURLExternal(url, true, uuid);
113}
114
115// static
116void LLWeb::loadURLExternal(const std::string& url, bool async, const std::string& uuid)
117{
118	// Act like the proxy window was closed, since we won't be able to track targeted windows in the external browser.
119	LLViewerMedia::proxyWindowClosed(uuid);
120	
121	if(gSavedSettings.getBOOL("DisableExternalBrowser"))
122	{
123		// Don't open an external browser under any circumstances.
124		llwarns << "Blocked attempt to open external browser." << llendl;
125		return;
126	}
127	
128	LLSD payload;
129	payload["url"] = url;
130	LLNotificationsUtil::add( "WebLaunchExternalTarget", LLSD(), payload, boost::bind(on_load_url_external_response, _1, _2, async));
131}
132
133// static 
134bool on_load_url_external_response(const LLSD& notification, const LLSD& response, bool async )
135{
136	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
137	if ( 0 == option )
138	{
139		LLSD payload = notification["payload"];
140		std::string url = payload["url"].asString();
141		std::string escaped_url = LLWeb::escapeURL(url);
142		if (gViewerWindow)
143		{
144			gViewerWindow->getWindow()->spawnWebBrowser(escaped_url, async);
145		}
146	}
147	return false;
148}
149
150
151// static
152std::string LLWeb::escapeURL(const std::string& url)
153{
154	// The CURL curl_escape() function escapes colons, slashes,
155	// and all characters but A-Z and 0-9.  Do a cheesy mini-escape.
156	std::string escaped_url;
157	S32 len = url.length();
158	for (S32 i = 0; i < len; i++)
159	{
160		char c = url[i];
161		if (c == ' ')
162		{
163			escaped_url += "%20";
164		}
165		else if (c == '\\')
166		{
167			escaped_url += "%5C";
168		}
169		else
170		{
171			escaped_url += c;
172		}
173	}
174	return escaped_url;
175}
176
177//static
178std::string LLWeb::expandURLSubstitutions(const std::string &url,
179										  const LLSD &default_subs)
180{
181	LLSD substitution = default_subs;
182	substitution["VERSION"] = LLVersionInfo::getVersion();
183	substitution["VERSION_MAJOR"] = LLVersionInfo::getMajor();
184	substitution["VERSION_MINOR"] = LLVersionInfo::getMinor();
185	substitution["VERSION_PATCH"] = LLVersionInfo::getPatch();
186	substitution["VERSION_BUILD"] = LLVersionInfo::getBuild();
187	substitution["CHANNEL"] = LLVersionInfo::getChannel();
188	substitution["GRID"] = LLGridManager::getInstance()->getGridLabel();
189	substitution["GRID_LOWERCASE"] = utf8str_tolower(LLGridManager::getInstance()->getGridLabel());
190	substitution["OS"] = LLAppViewer::instance()->getOSInfo().getOSStringSimple();
191	substitution["SESSION_ID"] = gAgent.getSessionID();
192	substitution["FIRST_LOGIN"] = gAgent.isFirstLogin();
193
194	// work out the current language
195	std::string lang = LLUI::getLanguage();
196	if (lang == "en-us")
197	{
198		// *HACK: the correct fix is to change English.lproj/language.txt,
199		// but we're late in the release cycle and this is a less risky fix
200		lang = "en";
201	}
202	substitution["LANGUAGE"] = lang;
203
204	// find the region ID
205	LLUUID region_id;
206	LLViewerRegion *region = gAgent.getRegion();
207	if (region)
208	{
209		region_id = region->getRegionID();
210	}
211	substitution["REGION_ID"] = region_id;
212
213	// find the parcel local ID
214	S32 parcel_id = 0;
215	LLParcel* parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
216	if (parcel)
217	{
218		parcel_id = parcel->getLocalID();
219	}
220	substitution["PARCEL_ID"] = llformat("%d", parcel_id);
221
222	// expand all of the substitution strings and escape the url
223	std::string expanded_url = url;
224	LLStringUtil::format(expanded_url, substitution);
225
226	return LLWeb::escapeURL(expanded_url);
227}