PageRenderTime 30ms CodeModel.GetById 18ms 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
Possible License(s): LGPL-2.1
  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. #include "llviewerprecompiledheaders.h"
  28. #include "llweb.h"
  29. // Library includes
  30. #include "llwindow.h" // spawnWebBrowser()
  31. #include "llagent.h"
  32. #include "llappviewer.h"
  33. #include "llfloaterwebcontent.h"
  34. #include "llfloaterreg.h"
  35. #include "lllogininstance.h"
  36. #include "llparcel.h"
  37. #include "llsd.h"
  38. #include "lltoastalertpanel.h"
  39. #include "llui.h"
  40. #include "lluri.h"
  41. #include "llversioninfo.h"
  42. #include "llviewercontrol.h"
  43. #include "llviewernetwork.h"
  44. #include "llviewerparcelmgr.h"
  45. #include "llviewerregion.h"
  46. #include "llviewerwindow.h"
  47. #include "llnotificationsutil.h"
  48. bool on_load_url_external_response(const LLSD& notification, const LLSD& response, bool async );
  49. class URLLoader : public LLToastAlertPanel::URLLoader
  50. {
  51. virtual void load(const std::string& url , bool force_open_externally)
  52. {
  53. if (force_open_externally)
  54. {
  55. LLWeb::loadURLExternal(url);
  56. }
  57. else
  58. {
  59. LLWeb::loadURL(url);
  60. }
  61. }
  62. };
  63. static URLLoader sAlertURLLoader;
  64. // static
  65. void LLWeb::initClass()
  66. {
  67. LLToastAlertPanel::setURLLoader(&sAlertURLLoader);
  68. }
  69. // static
  70. void LLWeb::loadURL(const std::string& url, const std::string& target, const std::string& uuid)
  71. {
  72. if(target == "_internal")
  73. {
  74. // Force load in the internal browser, as if with a blank target.
  75. loadURLInternal(url, "", uuid);
  76. }
  77. else if (gSavedSettings.getBOOL("UseExternalBrowser") || (target == "_external"))
  78. {
  79. loadURLExternal(url);
  80. }
  81. else
  82. {
  83. loadURLInternal(url, target, uuid);
  84. }
  85. }
  86. // static
  87. // Explicitly open a Web URL using the Web content floater
  88. void LLWeb::loadURLInternal(const std::string &url, const std::string& target, const std::string& uuid)
  89. {
  90. LLFloaterWebContent::Params p;
  91. p.url(url).target(target).id(uuid);
  92. LLFloaterReg::showInstance("web_content", p);
  93. }
  94. // static
  95. void LLWeb::loadURLExternal(const std::string& url, const std::string& uuid)
  96. {
  97. loadURLExternal(url, true, uuid);
  98. }
  99. // static
  100. void LLWeb::loadURLExternal(const std::string& url, bool async, const std::string& uuid)
  101. {
  102. // Act like the proxy window was closed, since we won't be able to track targeted windows in the external browser.
  103. LLViewerMedia::proxyWindowClosed(uuid);
  104. if(gSavedSettings.getBOOL("DisableExternalBrowser"))
  105. {
  106. // Don't open an external browser under any circumstances.
  107. llwarns << "Blocked attempt to open external browser." << llendl;
  108. return;
  109. }
  110. LLSD payload;
  111. payload["url"] = url;
  112. LLNotificationsUtil::add( "WebLaunchExternalTarget", LLSD(), payload, boost::bind(on_load_url_external_response, _1, _2, async));
  113. }
  114. // static
  115. bool on_load_url_external_response(const LLSD& notification, const LLSD& response, bool async )
  116. {
  117. S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
  118. if ( 0 == option )
  119. {
  120. LLSD payload = notification["payload"];
  121. std::string url = payload["url"].asString();
  122. std::string escaped_url = LLWeb::escapeURL(url);
  123. if (gViewerWindow)
  124. {
  125. gViewerWindow->getWindow()->spawnWebBrowser(escaped_url, async);
  126. }
  127. }
  128. return false;
  129. }
  130. // static
  131. std::string LLWeb::escapeURL(const std::string& url)
  132. {
  133. // The CURL curl_escape() function escapes colons, slashes,
  134. // and all characters but A-Z and 0-9. Do a cheesy mini-escape.
  135. std::string escaped_url;
  136. S32 len = url.length();
  137. for (S32 i = 0; i < len; i++)
  138. {
  139. char c = url[i];
  140. if (c == ' ')
  141. {
  142. escaped_url += "%20";
  143. }
  144. else if (c == '\\')
  145. {
  146. escaped_url += "%5C";
  147. }
  148. else
  149. {
  150. escaped_url += c;
  151. }
  152. }
  153. return escaped_url;
  154. }
  155. //static
  156. std::string LLWeb::expandURLSubstitutions(const std::string &url,
  157. const LLSD &default_subs)
  158. {
  159. LLSD substitution = default_subs;
  160. substitution["VERSION"] = LLVersionInfo::getVersion();
  161. substitution["VERSION_MAJOR"] = LLVersionInfo::getMajor();
  162. substitution["VERSION_MINOR"] = LLVersionInfo::getMinor();
  163. substitution["VERSION_PATCH"] = LLVersionInfo::getPatch();
  164. substitution["VERSION_BUILD"] = LLVersionInfo::getBuild();
  165. substitution["CHANNEL"] = LLVersionInfo::getChannel();
  166. substitution["GRID"] = LLGridManager::getInstance()->getGridLabel();
  167. substitution["GRID_LOWERCASE"] = utf8str_tolower(LLGridManager::getInstance()->getGridLabel());
  168. substitution["OS"] = LLAppViewer::instance()->getOSInfo().getOSStringSimple();
  169. substitution["SESSION_ID"] = gAgent.getSessionID();
  170. substitution["FIRST_LOGIN"] = gAgent.isFirstLogin();
  171. // work out the current language
  172. std::string lang = LLUI::getLanguage();
  173. if (lang == "en-us")
  174. {
  175. // *HACK: the correct fix is to change English.lproj/language.txt,
  176. // but we're late in the release cycle and this is a less risky fix
  177. lang = "en";
  178. }
  179. substitution["LANGUAGE"] = lang;
  180. // find the region ID
  181. LLUUID region_id;
  182. LLViewerRegion *region = gAgent.getRegion();
  183. if (region)
  184. {
  185. region_id = region->getRegionID();
  186. }
  187. substitution["REGION_ID"] = region_id;
  188. // find the parcel local ID
  189. S32 parcel_id = 0;
  190. LLParcel* parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
  191. if (parcel)
  192. {
  193. parcel_id = parcel->getLocalID();
  194. }
  195. substitution["PARCEL_ID"] = llformat("%d", parcel_id);
  196. // expand all of the substitution strings and escape the url
  197. std::string expanded_url = url;
  198. LLStringUtil::format(expanded_url, substitution);
  199. return LLWeb::escapeURL(expanded_url);
  200. }