llfloatermediabrowser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @file llfloatermediabrowser.cpp
  3. * @brief Web browser floaters
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llfloatermediabrowser.h"
  34. #include "llcombobox.h"
  35. #include "llhttpconstants.h" // For HTTP_CONTENT_TEXT_HTML
  36. #include "llpluginclassmedia.h"
  37. #include "llsdutil.h"
  38. #include "lltextbox.h"
  39. #include "lluictrlfactory.h"
  40. #include "llurlhistory.h"
  41. #include "llwindow.h"
  42. #include "llcommandhandler.h"
  43. #include "llgridmanager.h"
  44. #include "llviewercontrol.h"
  45. #include "llviewerparcelmedia.h"
  46. #include "llviewerparcelmgr.h"
  47. #include "llweb.h"
  48. #include "roles_constants.h"
  49. // Global
  50. LLViewerHtmlHelp gViewerHtmlHelp;
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Command handler for secondlife:///app/help/{TOPIC} SLapps SLURL support
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // Note: TOPIC is ignored (it is pretty dumb anyway: only pre and post login
  55. // topics are used in LL's v3 viewer). HB
  56. class LLHelpHandler final : public LLCommandHandler
  57. {
  58. public:
  59. // Requests will be throttled from a non-trusted browser
  60. LLHelpHandler()
  61. : LLCommandHandler("help", UNTRUSTED_THROTTLE)
  62. {
  63. }
  64. bool handle(const LLSD&, const LLSD&, LLMediaCtrl*) override
  65. {
  66. gViewerHtmlHelp.show();
  67. return true;
  68. }
  69. };
  70. LLHelpHandler gHelpHandler;
  71. ////////////////////////////////////////////////////////////////////////////////
  72. // LLFloaterMediaBrowser class
  73. ////////////////////////////////////////////////////////////////////////////////
  74. //static
  75. LLFloaterMediaBrowser::instances_vec_t LLFloaterMediaBrowser::sInstances;
  76. //static
  77. LLFloaterMediaBrowser* LLFloaterMediaBrowser::getInstance(const LLSD& media_url)
  78. {
  79. std::string url = media_url.asString();
  80. // Try and find a corresponding open instance
  81. U32 count = sInstances.size();
  82. for (U32 i = 0; i < count; ++i)
  83. {
  84. LLFloaterMediaBrowser* floaterp = sInstances[i];
  85. if (floaterp->mInitalUrl == url || floaterp->mCurrentURL == url)
  86. {
  87. return floaterp;
  88. }
  89. }
  90. U32 max_count = gSavedSettings.getU32("MaxBrowserInstances");
  91. if (max_count < 1)
  92. {
  93. max_count = 1;
  94. gSavedSettings.setU32("MaxBrowserInstances", 1);
  95. }
  96. if (count >= max_count)
  97. {
  98. llinfos << "Maximum Web floaters instances reached, reusing the last one."
  99. << llendl;
  100. // Pick the last instance.
  101. return sInstances.back();
  102. }
  103. return new LLFloaterMediaBrowser(media_url);
  104. }
  105. //static
  106. LLFloaterMediaBrowser* LLFloaterMediaBrowser::showInstance(const LLSD& media_url,
  107. bool trusted)
  108. {
  109. LLFloaterMediaBrowser* floaterp =
  110. LLFloaterMediaBrowser::getInstance(media_url);
  111. if (floaterp) // Paranoia
  112. {
  113. floaterp->openMedia(media_url.asString(), trusted);
  114. gFloaterViewp->bringToFront(floaterp);
  115. }
  116. return floaterp;
  117. }
  118. LLFloaterMediaBrowser::LLFloaterMediaBrowser(const LLSD& media_url)
  119. : mInitalUrl(media_url.asString()),
  120. mBrowser(NULL),
  121. mParcel(NULL),
  122. mBackButton(NULL),
  123. mForwardButton(NULL),
  124. mReloadButton(NULL),
  125. mRewindButton(NULL),
  126. mPlayButton(NULL),
  127. mPauseButton(NULL),
  128. mStopButton(NULL),
  129. mSeekButton(NULL),
  130. mGoButton(NULL),
  131. mCloseButton(NULL),
  132. mBrowserButton(NULL),
  133. mAssignButton(NULL),
  134. mAddressCombo(NULL),
  135. mLoadingText(NULL)
  136. {
  137. sInstances.push_back(this);
  138. LLUICtrlFactory::getInstance()->buildFloater(this,
  139. "floater_media_browser.xml");
  140. }
  141. //virtual
  142. LLFloaterMediaBrowser::~LLFloaterMediaBrowser()
  143. {
  144. for (instances_vec_t::iterator it = sInstances.begin(),
  145. end = sInstances.end();
  146. it != end; ++it)
  147. {
  148. if (*it == this)
  149. {
  150. sInstances.erase(it);
  151. break;
  152. }
  153. }
  154. }
  155. bool LLFloaterMediaBrowser::postBuild()
  156. {
  157. // Note: we use the "build dummy widget if missing" version of getChild<T>
  158. // so that all pointers are non-NULL and warnings are issued in the log
  159. // about missing UI elements. All the UI elements are considered mandatory.
  160. mBrowser = getChild<LLMediaCtrl>("browser");
  161. mBrowser->addObserver(this);
  162. mAddressCombo = getChild<LLComboBox>("address");
  163. mAddressCombo->setCommitCallback(onEnterAddress);
  164. mAddressCombo->setCallbackUserData(this);
  165. mBackButton = getChild<LLButton>("back");
  166. mBackButton->setClickedCallback(onClickBack, this);
  167. mForwardButton = getChild<LLButton>("forward");
  168. mForwardButton->setClickedCallback(onClickForward, this);
  169. mReloadButton = getChild<LLButton>("reload");
  170. mReloadButton->setClickedCallback(onClickRefresh, this);
  171. mRewindButton = getChild<LLButton>("rewind");
  172. mRewindButton->setClickedCallback(onClickRewind, this);
  173. mPlayButton = getChild<LLButton>("play");
  174. mPlayButton->setClickedCallback(onClickPlay, this);
  175. mPauseButton = getChild<LLButton>("pause");
  176. mPauseButton->setClickedCallback(onClickPlay, this);
  177. mStopButton = getChild<LLButton>("stop");
  178. mStopButton->setClickedCallback(onClickStop, this);
  179. mSeekButton = getChild<LLButton>("seek");
  180. mSeekButton->setClickedCallback(onClickSeek, this);
  181. mGoButton = getChild<LLButton>("go");
  182. mGoButton->setClickedCallback(onClickGo, this);
  183. mCloseButton = getChild<LLButton>("close");
  184. mCloseButton->setClickedCallback(onClickClose, this);
  185. mBrowserButton = getChild<LLButton>("open_browser");
  186. mBrowserButton->setClickedCallback(onClickOpenWebBrowser, this);
  187. mAssignButton = getChild<LLButton>("assign");
  188. mAssignButton->setClickedCallback(onClickAssign, this);
  189. mLoadingText = getChild<LLTextBox>("loading");
  190. buildURLHistory();
  191. return true;
  192. }
  193. void LLFloaterMediaBrowser::geometryChanged(S32 x, S32 y, S32 width,
  194. S32 height)
  195. {
  196. // Make sure the layout of the browser control is updated, so this
  197. // calculation is correct.
  198. LLLayoutStack::updateClass();
  199. LLCoordWindow window_size;
  200. gWindowp->getSize(&window_size);
  201. // Adjust width and height for the size of the chrome on the Media Browser
  202. // window.
  203. width += getRect().getWidth() - mBrowser->getRect().getWidth();
  204. height += getRect().getHeight() - mBrowser->getRect().getHeight();
  205. LLRect geom;
  206. geom.setOriginAndSize(x, window_size.mY - (y + height), width, height);
  207. LL_DEBUGS("MediaBrowser") << "geometry change: " << geom << LL_ENDL;
  208. userSetShape(geom);
  209. }
  210. void LLFloaterMediaBrowser::draw()
  211. {
  212. if (!mBrowser)
  213. {
  214. // There is something *very* wrong: abort
  215. llwarns_once << "Incomplete floater media browser !" << llendl;
  216. LLFloater::draw();
  217. return;
  218. }
  219. mBackButton->setEnabled(mBrowser->canNavigateBack());
  220. mForwardButton->setEnabled(mBrowser->canNavigateForward());
  221. mGoButton->setEnabled(!mAddressCombo->getValue().asString().empty() &&
  222. // Forbid changing a trusted browser URL
  223. !mBrowser->isTrusted());
  224. LLParcel* parcelp = gViewerParcelMgr.getAgentParcel();
  225. if (mParcel != parcelp)
  226. {
  227. mParcel = parcelp;
  228. bool can_change =
  229. LLViewerParcelMgr::isParcelModifiableByAgent(parcelp,
  230. GP_LAND_CHANGE_MEDIA);
  231. mAssignButton->setVisible(can_change);
  232. bool not_empty = !mAddressCombo->getValue().asString().empty();
  233. mAssignButton->setEnabled(not_empty);
  234. }
  235. bool show_time_controls = false;
  236. bool media_playing = false;
  237. LLPluginClassMedia* pluginp = mBrowser->getMediaPlugin();
  238. if (pluginp)
  239. {
  240. show_time_controls = pluginp->pluginSupportsMediaTime();
  241. media_playing =
  242. pluginp->getStatus() == LLPluginClassMediaOwner::MEDIA_PLAYING;
  243. }
  244. mRewindButton->setVisible(show_time_controls);
  245. mPlayButton->setVisible(show_time_controls && !media_playing);
  246. mPlayButton->setEnabled(!media_playing);
  247. mPauseButton->setVisible(show_time_controls && media_playing);
  248. mStopButton->setVisible(show_time_controls);
  249. mStopButton->setEnabled(media_playing);
  250. mSeekButton->setVisible(show_time_controls);
  251. LLFloater::draw();
  252. }
  253. void LLFloaterMediaBrowser::buildURLHistory()
  254. {
  255. mAddressCombo->operateOnAll(LLComboBox::OP_DELETE);
  256. // Get all of the entries in the "browser" collection
  257. LLSD browser_history = LLURLHistory::getURLHistory("browser");
  258. std::string url;
  259. for(LLSD::array_iterator iter_history = browser_history.beginArray(),
  260. end_history = browser_history.endArray();
  261. iter_history != end_history; ++iter_history)
  262. {
  263. url = iter_history->asString();
  264. if (!url.empty())
  265. {
  266. mAddressCombo->addSimpleElement(url);
  267. }
  268. }
  269. // Initialize URL history in the plugin
  270. LLPluginClassMedia* pluginp = mBrowser->getMediaPlugin();
  271. if (pluginp)
  272. {
  273. pluginp->initializeUrlHistory(browser_history);
  274. }
  275. }
  276. void LLFloaterMediaBrowser::onClose(bool app_quitting)
  277. {
  278. if (mBrowser)
  279. {
  280. mBrowser->remObserver(this);
  281. if (mBrowser->getMediaSource())
  282. {
  283. mBrowser->getMediaSource()->cancelMimeTypeProbe();
  284. }
  285. }
  286. destroy();
  287. }
  288. void LLFloaterMediaBrowser::handleMediaEvent(LLPluginClassMedia* self,
  289. EMediaEvent event)
  290. {
  291. if (event == MEDIA_EVENT_LOCATION_CHANGED)
  292. {
  293. setCurrentURL(self->getLocation());
  294. mAddressCombo->setVisible(false);
  295. mLoadingText->setVisible(true);
  296. }
  297. else if (event == MEDIA_EVENT_NAVIGATE_COMPLETE)
  298. {
  299. // This is the event these flags are sent with.
  300. mBackButton->setEnabled(self->getHistoryBackAvailable());
  301. mForwardButton->setEnabled(self->getHistoryForwardAvailable());
  302. mAddressCombo->setVisible(true);
  303. mLoadingText->setVisible(false);
  304. }
  305. else if (event == MEDIA_EVENT_CLOSE_REQUEST)
  306. {
  307. // The browser instance wants its window closed.
  308. close();
  309. }
  310. else if (event == MEDIA_EVENT_GEOMETRY_CHANGE)
  311. {
  312. geometryChanged(self->getGeometryX(), self->getGeometryY(),
  313. self->getGeometryWidth(), self->getGeometryHeight());
  314. }
  315. }
  316. void LLFloaterMediaBrowser::setCurrentURL(const std::string& url)
  317. {
  318. mCurrentURL = url;
  319. // Redirects will navigate momentarily to about:blank: do not add to
  320. // history
  321. if (mCurrentURL != "about:blank")
  322. {
  323. mAddressCombo->remove(mCurrentURL);
  324. mAddressCombo->add(mCurrentURL, ADD_SORTED);
  325. mAddressCombo->selectByValue(mCurrentURL);
  326. // Serialize url history
  327. LLURLHistory::removeURL("browser", mCurrentURL);
  328. LLURLHistory::addURL("browser", mCurrentURL);
  329. }
  330. mBackButton->setEnabled(mBrowser->canNavigateBack());
  331. mForwardButton->setEnabled(mBrowser->canNavigateForward());
  332. mReloadButton->setEnabled(true);
  333. }
  334. //static
  335. void LLFloaterMediaBrowser::onEnterAddress(LLUICtrl* ctrl, void* user_data)
  336. {
  337. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  338. if (self)
  339. {
  340. self->mBrowser->navigateTo(self->mAddressCombo->getValue().asString());
  341. }
  342. }
  343. //static
  344. void LLFloaterMediaBrowser::onClickRefresh(void* user_data)
  345. {
  346. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  347. if (self)
  348. {
  349. self->mAddressCombo->remove(0);
  350. std::string url = self->mCurrentURL;
  351. // Force a reload by changing the page first
  352. self->mBrowser->navigateTo("about:blank");
  353. self->mBrowser->navigateTo(url);
  354. }
  355. }
  356. //static
  357. void LLFloaterMediaBrowser::onClickForward(void* user_data)
  358. {
  359. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  360. if (self)
  361. {
  362. self->mBrowser->navigateForward();
  363. }
  364. }
  365. //static
  366. void LLFloaterMediaBrowser::onClickBack(void* user_data)
  367. {
  368. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  369. if (self)
  370. {
  371. self->mBrowser->navigateBack();
  372. }
  373. }
  374. //static
  375. void LLFloaterMediaBrowser::onClickGo(void* user_data)
  376. {
  377. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  378. self->mBrowser->navigateTo(self->mAddressCombo->getValue().asString());
  379. }
  380. //static
  381. void LLFloaterMediaBrowser::onClickClose(void* user_data)
  382. {
  383. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  384. if (self)
  385. {
  386. self->close();
  387. }
  388. }
  389. //static
  390. void LLFloaterMediaBrowser::onClickOpenWebBrowser(void* user_data)
  391. {
  392. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  393. if (self)
  394. {
  395. // NOTE: we favour the URL in the combo box, because in case of a page
  396. // loading failure (SSL handshake failures, for example), mCurrentURL
  397. // contains about:blank or another URL than the failed page URL...
  398. std::string url = self->mAddressCombo->getValue().asString();
  399. if (url.empty())
  400. {
  401. url = self->mCurrentURL;
  402. }
  403. if (url.empty())
  404. {
  405. url = self->mBrowser->getHomePageUrl();
  406. }
  407. LLWeb::loadURLExternal(url);
  408. }
  409. }
  410. void LLFloaterMediaBrowser::onClickAssign(void* user_data)
  411. {
  412. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  413. if (!self) return;
  414. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  415. if (!parcel)
  416. {
  417. return;
  418. }
  419. std::string media_url = self->mAddressCombo->getValue().asString();
  420. LLStringUtil::trim(media_url);
  421. if (parcel->getMediaType() != HTTP_CONTENT_TEXT_HTML)
  422. {
  423. parcel->setMediaURL(media_url);
  424. parcel->setMediaCurrentURL(media_url);
  425. parcel->setMediaType(HTTP_CONTENT_TEXT_HTML);
  426. gViewerParcelMgr.sendParcelPropertiesUpdate(parcel, true);
  427. LLViewerParcelMedia::sendMediaNavigateMessage(media_url);
  428. LLViewerParcelMedia::stop();
  429. #if 0
  430. LLViewerParcelMedia::update(parcel);
  431. #endif
  432. }
  433. LLViewerParcelMedia::sendMediaNavigateMessage(media_url);
  434. }
  435. //static
  436. void LLFloaterMediaBrowser::onClickRewind(void* user_data)
  437. {
  438. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  439. if (!self) return; // Paranoia
  440. LLPluginClassMedia* pluginp = self->mBrowser->getMediaPlugin();
  441. if (pluginp)
  442. {
  443. pluginp->start(-2.f);
  444. }
  445. }
  446. //static
  447. void LLFloaterMediaBrowser::onClickPlay(void* user_data)
  448. {
  449. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  450. if (!self) return; // Paranoia
  451. LLPluginClassMedia* pluginp = self->mBrowser->getMediaPlugin();
  452. if (!pluginp) return;
  453. if (pluginp->getStatus() == LLPluginClassMediaOwner::MEDIA_PLAYING)
  454. {
  455. pluginp->pause();
  456. }
  457. else
  458. {
  459. pluginp->start();
  460. }
  461. }
  462. //static
  463. void LLFloaterMediaBrowser::onClickStop(void* user_data)
  464. {
  465. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  466. if (!self) return; // Paranoia
  467. LLPluginClassMedia* pluginp = self->mBrowser->getMediaPlugin();
  468. if (pluginp)
  469. {
  470. pluginp->stop();
  471. }
  472. }
  473. //static
  474. void LLFloaterMediaBrowser::onClickSeek(void* user_data)
  475. {
  476. LLFloaterMediaBrowser* self = (LLFloaterMediaBrowser*)user_data;
  477. if (!self) return; // Paranoia
  478. LLPluginClassMedia* pluginp = self->mBrowser->getMediaPlugin();
  479. if (pluginp)
  480. {
  481. pluginp->start(2.f);
  482. }
  483. }
  484. void LLFloaterMediaBrowser::openMedia(const std::string& media_url,
  485. bool trusted)
  486. {
  487. openMedia(media_url, "", trusted);
  488. }
  489. void LLFloaterMediaBrowser::openMedia(const std::string& media_url,
  490. const std::string& target,
  491. bool trusted)
  492. {
  493. mBrowser->setHomePageUrl(media_url);
  494. mBrowser->setTarget(target);
  495. mBrowser->setTrusted(trusted);
  496. mAddressCombo->setEnabled(!trusted);
  497. mGoButton->setEnabled(!trusted);
  498. mAddressCombo->setVisible(false);
  499. mLoadingText->setVisible(true);
  500. mBrowser->navigateTo(media_url);
  501. setCurrentURL(media_url);
  502. }
  503. ////////////////////////////////////////////////////////////////////////////////
  504. // LLViewerHtmlHelp class
  505. ////////////////////////////////////////////////////////////////////////////////
  506. LLViewerHtmlHelp::LLViewerHtmlHelp()
  507. {
  508. LLUI::setHtmlHelp(this);
  509. }
  510. LLViewerHtmlHelp::~LLViewerHtmlHelp()
  511. {
  512. LLUI::setHtmlHelp(NULL);
  513. }
  514. void LLViewerHtmlHelp::show()
  515. {
  516. show("");
  517. }
  518. void LLViewerHtmlHelp::show(std::string url)
  519. {
  520. if (url.empty())
  521. {
  522. url = LLGridManager::getInstance()->getSupportURL();
  523. }
  524. if (gSavedSettings.getBool("UseExternalBrowser"))
  525. {
  526. LLSD data;
  527. data["url"] = url;
  528. gNotifications.add("ClickOpenF1Help", data, LLSD(),
  529. onClickF1HelpLoadURL);
  530. return;
  531. }
  532. LLFloaterMediaBrowser* floaterp = LLFloaterMediaBrowser::getInstance(url);
  533. if (floaterp)
  534. {
  535. floaterp->setVisible(true);
  536. floaterp->openMedia(url);
  537. }
  538. }
  539. //static
  540. bool LLViewerHtmlHelp::onClickF1HelpLoadURL(const LLSD& notification,
  541. const LLSD& response)
  542. {
  543. if (LLNotification::getSelectedOption(notification, response) == 0)
  544. {
  545. LLWeb::loadURL(LLGridManager::getInstance()->getSupportURL());
  546. }
  547. return false;
  548. }