llweb.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file llweb.cpp
  3. * @brief Functions dealing with web browsers
  4. * @author James Cook
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewergpl$
  7. *
  8. * Copyright (c) 2006-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "llviewerprecompiledheaders.h"
  34. #include "llweb.h"
  35. #include "llnotifications.h"
  36. #include "llsys.h" // For LLOSInfo
  37. #include "lluri.h"
  38. #include "llversionviewer.h"
  39. #include "llagent.h"
  40. #include "llfloatermediabrowser.h"
  41. #include "llgridmanager.h"
  42. #include "hbviewerautomation.h"
  43. #include "llviewercontrol.h"
  44. #include "llviewerparcelmgr.h"
  45. #include "llviewerregion.h"
  46. #include "llviewerwindow.h"
  47. //static
  48. void LLWeb::initClass()
  49. {
  50. LLAlertDialog::setURLLoader(&sAlertURLLoader);
  51. }
  52. //static
  53. void LLWeb::loadURL(const std::string& url)
  54. {
  55. loadURL(url, "");
  56. }
  57. //static
  58. void LLWeb::loadURL(const std::string& url, const std::string& target)
  59. {
  60. // 0 = loading forbidden, 1 = use web plugin, 2 = use external browser. HB
  61. U32 dispatch = 1;
  62. if (target != "_internal" &&
  63. (target == "_external" || gSavedSettings.getBool("UseExternalBrowser")))
  64. {
  65. dispatch = 2;
  66. }
  67. if (gAutomationp)
  68. {
  69. dispatch = gAutomationp->onURLDispatch(url, target, dispatch == 2);
  70. if (!dispatch)
  71. {
  72. // Dispatching this URL has been forbidden by the user's Lua
  73. // script. HB
  74. return;
  75. }
  76. }
  77. if (dispatch == 2)
  78. {
  79. loadURLExternal(url);
  80. }
  81. else
  82. {
  83. LLFloaterMediaBrowser::showInstance(url);
  84. }
  85. }
  86. //static
  87. void LLWeb::loadURLInternal(const std::string& url)
  88. {
  89. loadURL(url, "_internal");
  90. }
  91. //static
  92. void LLWeb::loadURLExternal(const std::string& url)
  93. {
  94. loadURLExternal(url, true);
  95. }
  96. bool on_load_url_external_response(const LLSD& notification,
  97. const LLSD& response, bool async)
  98. {
  99. if (LLNotification::getSelectedOption(notification, response) == 0)
  100. {
  101. LLSD payload = notification["payload"];
  102. std::string url = payload["url"].asString();
  103. std::string escaped_url = LLWeb::escapeURL(url);
  104. if (gWindowp)
  105. {
  106. gWindowp->spawnWebBrowser(escaped_url, async);
  107. }
  108. }
  109. return false;
  110. }
  111. //static
  112. void LLWeb::loadURLExternal(const std::string& url, bool async)
  113. {
  114. LLSD payload;
  115. payload["url"] = url;
  116. gNotifications.add("WebLaunchExternalTarget", LLSD(), payload,
  117. boost::bind(on_load_url_external_response, _1, _2,
  118. async));
  119. }
  120. //static
  121. std::string LLWeb::escapeURL(const std::string& url)
  122. {
  123. // The CURL curl_escape() function escapes colons, slashes and all
  124. // characters but A-Z and 0-9. Do a cheesy mini-escape.
  125. std::string escaped_url;
  126. S32 len = url.length();
  127. for (S32 i = 0; i < len; ++i)
  128. {
  129. char c = url[i];
  130. if (c == ' ')
  131. {
  132. escaped_url += "%20";
  133. }
  134. else if (c == '\\')
  135. {
  136. escaped_url += "%5C";
  137. }
  138. else
  139. {
  140. escaped_url += c;
  141. }
  142. }
  143. return escaped_url;
  144. }
  145. //static
  146. std::string LLWeb::expandURLSubstitutions(const std::string& url,
  147. const LLStringUtil::format_map_t& default_subs)
  148. {
  149. LLStringUtil::format_map_t substitution = default_subs;
  150. substitution["[VERSION]"] = llformat("%d.%d.%d.%d", LL_VERSION_MAJOR,
  151. LL_VERSION_MINOR, LL_VERSION_BRANCH,
  152. LL_VERSION_RELEASE);
  153. substitution["[VERSION_MAJOR]"] = llformat("%d", LL_VERSION_MAJOR);
  154. substitution["[VERSION_MINOR]"] = llformat("%d", LL_VERSION_MINOR);
  155. substitution["[VERSION_PATCH]"] = llformat("%d", LL_VERSION_BRANCH);
  156. substitution["[VERSION_BUILD]"] = llformat("%d", LL_VERSION_RELEASE);
  157. substitution["[CHANNEL]"] = gSavedSettings.getString("VersionChannelName");
  158. substitution["[GRID]"] = LLGridManager::getInstance()->getGridLabel();
  159. substitution["[OS]"] = LLOSInfo::getInstance()->getOSStringSimple();
  160. substitution["[SESSION_ID]"] = gAgentSessionID.asString();
  161. substitution["[FIRST_LOGIN]"] = llformat("%d", gAgent.isFirstLogin());
  162. // Work out the current language
  163. std::string lang = LLUI::getLanguage();
  164. if (lang == "en-us")
  165. {
  166. lang = "en";
  167. }
  168. substitution["[LANGUAGE]"] = lang;
  169. // Find the region ID and name
  170. LLUUID region_id;
  171. std::string region_name;
  172. LLViewerRegion* region = gAgent.getRegion();
  173. if (region)
  174. {
  175. region_id = region->getRegionID();
  176. region_name = LLURI::escape(region->getName());
  177. }
  178. substitution["[REGION_ID]"] = region_id.asString();
  179. substitution["[REGION]"] = region_name;
  180. // Find the parcel local ID
  181. S32 parcel_id = 0;
  182. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  183. if (parcel)
  184. {
  185. parcel_id = parcel->getLocalID();
  186. }
  187. substitution["[PARCEL_ID]"] = llformat("%d", parcel_id);
  188. if (!gIsInSecondLife)
  189. {
  190. substitution["SLURL_TYPE"] = "hop";
  191. }
  192. // Expand all of the substitution strings and escape the url
  193. std::string expanded_url = url;
  194. LLStringUtil::format(expanded_url, substitution);
  195. return escapeURL(expanded_url);
  196. }
  197. //virtual
  198. void LLWeb::URLLoader::load(const std::string& url)
  199. {
  200. loadURL(url);
  201. }
  202. //static
  203. LLWeb::URLLoader LLWeb::sAlertURLLoader;