PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/media_plugins/webkit/media_plugin_webkit.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1481 lines | 1088 code | 188 blank | 205 comment | 249 complexity | 6b7a9d3e83388018b8532dc550608c4c MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file media_plugin_webkit.cpp
  3. * @brief Webkit plugin for LLMedia API plugin system
  4. *
  5. * @cond
  6. * $LicenseInfo:firstyear=2008&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. * @endcond
  27. */
  28. #include "llqtwebkit.h"
  29. #include "linden_common.h"
  30. #include "indra_constants.h" // for indra keyboard codes
  31. #include "lltimer.h"
  32. #include "llgl.h"
  33. #include "llplugininstance.h"
  34. #include "llpluginmessage.h"
  35. #include "llpluginmessageclasses.h"
  36. #include "media_plugin_base.h"
  37. // set to 1 if you're using the version of llqtwebkit that's QPixmap-ified
  38. #if LL_LINUX
  39. # define LL_QTWEBKIT_USES_PIXMAPS 0
  40. extern "C" {
  41. # include <glib.h>
  42. # include <glib-object.h>
  43. }
  44. #else
  45. # define LL_QTWEBKIT_USES_PIXMAPS 0
  46. #endif // LL_LINUX
  47. # include "volume_catcher.h"
  48. #if LL_WINDOWS
  49. # include <direct.h>
  50. #else
  51. # include <unistd.h>
  52. # include <stdlib.h>
  53. #endif
  54. #if LL_WINDOWS
  55. // *NOTE:Mani - This captures the module handle for the dll. This is used below
  56. // to get the path to this dll for webkit initialization.
  57. // I don't know how/if this can be done with apr...
  58. namespace { HMODULE gModuleHandle;};
  59. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  60. {
  61. gModuleHandle = (HMODULE) hinstDLL;
  62. return TRUE;
  63. }
  64. #endif
  65. ////////////////////////////////////////////////////////////////////////////////
  66. //
  67. class MediaPluginWebKit :
  68. public MediaPluginBase,
  69. public LLEmbeddedBrowserWindowObserver
  70. {
  71. public:
  72. MediaPluginWebKit(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data);
  73. ~MediaPluginWebKit();
  74. /*virtual*/ void receiveMessage(const char *message_string);
  75. private:
  76. std::string mProfileDir;
  77. std::string mHostLanguage;
  78. std::string mUserAgent;
  79. bool mCookiesEnabled;
  80. bool mJavascriptEnabled;
  81. bool mPluginsEnabled;
  82. bool mEnableMediaPluginDebugging;
  83. enum
  84. {
  85. INIT_STATE_UNINITIALIZED, // LLQtWebkit hasn't been set up yet
  86. INIT_STATE_INITIALIZED, // LLQtWebkit has been set up, but no browser window has been created yet.
  87. INIT_STATE_NAVIGATING, // Browser instance has been set up and initial navigate to about:blank has been issued
  88. INIT_STATE_NAVIGATE_COMPLETE, // initial navigate to about:blank has completed
  89. INIT_STATE_WAIT_REDRAW, // First real navigate begin has been received, waiting for page changed event to start handling redraws
  90. INIT_STATE_WAIT_COMPLETE, // Waiting for first real navigate complete event
  91. INIT_STATE_RUNNING // All initialization gymnastics are complete.
  92. };
  93. int mBrowserWindowId;
  94. int mInitState;
  95. std::string mInitialNavigateURL;
  96. bool mNeedsUpdate;
  97. bool mCanCut;
  98. bool mCanCopy;
  99. bool mCanPaste;
  100. int mLastMouseX;
  101. int mLastMouseY;
  102. bool mFirstFocus;
  103. F32 mBackgroundR;
  104. F32 mBackgroundG;
  105. F32 mBackgroundB;
  106. std::string mTarget;
  107. LLTimer mElapsedTime;
  108. VolumeCatcher mVolumeCatcher;
  109. void postDebugMessage( const std::string& msg )
  110. {
  111. if ( mEnableMediaPluginDebugging )
  112. {
  113. std::stringstream str;
  114. str << "@Media Msg> " << "[" << (double)mElapsedTime.getElapsedTimeF32() << "] -- " << msg;
  115. LLPluginMessage debug_message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "debug_message");
  116. debug_message.setValue("message_text", str.str());
  117. debug_message.setValue("message_level", "info");
  118. sendMessage(debug_message);
  119. }
  120. }
  121. void setInitState(int state)
  122. {
  123. // std::cerr << "changing init state to " << state << std::endl;
  124. mInitState = state;
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////
  127. //
  128. void update(int milliseconds)
  129. {
  130. #if LL_QTLINUX_DOESNT_HAVE_GLIB
  131. // pump glib generously, as Linux browser plugins are on the
  132. // glib main loop, even if the browser itself isn't - ugh
  133. // This is NOT NEEDED if Qt itself was built with glib
  134. // mainloop integration.
  135. GMainContext *mainc = g_main_context_default();
  136. while(g_main_context_iteration(mainc, FALSE));
  137. #endif // LL_QTLINUX_DOESNT_HAVE_GLIB
  138. // pump qt
  139. LLQtWebKit::getInstance()->pump( milliseconds );
  140. mVolumeCatcher.pump();
  141. checkEditState();
  142. if(mInitState == INIT_STATE_NAVIGATE_COMPLETE)
  143. {
  144. if(!mInitialNavigateURL.empty())
  145. {
  146. // We already have the initial navigate URL -- kick off the navigate.
  147. LLQtWebKit::getInstance()->navigateTo( mBrowserWindowId, mInitialNavigateURL );
  148. mInitialNavigateURL.clear();
  149. }
  150. }
  151. if ( (mInitState > INIT_STATE_WAIT_REDRAW) && mNeedsUpdate )
  152. {
  153. const unsigned char* browser_pixels = LLQtWebKit::getInstance()->grabBrowserWindow( mBrowserWindowId );
  154. unsigned int rowspan = LLQtWebKit::getInstance()->getBrowserRowSpan( mBrowserWindowId );
  155. unsigned int height = LLQtWebKit::getInstance()->getBrowserHeight( mBrowserWindowId );
  156. #if !LL_QTWEBKIT_USES_PIXMAPS
  157. unsigned int buffer_size = rowspan * height;
  158. #endif // !LL_QTWEBKIT_USES_PIXMAPS
  159. // std::cerr << "webkit plugin: updating" << std::endl;
  160. // TODO: should get rid of this memcpy if possible
  161. if ( mPixels && browser_pixels )
  162. {
  163. // std::cerr << " memcopy of " << buffer_size << " bytes" << std::endl;
  164. #if LL_QTWEBKIT_USES_PIXMAPS
  165. // copy the pixel data upside-down because of the co-ord system
  166. for (int y=0; y<height; ++y)
  167. {
  168. memcpy( &mPixels[(height-y-1)*rowspan], &browser_pixels[y*rowspan], rowspan );
  169. }
  170. #else
  171. memcpy( mPixels, browser_pixels, buffer_size );
  172. #endif // LL_QTWEBKIT_USES_PIXMAPS
  173. }
  174. if ( mWidth > 0 && mHeight > 0 )
  175. {
  176. // std::cerr << "Setting dirty, " << mWidth << " x " << mHeight << std::endl;
  177. setDirty( 0, 0, mWidth, mHeight );
  178. }
  179. mNeedsUpdate = false;
  180. };
  181. };
  182. ////////////////////////////////////////////////////////////////////////////////
  183. //
  184. bool initBrowser()
  185. {
  186. // already initialized
  187. if ( mInitState > INIT_STATE_UNINITIALIZED )
  188. return true;
  189. // set up directories
  190. char cwd[ FILENAME_MAX ]; // I *think* this is defined on all platforms we use
  191. if (NULL == getcwd( cwd, FILENAME_MAX - 1 ))
  192. {
  193. llwarns << "Couldn't get cwd - probably too long - failing to init." << llendl;
  194. return false;
  195. }
  196. std::string application_dir = std::string( cwd );
  197. #if LL_LINUX
  198. // take care to initialize glib properly, because some
  199. // versions of Qt don't, and we indirectly need it for (some
  200. // versions of) Flash to not crash the browser.
  201. if (!g_thread_supported ()) g_thread_init (NULL);
  202. g_type_init();
  203. #endif
  204. #if LL_DARWIN
  205. // When running under the Xcode debugger, there's a setting called "Break on Debugger()/DebugStr()" which defaults to being turned on.
  206. // This causes the environment variable USERBREAK to be set to 1, which causes these legacy calls to break into the debugger.
  207. // This wouldn't cause any problems except for the fact that the current release version of the Flash plugin has a call to Debugger() in it
  208. // which gets hit when the plugin is probed by webkit.
  209. // Unsetting the environment variable here works around this issue.
  210. unsetenv("USERBREAK");
  211. #endif
  212. #if LL_WINDOWS
  213. //*NOTE:Mani - On windows, at least, the component path is the
  214. // location of this dll's image file.
  215. std::string component_dir;
  216. char dll_path[_MAX_PATH];
  217. DWORD len = GetModuleFileNameA(gModuleHandle, (LPCH)&dll_path, _MAX_PATH);
  218. while(len && dll_path[ len ] != ('\\') )
  219. {
  220. len--;
  221. }
  222. if(len >= 0)
  223. {
  224. dll_path[len] = 0;
  225. component_dir = dll_path;
  226. }
  227. else
  228. {
  229. // *NOTE:Mani - This case should be an rare exception.
  230. // GetModuleFileNameA should always give you a full path, no?
  231. component_dir = application_dir;
  232. }
  233. #else
  234. std::string component_dir = application_dir;
  235. #endif
  236. // debug spam sent to viewer and displayed in the log as usual
  237. postDebugMessage( "Component dir set to: " + component_dir );
  238. // window handle - needed on Windows and must be app window.
  239. #if LL_WINDOWS
  240. char window_title[ MAX_PATH ];
  241. GetConsoleTitleA( window_title, MAX_PATH );
  242. void* native_window_handle = (void*)FindWindowA( NULL, window_title );
  243. #else
  244. void* native_window_handle = 0;
  245. #endif
  246. // main browser initialization
  247. bool result = LLQtWebKit::getInstance()->init( application_dir, component_dir, mProfileDir, native_window_handle );
  248. if ( result )
  249. {
  250. mInitState = INIT_STATE_INITIALIZED;
  251. // debug spam sent to viewer and displayed in the log as usual
  252. postDebugMessage( "browser initialized okay" );
  253. return true;
  254. };
  255. // debug spam sent to viewer and displayed in the log as usual
  256. postDebugMessage( "browser nOT initialized." );
  257. return false;
  258. };
  259. ////////////////////////////////////////////////////////////////////////////////
  260. //
  261. bool initBrowserWindow()
  262. {
  263. // already initialized
  264. if ( mInitState > INIT_STATE_INITIALIZED )
  265. return true;
  266. // not enough information to initialize the browser yet.
  267. if ( mWidth < 0 || mHeight < 0 || mDepth < 0 ||
  268. mTextureWidth < 0 || mTextureHeight < 0 )
  269. {
  270. return false;
  271. };
  272. // Set up host language before creating browser window
  273. if(!mHostLanguage.empty())
  274. {
  275. LLQtWebKit::getInstance()->setHostLanguage(mHostLanguage);
  276. postDebugMessage( "Setting language to " + mHostLanguage );
  277. }
  278. // turn on/off cookies based on what host app tells us
  279. LLQtWebKit::getInstance()->enableCookies( mCookiesEnabled );
  280. // turn on/off plugins based on what host app tells us
  281. LLQtWebKit::getInstance()->enablePlugins( mPluginsEnabled );
  282. // turn on/off Javascript based on what host app tells us
  283. #if LLQTWEBKIT_API_VERSION >= 11
  284. LLQtWebKit::getInstance()->enableJavaScript( mJavascriptEnabled );
  285. #else
  286. LLQtWebKit::getInstance()->enableJavascript( mJavascriptEnabled );
  287. #endif
  288. std::stringstream str;
  289. str << "Cookies enabled = " << mCookiesEnabled << ", plugins enabled = " << mPluginsEnabled << ", Javascript enabled = " << mJavascriptEnabled;
  290. postDebugMessage( str.str() );
  291. // create single browser window
  292. mBrowserWindowId = LLQtWebKit::getInstance()->createBrowserWindow( mWidth, mHeight, mTarget);
  293. str.str("");
  294. str.clear();
  295. str << "Setting browser window size to " << mWidth << " x " << mHeight;
  296. postDebugMessage( str.str() );
  297. // tell LLQtWebKit about the size of the browser window
  298. LLQtWebKit::getInstance()->setSize( mBrowserWindowId, mWidth, mHeight );
  299. // observer events that LLQtWebKit emits
  300. LLQtWebKit::getInstance()->addObserver( mBrowserWindowId, this );
  301. // append details to agent string
  302. LLQtWebKit::getInstance()->setBrowserAgentId( mUserAgent );
  303. postDebugMessage( "Updating user agent with " + mUserAgent );
  304. #if !LL_QTWEBKIT_USES_PIXMAPS
  305. // don't flip bitmap
  306. LLQtWebKit::getInstance()->flipWindow( mBrowserWindowId, true );
  307. #endif // !LL_QTWEBKIT_USES_PIXMAPS
  308. // set background color
  309. // convert background color channels from [0.0, 1.0] to [0, 255];
  310. LLQtWebKit::getInstance()->setBackgroundColor( mBrowserWindowId, int(mBackgroundR * 255.0f), int(mBackgroundG * 255.0f), int(mBackgroundB * 255.0f) );
  311. // Set state _before_ starting the navigate, since onNavigateBegin might get called before this call returns.
  312. setInitState(INIT_STATE_NAVIGATING);
  313. // Don't do this here -- it causes the dreaded "white flash" when loading a browser instance.
  314. // FIXME: Re-added this because navigating to a "page" initializes things correctly - especially
  315. // for the HTTP AUTH dialog issues (DEV-41731). Will fix at a later date.
  316. // Build a data URL like this: "data:text/html,%3Chtml%3E%3Cbody bgcolor=%22#RRGGBB%22%3E%3C/body%3E%3C/html%3E"
  317. // where RRGGBB is the background color in HTML style
  318. std::stringstream url;
  319. url << "data:text/html,%3Chtml%3E%3Cbody%20bgcolor=%22#";
  320. // convert background color channels from [0.0, 1.0] to [0, 255];
  321. url << std::setfill('0') << std::setw(2) << std::hex << int(mBackgroundR * 255.0f);
  322. url << std::setfill('0') << std::setw(2) << std::hex << int(mBackgroundG * 255.0f);
  323. url << std::setfill('0') << std::setw(2) << std::hex << int(mBackgroundB * 255.0f);
  324. url << "%22%3E%3C/body%3E%3C/html%3E";
  325. //lldebugs << "data url is: " << url.str() << llendl;
  326. // always display loading overlay now
  327. #if LLQTWEBKIT_API_VERSION >= 16
  328. LLQtWebKit::getInstance()->enableLoadingOverlay(mBrowserWindowId, true);
  329. #else
  330. llwarns << "Ignoring enableLoadingOverlay() call (llqtwebkit version is too old)." << llendl;
  331. #endif
  332. str.clear();
  333. str << "Loading overlay enabled = " << mEnableMediaPluginDebugging << " for mBrowserWindowId = " << mBrowserWindowId;
  334. postDebugMessage( str.str() );
  335. LLQtWebKit::getInstance()->navigateTo( mBrowserWindowId, url.str() );
  336. // LLQtWebKit::getInstance()->navigateTo( mBrowserWindowId, "about:blank" );
  337. return true;
  338. }
  339. void setVolume(F32 vol);
  340. ////////////////////////////////////////////////////////////////////////////////
  341. // virtual
  342. void onCursorChanged(const EventType& event)
  343. {
  344. LLQtWebKit::ECursor llqt_cursor = (LLQtWebKit::ECursor)event.getIntValue();
  345. std::string name;
  346. switch(llqt_cursor)
  347. {
  348. case LLQtWebKit::C_ARROW:
  349. name = "arrow";
  350. break;
  351. case LLQtWebKit::C_IBEAM:
  352. name = "ibeam";
  353. break;
  354. case LLQtWebKit::C_SPLITV:
  355. name = "splitv";
  356. break;
  357. case LLQtWebKit::C_SPLITH:
  358. name = "splith";
  359. break;
  360. case LLQtWebKit::C_POINTINGHAND:
  361. name = "hand";
  362. break;
  363. default:
  364. llwarns << "Unknown cursor ID: " << (int)llqt_cursor << llendl;
  365. break;
  366. }
  367. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "cursor_changed");
  368. message.setValue("name", name);
  369. sendMessage(message);
  370. }
  371. ////////////////////////////////////////////////////////////////////////////////
  372. // virtual
  373. void onPageChanged( const EventType& event )
  374. {
  375. if(mInitState == INIT_STATE_WAIT_REDRAW)
  376. {
  377. setInitState(INIT_STATE_WAIT_COMPLETE);
  378. }
  379. // flag that an update is required
  380. mNeedsUpdate = true;
  381. };
  382. ////////////////////////////////////////////////////////////////////////////////
  383. // virtual
  384. void onNavigateBegin(const EventType& event)
  385. {
  386. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  387. {
  388. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "navigate_begin");
  389. message.setValue("uri", event.getEventUri());
  390. message.setValueBoolean("history_back_available", LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_BACK));
  391. message.setValueBoolean("history_forward_available", LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_FORWARD));
  392. sendMessage(message);
  393. // debug spam sent to viewer and displayed in the log as usual
  394. postDebugMessage( "Navigate begin event at: " + event.getEventUri() );
  395. setStatus(STATUS_LOADING);
  396. }
  397. if(mInitState == INIT_STATE_NAVIGATE_COMPLETE)
  398. {
  399. // Skip the WAIT_REDRAW state now -- with the right background color set, it should no longer be necessary.
  400. // setInitState(INIT_STATE_WAIT_REDRAW);
  401. setInitState(INIT_STATE_WAIT_COMPLETE);
  402. }
  403. }
  404. ////////////////////////////////////////////////////////////////////////////////
  405. // virtual
  406. void onNavigateComplete(const EventType& event)
  407. {
  408. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  409. {
  410. if(mInitState < INIT_STATE_RUNNING)
  411. {
  412. setInitState(INIT_STATE_RUNNING);
  413. // Clear the history, so the "back" button doesn't take you back to "about:blank".
  414. LLQtWebKit::getInstance()->clearHistory(mBrowserWindowId);
  415. }
  416. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "navigate_complete");
  417. message.setValue("uri", event.getEventUri());
  418. message.setValueS32("result_code", event.getIntValue());
  419. message.setValue("result_string", event.getStringValue());
  420. message.setValueBoolean("history_back_available", LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_BACK));
  421. message.setValueBoolean("history_forward_available", LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_FORWARD));
  422. sendMessage(message);
  423. setStatus(STATUS_LOADED);
  424. }
  425. else if(mInitState == INIT_STATE_NAVIGATING)
  426. {
  427. setInitState(INIT_STATE_NAVIGATE_COMPLETE);
  428. }
  429. // debug spam sent to viewer and displayed in the log as usual
  430. postDebugMessage( "Navigate complete event at: " + event.getEventUri() );
  431. }
  432. ////////////////////////////////////////////////////////////////////////////////
  433. // virtual
  434. void onUpdateProgress(const EventType& event)
  435. {
  436. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  437. {
  438. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "progress");
  439. message.setValueS32("percent", event.getIntValue());
  440. sendMessage(message);
  441. }
  442. }
  443. ////////////////////////////////////////////////////////////////////////////////
  444. // virtual
  445. void onStatusTextChange(const EventType& event)
  446. {
  447. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  448. {
  449. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "status_text");
  450. message.setValue("status", event.getStringValue());
  451. sendMessage(message);
  452. }
  453. }
  454. ////////////////////////////////////////////////////////////////////////////////
  455. // virtual
  456. void onTitleChange(const EventType& event)
  457. {
  458. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  459. {
  460. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "name_text");
  461. message.setValue("name", event.getStringValue());
  462. sendMessage(message);
  463. }
  464. }
  465. ////////////////////////////////////////////////////////////////////////////////
  466. // virtual
  467. void onNavigateErrorPage(const EventType& event)
  468. {
  469. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "navigate_error_page");
  470. message.setValueS32("status_code", event.getIntValue());
  471. sendMessage(message);
  472. }
  473. ////////////////////////////////////////////////////////////////////////////////
  474. // virtual
  475. void onLocationChange(const EventType& event)
  476. {
  477. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  478. {
  479. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "location_changed");
  480. message.setValue("uri", event.getEventUri());
  481. sendMessage(message);
  482. }
  483. }
  484. ////////////////////////////////////////////////////////////////////////////////
  485. // virtual
  486. void onClickLinkHref(const EventType& event)
  487. {
  488. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "click_href");
  489. message.setValue("uri", event.getEventUri());
  490. message.setValue("target", event.getStringValue());
  491. message.setValue("uuid", event.getStringValue2());
  492. sendMessage(message);
  493. }
  494. ////////////////////////////////////////////////////////////////////////////////
  495. // virtual
  496. void onClickLinkNoFollow(const EventType& event)
  497. {
  498. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "click_nofollow");
  499. message.setValue("uri", event.getEventUri());
  500. #if LLQTWEBKIT_API_VERSION >= 7
  501. message.setValue("nav_type", event.getNavigationType());
  502. #else
  503. message.setValue("nav_type", "clicked");
  504. #endif
  505. sendMessage(message);
  506. }
  507. ////////////////////////////////////////////////////////////////////////////////
  508. // virtual
  509. void onCookieChanged(const EventType& event)
  510. {
  511. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "cookie_set");
  512. message.setValue("cookie", event.getStringValue());
  513. // These could be passed through as well, but aren't really needed.
  514. // message.setValue("uri", event.getEventUri());
  515. // message.setValueBoolean("dead", (event.getIntValue() != 0))
  516. // debug spam
  517. postDebugMessage( "Sending cookie_set message from plugin: " + event.getStringValue() );
  518. sendMessage(message);
  519. }
  520. ////////////////////////////////////////////////////////////////////////////////
  521. // virtual
  522. void onWindowCloseRequested(const EventType& event)
  523. {
  524. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "close_request");
  525. message.setValue("uuid", event.getStringValue());
  526. sendMessage(message);
  527. }
  528. ////////////////////////////////////////////////////////////////////////////////
  529. // virtual
  530. void onWindowGeometryChangeRequested(const EventType& event)
  531. {
  532. int x, y, width, height;
  533. event.getRectValue(x, y, width, height);
  534. // This sometimes gets called with a zero-size request. Don't pass these along.
  535. if(width > 0 && height > 0)
  536. {
  537. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "geometry_change");
  538. message.setValue("uuid", event.getStringValue());
  539. message.setValueS32("x", x);
  540. message.setValueS32("y", y);
  541. message.setValueS32("width", width);
  542. message.setValueS32("height", height);
  543. sendMessage(message);
  544. }
  545. }
  546. ////////////////////////////////////////////////////////////////////////////////
  547. // virtual
  548. std::string onRequestFilePicker( const EventType& eventIn )
  549. {
  550. return blockingPickFile();
  551. }
  552. std::string mAuthUsername;
  553. std::string mAuthPassword;
  554. bool mAuthOK;
  555. ////////////////////////////////////////////////////////////////////////////////
  556. // virtual
  557. bool onAuthRequest(const std::string &in_url, const std::string &in_realm, std::string &out_username, std::string &out_password)
  558. {
  559. mAuthOK = false;
  560. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "auth_request");
  561. message.setValue("url", in_url);
  562. message.setValue("realm", in_realm);
  563. message.setValueBoolean("blocking_request", true);
  564. // The "blocking_request" key in the message means this sendMessage call will block until a response is received.
  565. sendMessage(message);
  566. if(mAuthOK)
  567. {
  568. out_username = mAuthUsername;
  569. out_password = mAuthPassword;
  570. }
  571. return mAuthOK;
  572. }
  573. void authResponse(LLPluginMessage &message)
  574. {
  575. mAuthOK = message.getValueBoolean("ok");
  576. if(mAuthOK)
  577. {
  578. mAuthUsername = message.getValue("username");
  579. mAuthPassword = message.getValue("password");
  580. }
  581. }
  582. ////////////////////////////////////////////////////////////////////////////////
  583. // virtual
  584. void onLinkHovered(const EventType& event)
  585. {
  586. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  587. {
  588. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER, "link_hovered");
  589. message.setValue("link", event.getEventUri());
  590. message.setValue("title", event.getStringValue());
  591. message.setValue("text", event.getStringValue2());
  592. sendMessage(message);
  593. }
  594. }
  595. LLQtWebKit::EKeyboardModifier decodeModifiers(std::string &modifiers)
  596. {
  597. int result = 0;
  598. if(modifiers.find("shift") != std::string::npos)
  599. result |= LLQtWebKit::KM_MODIFIER_SHIFT;
  600. if(modifiers.find("alt") != std::string::npos)
  601. result |= LLQtWebKit::KM_MODIFIER_ALT;
  602. if(modifiers.find("control") != std::string::npos)
  603. result |= LLQtWebKit::KM_MODIFIER_CONTROL;
  604. if(modifiers.find("meta") != std::string::npos)
  605. result |= LLQtWebKit::KM_MODIFIER_META;
  606. return (LLQtWebKit::EKeyboardModifier)result;
  607. }
  608. ////////////////////////////////////////////////////////////////////////////////
  609. //
  610. void deserializeKeyboardData( LLSD native_key_data, uint32_t& native_scan_code, uint32_t& native_virtual_key, uint32_t& native_modifiers )
  611. {
  612. native_scan_code = 0;
  613. native_virtual_key = 0;
  614. native_modifiers = 0;
  615. if( native_key_data.isMap() )
  616. {
  617. #if LL_DARWIN
  618. native_scan_code = (uint32_t)(native_key_data["char_code"].asInteger());
  619. native_virtual_key = (uint32_t)(native_key_data["key_code"].asInteger());
  620. native_modifiers = (uint32_t)(native_key_data["modifiers"].asInteger());
  621. #elif LL_WINDOWS
  622. native_scan_code = (uint32_t)(native_key_data["scan_code"].asInteger());
  623. native_virtual_key = (uint32_t)(native_key_data["virtual_key"].asInteger());
  624. // TODO: I don't think we need to do anything with native modifiers here -- please verify
  625. #elif LL_LINUX
  626. native_scan_code = (uint32_t)(native_key_data["scan_code"].asInteger());
  627. native_virtual_key = (uint32_t)(native_key_data["virtual_key"].asInteger());
  628. native_modifiers = (uint32_t)(native_key_data["modifiers"].asInteger());
  629. #else
  630. // Add other platforms here as needed
  631. #endif
  632. };
  633. };
  634. ////////////////////////////////////////////////////////////////////////////////
  635. //
  636. void keyEvent(LLQtWebKit::EKeyEvent key_event, int key, LLQtWebKit::EKeyboardModifier modifiers, LLSD native_key_data = LLSD::emptyMap())
  637. {
  638. // The incoming values for 'key' will be the ones from indra_constants.h
  639. std::string utf8_text;
  640. if(key < KEY_SPECIAL)
  641. {
  642. // Low-ascii characters need to get passed through.
  643. utf8_text = (char)key;
  644. }
  645. // Any special-case handling we want to do for particular keys...
  646. switch((KEY)key)
  647. {
  648. // ASCII codes for some standard keys
  649. case LLQtWebKit::KEY_BACKSPACE: utf8_text = (char)8; break;
  650. case LLQtWebKit::KEY_TAB: utf8_text = (char)9; break;
  651. case LLQtWebKit::KEY_RETURN: utf8_text = (char)13; break;
  652. case LLQtWebKit::KEY_PAD_RETURN: utf8_text = (char)13; break;
  653. case LLQtWebKit::KEY_ESCAPE: utf8_text = (char)27; break;
  654. default:
  655. break;
  656. }
  657. // std::cerr << "key event " << (int)key_event << ", native_key_data = " << native_key_data << std::endl;
  658. uint32_t native_scan_code = 0;
  659. uint32_t native_virtual_key = 0;
  660. uint32_t native_modifiers = 0;
  661. deserializeKeyboardData( native_key_data, native_scan_code, native_virtual_key, native_modifiers );
  662. LLQtWebKit::getInstance()->keyboardEvent( mBrowserWindowId, key_event, (uint32_t)key, utf8_text.c_str(), modifiers, native_scan_code, native_virtual_key, native_modifiers);
  663. checkEditState();
  664. };
  665. ////////////////////////////////////////////////////////////////////////////////
  666. //
  667. void unicodeInput( const std::string &utf8str, LLQtWebKit::EKeyboardModifier modifiers, LLSD native_key_data = LLSD::emptyMap())
  668. {
  669. uint32_t key = LLQtWebKit::KEY_NONE;
  670. // std::cerr << "unicode input, native_key_data = " << native_key_data << std::endl;
  671. if(utf8str.size() == 1)
  672. {
  673. // The only way a utf8 string can be one byte long is if it's actually a single 7-bit ascii character.
  674. // In this case, use it as the key value.
  675. key = utf8str[0];
  676. }
  677. uint32_t native_scan_code = 0;
  678. uint32_t native_virtual_key = 0;
  679. uint32_t native_modifiers = 0;
  680. deserializeKeyboardData( native_key_data, native_scan_code, native_virtual_key, native_modifiers );
  681. LLQtWebKit::getInstance()->keyboardEvent( mBrowserWindowId, LLQtWebKit::KE_KEY_DOWN, (uint32_t)key, utf8str.c_str(), modifiers, native_scan_code, native_virtual_key, native_modifiers);
  682. LLQtWebKit::getInstance()->keyboardEvent( mBrowserWindowId, LLQtWebKit::KE_KEY_UP, (uint32_t)key, utf8str.c_str(), modifiers, native_scan_code, native_virtual_key, native_modifiers);
  683. checkEditState();
  684. };
  685. void checkEditState(void)
  686. {
  687. bool can_cut = LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_EDIT_CUT);
  688. bool can_copy = LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_EDIT_COPY);
  689. bool can_paste = LLQtWebKit::getInstance()->userActionIsEnabled( mBrowserWindowId, LLQtWebKit::UA_EDIT_PASTE);
  690. if((can_cut != mCanCut) || (can_copy != mCanCopy) || (can_paste != mCanPaste))
  691. {
  692. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "edit_state");
  693. if(can_cut != mCanCut)
  694. {
  695. mCanCut = can_cut;
  696. message.setValueBoolean("cut", can_cut);
  697. }
  698. if(can_copy != mCanCopy)
  699. {
  700. mCanCopy = can_copy;
  701. message.setValueBoolean("copy", can_copy);
  702. }
  703. if(can_paste != mCanPaste)
  704. {
  705. mCanPaste = can_paste;
  706. message.setValueBoolean("paste", can_paste);
  707. }
  708. sendMessage(message);
  709. }
  710. }
  711. std::string mPickedFile;
  712. std::string blockingPickFile(void)
  713. {
  714. mPickedFile.clear();
  715. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "pick_file");
  716. message.setValueBoolean("blocking_request", true);
  717. // The "blocking_request" key in the message means this sendMessage call will block until a response is received.
  718. sendMessage(message);
  719. return mPickedFile;
  720. }
  721. void onPickFileResponse(const std::string &file)
  722. {
  723. mPickedFile = file;
  724. }
  725. };
  726. MediaPluginWebKit::MediaPluginWebKit(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data) :
  727. MediaPluginBase(host_send_func, host_user_data)
  728. {
  729. // std::cerr << "MediaPluginWebKit constructor" << std::endl;
  730. mBrowserWindowId = 0;
  731. mInitState = INIT_STATE_UNINITIALIZED;
  732. mNeedsUpdate = true;
  733. mCanCut = false;
  734. mCanCopy = false;
  735. mCanPaste = false;
  736. mLastMouseX = 0;
  737. mLastMouseY = 0;
  738. mFirstFocus = true;
  739. mBackgroundR = 0.0f;
  740. mBackgroundG = 0.0f;
  741. mBackgroundB = 0.0f;
  742. mHostLanguage = "en"; // default to english
  743. mJavascriptEnabled = true; // default to on
  744. mPluginsEnabled = true; // default to on
  745. mEnableMediaPluginDebugging = false;
  746. mUserAgent = "LLPluginMedia Web Browser";
  747. mElapsedTime.reset();
  748. }
  749. MediaPluginWebKit::~MediaPluginWebKit()
  750. {
  751. // unhook observer
  752. LLQtWebKit::getInstance()->remObserver( mBrowserWindowId, this );
  753. // clean up
  754. LLQtWebKit::getInstance()->reset();
  755. // std::cerr << "MediaPluginWebKit destructor" << std::endl;
  756. }
  757. void MediaPluginWebKit::receiveMessage(const char *message_string)
  758. {
  759. // std::cerr << "MediaPluginWebKit::receiveMessage: received message: \"" << message_string << "\"" << std::endl;
  760. LLPluginMessage message_in;
  761. if(message_in.parse(message_string) >= 0)
  762. {
  763. std::string message_class = message_in.getClass();
  764. std::string message_name = message_in.getName();
  765. if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
  766. {
  767. if(message_name == "init")
  768. {
  769. LLPluginMessage message("base", "init_response");
  770. LLSD versions = LLSD::emptyMap();
  771. versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
  772. versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
  773. versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
  774. message.setValueLLSD("versions", versions);
  775. std::string plugin_version = "Webkit media plugin, Webkit version ";
  776. plugin_version += LLQtWebKit::getInstance()->getVersion();
  777. message.setValue("plugin_version", plugin_version);
  778. sendMessage(message);
  779. }
  780. else if(message_name == "idle")
  781. {
  782. // no response is necessary here.
  783. F64 time = message_in.getValueReal("time");
  784. // Convert time to milliseconds for update()
  785. update((int)(time * 1000.0f));
  786. }
  787. else if(message_name == "cleanup")
  788. {
  789. // DTOR most likely won't be called but the recent change to the way this process
  790. // is (not) killed means we see this message and can do what we need to here.
  791. // Note: this cleanup is ultimately what writes cookies to the disk
  792. LLQtWebKit::getInstance()->remObserver( mBrowserWindowId, this );
  793. LLQtWebKit::getInstance()->reset();
  794. }
  795. else if(message_name == "shm_added")
  796. {
  797. SharedSegmentInfo info;
  798. info.mAddress = message_in.getValuePointer("address");
  799. info.mSize = (size_t)message_in.getValueS32("size");
  800. std::string name = message_in.getValue("name");
  801. // std::cerr << "MediaPluginWebKit::receiveMessage: shared memory added, name: " << name
  802. // << ", size: " << info.mSize
  803. // << ", address: " << info.mAddress
  804. // << std::endl;
  805. mSharedSegments.insert(SharedSegmentMap::value_type(name, info));
  806. }
  807. else if(message_name == "shm_remove")
  808. {
  809. std::string name = message_in.getValue("name");
  810. // std::cerr << "MediaPluginWebKit::receiveMessage: shared memory remove, name = " << name << std::endl;
  811. SharedSegmentMap::iterator iter = mSharedSegments.find(name);
  812. if(iter != mSharedSegments.end())
  813. {
  814. if(mPixels == iter->second.mAddress)
  815. {
  816. // This is the currently active pixel buffer. Make sure we stop drawing to it.
  817. mPixels = NULL;
  818. mTextureSegmentName.clear();
  819. }
  820. mSharedSegments.erase(iter);
  821. }
  822. else
  823. {
  824. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown shared memory region!" << std::endl;
  825. }
  826. // Send the response so it can be cleaned up.
  827. LLPluginMessage message("base", "shm_remove_response");
  828. message.setValue("name", name);
  829. sendMessage(message);
  830. }
  831. else
  832. {
  833. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown base message: " << message_name << std::endl;
  834. }
  835. }
  836. else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME)
  837. {
  838. if(message_name == "set_volume")
  839. {
  840. F32 volume = message_in.getValueReal("volume");
  841. setVolume(volume);
  842. }
  843. }
  844. else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
  845. {
  846. if(message_name == "init")
  847. {
  848. mTarget = message_in.getValue("target");
  849. // This is the media init message -- all necessary data for initialization should have been received.
  850. if(initBrowser())
  851. {
  852. // Plugin gets to decide the texture parameters to use.
  853. mDepth = 4;
  854. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
  855. message.setValueS32("default_width", 1024);
  856. message.setValueS32("default_height", 1024);
  857. message.setValueS32("depth", mDepth);
  858. message.setValueU32("internalformat", GL_RGBA);
  859. #if LL_QTWEBKIT_USES_PIXMAPS
  860. message.setValueU32("format", GL_BGRA_EXT); // I hope this isn't system-dependant... is it? If so, we'll have to check the root window's pixel layout or something... yuck.
  861. #else
  862. message.setValueU32("format", GL_RGBA);
  863. #endif // LL_QTWEBKIT_USES_PIXMAPS
  864. message.setValueU32("type", GL_UNSIGNED_BYTE);
  865. message.setValueBoolean("coords_opengl", true);
  866. sendMessage(message);
  867. }
  868. else
  869. {
  870. // if initialization failed, we're done.
  871. mDeleteMe = true;
  872. }
  873. }
  874. else if(message_name == "set_user_data_path")
  875. {
  876. std::string user_data_path = message_in.getValue("path"); // n.b. always has trailing platform-specific dir-delimiter
  877. mProfileDir = user_data_path + "browser_profile";
  878. // FIXME: Should we do anything with this if it comes in after the browser has been initialized?
  879. }
  880. else if(message_name == "set_language_code")
  881. {
  882. mHostLanguage = message_in.getValue("language");
  883. // FIXME: Should we do anything with this if it comes in after the browser has been initialized?
  884. }
  885. else if(message_name == "plugins_enabled")
  886. {
  887. mPluginsEnabled = message_in.getValueBoolean("enable");
  888. }
  889. else if(message_name == "javascript_enabled")
  890. {
  891. mJavascriptEnabled = message_in.getValueBoolean("enable");
  892. }
  893. else if(message_name == "size_change")
  894. {
  895. std::string name = message_in.getValue("name");
  896. S32 width = message_in.getValueS32("width");
  897. S32 height = message_in.getValueS32("height");
  898. S32 texture_width = message_in.getValueS32("texture_width");
  899. S32 texture_height = message_in.getValueS32("texture_height");
  900. mBackgroundR = message_in.getValueReal("background_r");
  901. mBackgroundG = message_in.getValueReal("background_g");
  902. mBackgroundB = message_in.getValueReal("background_b");
  903. // mBackgroundA = message_in.setValueReal("background_a"); // Ignore any alpha
  904. if(!name.empty())
  905. {
  906. // Find the shared memory region with this name
  907. SharedSegmentMap::iterator iter = mSharedSegments.find(name);
  908. if(iter != mSharedSegments.end())
  909. {
  910. mPixels = (unsigned char*)iter->second.mAddress;
  911. mWidth = width;
  912. mHeight = height;
  913. if(initBrowserWindow())
  914. {
  915. // size changed so tell the browser
  916. LLQtWebKit::getInstance()->setSize( mBrowserWindowId, mWidth, mHeight );
  917. // std::cerr << "webkit plugin: set size to " << mWidth << " x " << mHeight
  918. // << ", rowspan is " << LLQtWebKit::getInstance()->getBrowserRowSpan(mBrowserWindowId) << std::endl;
  919. S32 real_width = LLQtWebKit::getInstance()->getBrowserRowSpan(mBrowserWindowId) / LLQtWebKit::getInstance()->getBrowserDepth(mBrowserWindowId);
  920. // The actual width the browser will be drawing to is probably smaller... let the host know by modifying texture_width in the response.
  921. if(real_width <= texture_width)
  922. {
  923. texture_width = real_width;
  924. }
  925. else
  926. {
  927. // This won't work -- it'll be bigger than the allocated memory. This is a fatal error.
  928. // std::cerr << "Fatal error: browser rowbytes greater than texture width" << std::endl;
  929. mDeleteMe = true;
  930. return;
  931. }
  932. }
  933. else
  934. {
  935. // Setting up the browser window failed. This is a fatal error.
  936. mDeleteMe = true;
  937. }
  938. mTextureWidth = texture_width;
  939. mTextureHeight = texture_height;
  940. };
  941. };
  942. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
  943. message.setValue("name", name);
  944. message.setValueS32("width", width);
  945. message.setValueS32("height", height);
  946. message.setValueS32("texture_width", texture_width);
  947. message.setValueS32("texture_height", texture_height);
  948. sendMessage(message);
  949. }
  950. else if(message_name == "load_uri")
  951. {
  952. std::string uri = message_in.getValue("uri");
  953. // std::cout << "loading URI: " << uri << std::endl;
  954. if(!uri.empty())
  955. {
  956. if(mInitState >= INIT_STATE_NAVIGATE_COMPLETE)
  957. {
  958. LLQtWebKit::getInstance()->navigateTo( mBrowserWindowId, uri );
  959. }
  960. else
  961. {
  962. mInitialNavigateURL = uri;
  963. }
  964. }
  965. }
  966. else if(message_name == "mouse_event")
  967. {
  968. std::string event = message_in.getValue("event");
  969. S32 button = message_in.getValueS32("button");
  970. mLastMouseX = message_in.getValueS32("x");
  971. mLastMouseY = message_in.getValueS32("y");
  972. std::string modifiers = message_in.getValue("modifiers");
  973. // Treat unknown mouse events as mouse-moves.
  974. LLQtWebKit::EMouseEvent mouse_event = LLQtWebKit::ME_MOUSE_MOVE;
  975. if(event == "down")
  976. {
  977. mouse_event = LLQtWebKit::ME_MOUSE_DOWN;
  978. }
  979. else if(event == "up")
  980. {
  981. mouse_event = LLQtWebKit::ME_MOUSE_UP;
  982. }
  983. else if(event == "double_click")
  984. {
  985. mouse_event = LLQtWebKit::ME_MOUSE_DOUBLE_CLICK;
  986. }
  987. LLQtWebKit::getInstance()->mouseEvent( mBrowserWindowId, mouse_event, button, mLastMouseX, mLastMouseY, decodeModifiers(modifiers));
  988. checkEditState();
  989. }
  990. else if(message_name == "scroll_event")
  991. {
  992. S32 x = message_in.getValueS32("x");
  993. S32 y = message_in.getValueS32("y");
  994. std::string modifiers = message_in.getValue("modifiers");
  995. // Incoming scroll events are adjusted so that 1 detent is approximately 1 unit.
  996. // Qt expects 1 detent to be 120 units.
  997. // It also seems that our y scroll direction is inverted vs. what Qt expects.
  998. x *= 120;
  999. y *= -120;
  1000. LLQtWebKit::getInstance()->scrollWheelEvent(mBrowserWindowId, mLastMouseX, mLastMouseY, x, y, decodeModifiers(modifiers));
  1001. }
  1002. else if(message_name == "key_event")
  1003. {
  1004. std::string event = message_in.getValue("event");
  1005. S32 key = message_in.getValueS32("key");
  1006. std::string modifiers = message_in.getValue("modifiers");
  1007. LLSD native_key_data = message_in.getValueLLSD("native_key_data");
  1008. // Treat unknown events as key-up for safety.
  1009. LLQtWebKit::EKeyEvent key_event = LLQtWebKit::KE_KEY_UP;
  1010. if(event == "down")
  1011. {
  1012. key_event = LLQtWebKit::KE_KEY_DOWN;
  1013. }
  1014. else if(event == "repeat")
  1015. {
  1016. key_event = LLQtWebKit::KE_KEY_REPEAT;
  1017. }
  1018. keyEvent(key_event, key, decodeModifiers(modifiers), native_key_data);
  1019. }
  1020. else if(message_name == "text_event")
  1021. {
  1022. std::string text = message_in.getValue("text");
  1023. std::string modifiers = message_in.getValue("modifiers");
  1024. LLSD native_key_data = message_in.getValueLLSD("native_key_data");
  1025. unicodeInput(text, decodeModifiers(modifiers), native_key_data);
  1026. }
  1027. if(message_name == "edit_cut")
  1028. {
  1029. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_EDIT_CUT );
  1030. checkEditState();
  1031. }
  1032. if(message_name == "edit_copy")
  1033. {
  1034. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_EDIT_COPY );
  1035. checkEditState();
  1036. }
  1037. if(message_name == "edit_paste")
  1038. {
  1039. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_EDIT_PASTE );
  1040. checkEditState();
  1041. }
  1042. if(message_name == "pick_file_response")
  1043. {
  1044. onPickFileResponse(message_in.getValue("file"));
  1045. }
  1046. if(message_name == "auth_response")
  1047. {
  1048. authResponse(message_in);
  1049. }
  1050. else
  1051. if(message_name == "enable_media_plugin_debugging")
  1052. {
  1053. mEnableMediaPluginDebugging = message_in.getValueBoolean( "enable" );
  1054. }
  1055. else
  1056. if(message_name == "js_enable_object")
  1057. {
  1058. #if LLQTWEBKIT_API_VERSION >= 9
  1059. bool enable = message_in.getValueBoolean( "enable" );
  1060. LLQtWebKit::getInstance()->setSLObjectEnabled( enable );
  1061. #endif
  1062. }
  1063. else
  1064. if(message_name == "js_agent_location")
  1065. {
  1066. #if LLQTWEBKIT_API_VERSION >= 9
  1067. F32 x = message_in.getValueReal("x");
  1068. F32 y = message_in.getValueReal("y");
  1069. F32 z = message_in.getValueReal("z");
  1070. LLQtWebKit::getInstance()->setAgentLocation( x, y, z );
  1071. LLQtWebKit::getInstance()->emitLocation();
  1072. #endif
  1073. }
  1074. else
  1075. if(message_name == "js_agent_global_location")
  1076. {
  1077. #if LLQTWEBKIT_API_VERSION >= 9
  1078. F32 x = message_in.getValueReal("x");
  1079. F32 y = message_in.getValueReal("y");
  1080. F32 z = message_in.getValueReal("z");
  1081. LLQtWebKit::getInstance()->setAgentGlobalLocation( x, y, z );
  1082. LLQtWebKit::getInstance()->emitLocation();
  1083. #endif
  1084. }
  1085. else
  1086. if(message_name == "js_agent_orientation")
  1087. {
  1088. #if LLQTWEBKIT_API_VERSION >= 9
  1089. F32 angle = message_in.getValueReal("angle");
  1090. LLQtWebKit::getInstance()->setAgentOrientation( angle );
  1091. LLQtWebKit::getInstance()->emitLocation();
  1092. #endif
  1093. }
  1094. else
  1095. if(message_name == "js_agent_region")
  1096. {
  1097. #if LLQTWEBKIT_API_VERSION >= 9
  1098. const std::string& region = message_in.getValue("region");
  1099. LLQtWebKit::getInstance()->setAgentRegion( region );
  1100. LLQtWebKit::getInstance()->emitLocation();
  1101. #endif
  1102. }
  1103. else
  1104. if(message_name == "js_agent_maturity")
  1105. {
  1106. #if LLQTWEBKIT_API_VERSION >= 9
  1107. const std::string& maturity = message_in.getValue("maturity");
  1108. LLQtWebKit::getInstance()->setAgentMaturity( maturity );
  1109. LLQtWebKit::getInstance()->emitMaturity();
  1110. #endif
  1111. }
  1112. else
  1113. if(message_name == "js_agent_language")
  1114. {
  1115. #if LLQTWEBKIT_API_VERSION >= 9
  1116. const std::string& language = message_in.getValue("language");
  1117. LLQtWebKit::getInstance()->setAgentLanguage( language );
  1118. LLQtWebKit::getInstance()->emitLanguage();
  1119. #endif
  1120. }
  1121. else
  1122. {
  1123. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown media message: " << message_string << std::endl;
  1124. }
  1125. }
  1126. else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER)
  1127. {
  1128. if(message_name == "focus")
  1129. {
  1130. bool val = message_in.getValueBoolean("focused");
  1131. LLQtWebKit::getInstance()->focusBrowser( mBrowserWindowId, val );
  1132. if(mFirstFocus && val)
  1133. {
  1134. // On the first focus, post a tab key event. This fixes a problem with initial focus.
  1135. std::string empty;
  1136. keyEvent(LLQtWebKit::KE_KEY_DOWN, KEY_TAB, decodeModifiers(empty));
  1137. keyEvent(LLQtWebKit::KE_KEY_UP, KEY_TAB, decodeModifiers(empty));
  1138. mFirstFocus = false;
  1139. }
  1140. }
  1141. else if(message_name == "set_page_zoom_factor")
  1142. {
  1143. #if LLQTWEBKIT_API_VERSION >= 15
  1144. F32 factor = message_in.getValueReal("factor");
  1145. LLQtWebKit::getInstance()->setPageZoomFactor(factor);
  1146. #else
  1147. llwarns << "Ignoring setPageZoomFactor message (llqtwebkit version is too old)." << llendl;
  1148. #endif
  1149. }
  1150. else if(message_name == "clear_cache")
  1151. {
  1152. LLQtWebKit::getInstance()->clearCache();
  1153. }
  1154. else if(message_name == "clear_cookies")
  1155. {
  1156. LLQtWebKit::getInstance()->clearAllCookies();
  1157. }
  1158. else if(message_name == "enable_cookies")
  1159. {
  1160. mCookiesEnabled = message_in.getValueBoolean("enable");
  1161. LLQtWebKit::getInstance()->enableCookies( mCookiesEnabled );
  1162. }
  1163. else if(message_name == "enable_plugins")
  1164. {
  1165. mPluginsEnabled = message_in.getValueBoolean("enable");
  1166. LLQtWebKit::getInstance()->enablePlugins( mPluginsEnabled );
  1167. }
  1168. else if(message_name == "enable_javascript")
  1169. {
  1170. mJavascriptEnabled = message_in.getValueBoolean("enable");
  1171. //LLQtWebKit::getInstance()->enableJavascript( mJavascriptEnabled );
  1172. }
  1173. else if(message_name == "set_cookies")
  1174. {
  1175. LLQtWebKit::getInstance()->setCookies(message_in.getValue("cookies"));
  1176. // debug spam
  1177. postDebugMessage( "Plugin setting cookie: " + message_in.getValue("cookies") );
  1178. }
  1179. else if(message_name == "proxy_setup")
  1180. {
  1181. bool val = message_in.getValueBoolean("enable");
  1182. std::string host = message_in.getValue("host");
  1183. int port = message_in.getValueS32("port");
  1184. LLQtWebKit::getInstance()->enableProxy( val, host, port );
  1185. }
  1186. else if(message_name == "browse_stop")
  1187. {
  1188. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_STOP );
  1189. }
  1190. else if(message_name == "browse_reload")
  1191. {
  1192. // foo = message_in.getValueBoolean("ignore_cache");
  1193. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_RELOAD );
  1194. }
  1195. else if(message_name == "browse_forward")
  1196. {
  1197. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_FORWARD );
  1198. }
  1199. else if(message_name == "browse_back")
  1200. {
  1201. LLQtWebKit::getInstance()->userAction( mBrowserWindowId, LLQtWebKit::UA_NAVIGATE_BACK );
  1202. }
  1203. else if(message_name == "set_status_redirect")
  1204. {
  1205. int code = message_in.getValueS32("code");
  1206. std::string url = message_in.getValue("url");
  1207. if ( 404 == code ) // browser lib only supports 404 right now
  1208. {
  1209. #if LLQTWEBKIT_API_VERSION < 8
  1210. LLQtWebKit::getInstance()->set404RedirectUrl( mBrowserWindowId, url );
  1211. #endif
  1212. };
  1213. }
  1214. else if(message_name == "set_user_agent")
  1215. {
  1216. mUserAgent = message_in.getValue("user_agent");
  1217. LLQtWebKit::getInstance()->setBrowserAgentId( mUserAgent );
  1218. }
  1219. else if(message_name == "show_web_inspector")
  1220. {
  1221. #if LLQTWEBKIT_API_VERSION >= 10
  1222. bool val = message_in.getValueBoolean("show");
  1223. LLQtWebKit::getInstance()->showWebInspector( val );
  1224. #else
  1225. llwarns << "Ignoring showWebInspector message (llqtwebkit version is too old)." << llendl;
  1226. #endif
  1227. }
  1228. else if(message_name == "ignore_ssl_cert_errors")
  1229. {
  1230. #if LLQTWEBKIT_API_VERSION >= 3
  1231. LLQtWebKit::getInstance()->setIgnoreSSLCertErrors( message_in.getValueBoolean("ignore") );
  1232. #else
  1233. llwarns << "Ignoring ignore_ssl_cert_errors message (llqtwebkit version is too old)." << llendl;
  1234. #endif
  1235. }
  1236. else if(message_name == "add_certificate_file_path")
  1237. {
  1238. #if LLQTWEBKIT_API_VERSION >= 6
  1239. LLQtWebKit::getInstance()->setCAFile( message_in.getValue("path") );
  1240. #else
  1241. llwarns << "Ignoring add_certificate_file_path message (llqtwebkit version is too old)." << llendl;
  1242. #endif
  1243. }
  1244. else if(message_name == "init_history")
  1245. {
  1246. // Initialize browser history
  1247. LLSD history = message_in.getValueLLSD("history");
  1248. // First, clear the URL history
  1249. LLQtWebKit::getInstance()->clearHistory(mBrowserWindowId);
  1250. // Then, add the history items in order
  1251. LLSD::array_iterator iter_history = history.beginArray();
  1252. LLSD::array_iterator end_history = history.endArray();
  1253. for(; iter_history != end_history; ++iter_history)
  1254. {
  1255. std::string url = (*iter_history).asString();
  1256. if(! url.empty()) {
  1257. LLQtWebKit::getInstance()->prependHistoryUrl(mBrowserWindowId, url);
  1258. }
  1259. }
  1260. }
  1261. else if(message_name == "proxy_window_opened")
  1262. {
  1263. std::string target = message_in.getValue("target");
  1264. std::string uuid = message_in.getValue("uuid");
  1265. LLQtWebKit::getInstance()->proxyWindowOpened(mBrowserWindowId, target, uuid);
  1266. }
  1267. else if(message_name == "proxy_window_closed")
  1268. {
  1269. std::string uuid = message_in.getValue("uuid");
  1270. LLQtWebKit::getInstance()->proxyWindowClosed(mBrowserWindowId, uuid);
  1271. }
  1272. else
  1273. {
  1274. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown media_browser message: " << message_string << std::endl;
  1275. };
  1276. }
  1277. else
  1278. {
  1279. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown message class: " << message_class << std::endl;
  1280. };
  1281. }
  1282. }
  1283. void MediaPluginWebKit::setVolume(F32 volume)
  1284. {
  1285. mVolumeCatcher.setVolume(volume);
  1286. }
  1287. int init_media_plugin(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data, LLPluginInstance::sendMessageFunction *plugin_send_func, void **plugin_user_data)
  1288. {
  1289. MediaPluginWebKit *self = new MediaPluginWebKit(host_send_func, host_user_data);
  1290. *plugin_send_func = MediaPluginWebKit::staticReceiveMessage;
  1291. *plugin_user_data = (void*)self;
  1292. return 0;
  1293. }