Sirikata
|
00001 /* Sirikata 00002 * WebView.hpp 00003 * 00004 * Copyright (c) 2009, Adam Jean Simmons 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef _SIRIKATA_GRAPHICS_WEBVIEW_HPP_ 00034 #define _SIRIKATA_GRAPHICS_WEBVIEW_HPP_ 00035 00036 #include <sirikata/ogre/OgreHeaders.hpp> 00037 #include "Ogre.h" 00038 #include <sirikata/ogre/ViewportOverlay.hpp> 00039 #include <sirikata/proxyobject/Invokable.hpp> 00040 #include <vector> 00041 #include <boost/any.hpp> 00042 00043 #include <sirikata/core/util/Liveness.hpp> 00044 #include <sirikata/core/service/Context.hpp> 00045 00046 #ifdef HAVE_BERKELIUM 00047 #include "berkelium/Berkelium.hpp" 00048 #include "berkelium/Widget.hpp" 00049 #include "berkelium/Window.hpp" 00050 #include "berkelium/WindowDelegate.hpp" 00051 #endif 00052 00053 #ifndef HAVE_BERKELIUM 00054 namespace Berkelium { 00055 struct Rect {}; 00056 struct Window; 00057 struct Widget; 00058 struct WindowDelegate { 00059 struct Data { char *message; int length; }; 00060 }; 00061 } 00062 #endif 00063 00064 00065 00066 namespace Sirikata { 00067 00068 typedef Sirikata::DataReference<const char*> JSArgument; 00069 typedef std::vector<JSArgument> JSArguments; 00070 00071 namespace Graphics { 00072 00073 class WebView; 00074 #ifdef HAVE_BERKELIUM 00075 using Berkelium::WideString; 00076 using Berkelium::URLString; 00077 #endif 00078 00083 typedef std::tr1::function<boost::any (WebView*, const JSArguments&)> JSDelegate; 00084 00089 class SIRIKATA_OGRE_EXPORT WebView 00090 : public Ogre::ManualResourceLoader 00091 #ifdef HAVE_BERKELIUM 00092 , public Berkelium::WindowDelegate 00093 #endif 00094 , public Invokable 00095 , public Liveness 00096 { 00097 public: 00098 00099 typedef std::tr1::function<void()> ReadyCallback; 00100 typedef std::tr1::function<void()> ResetReadyCallback; 00101 typedef std::tr1::function<void(const String&)> NavigatedCallback; 00102 typedef std::tr1::function<void(int32 left, int32 top, int32 right, int32 bottom)> UpdateViewportCallback; 00103 00107 void loadURL(const std::string& url); 00108 00114 void loadFile(const std::string& file); 00115 00121 void loadHTML(const std::string& html); 00122 00128 void evaluateJS(const std::string& javascript); 00129 00137 void defaultEvent(const String& name); 00138 00139 void setUpdateViewportCallback(UpdateViewportCallback cb); 00140 00141 void setReadyCallback(ReadyCallback cb); 00142 void setResetReadyCallback(ResetReadyCallback cb); 00143 00144 // Callback to invoke when the browser URL 00145 // changes. Does *not* indicate the page is fully loaded. 00146 void setNavigatedCallback(NavigatedCallback cb); 00147 00155 void bind(const std::string& name, JSDelegate callback); 00156 00157 00158 void setViewport(Ogre::Viewport* newViewport); 00159 00160 void setTransparent(bool isTransparent); 00161 00175 void setIgnoreBounds(bool ignoreBounds = true); 00176 00187 void setIgnoreTransparent(bool ignoreTrans, float threshold = 0.05); 00188 00195 void setMaxUPS(unsigned int maxUPS = 0); 00196 00203 void setOpacity(float opacity); 00204 00211 void setPosition(const OverlayPosition &viewPosition); 00212 00216 void resetPosition(); 00217 00218 void hide(); 00219 00227 void hide(bool fade, unsigned short fadeDurationMS = 300); 00228 00229 void show(); 00230 00238 void show(bool fade, unsigned short fadeDurationMS = 300); 00239 00243 void focus(); 00247 void unfocus(); 00248 00252 void raise(); 00253 00261 void move(int deltaX, int deltaY); 00262 00270 void getExtents(unsigned short &width, unsigned short &height); 00271 00279 int getRelativeX(int absX); 00280 00286 int getRelativeY(int absY); 00287 00296 bool inDraggableRegion(int relX, int relY); 00297 00298 00302 bool isMaterialOnly(); 00303 00304 ViewportOverlay* getOverlay(); 00305 00309 std::string getName(); 00310 00314 std::string getType(); 00315 00319 std::string getURL(); 00320 00324 std::string getViewTextureName(); 00325 00329 std::string getMaterialName(); 00330 00334 bool getVisibility(); 00335 00342 bool getNonStrictVisibility(); 00343 00354 void getDerivedUV(Ogre::Real& u1, Ogre::Real& v1, Ogre::Real& u2, Ogre::Real& v2); 00355 00363 void injectMouseMove(int xPos, int yPos); 00364 00371 void injectMouseWheel(int scrollX, int scrollY); 00372 00380 void injectMouseDown(int xPos, int yPos); 00381 00389 void injectMouseUp(int xPos, int yPos); 00390 00391 void injectCut(); 00392 void injectCopy(); 00393 void injectPaste(); 00394 00395 void injectKeyEvent(bool press, int modifiers, int vk_code, int raw_scancode); 00396 00397 void injectTextEvent(std::string utf8); 00398 00399 void resize(int width, int height); 00400 00401 class WebViewBorderSize { 00402 public: 00403 unsigned short mBorderLeft; 00404 unsigned short mBorderRight; 00405 unsigned short mBorderTop; 00406 unsigned short mBorderBottom; 00407 WebViewBorderSize(unsigned short left, unsigned short right, 00408 unsigned short top, unsigned short bottom) 00409 : mBorderLeft(left), mBorderRight(right), 00410 mBorderTop(top), mBorderBottom(bottom) {} 00411 }; 00412 00413 static const WebViewBorderSize mDefaultBorder; 00414 00415 protected: 00416 Context* mContext; 00417 Network::IOStrandPtr postingStrand; 00418 00419 00420 #ifdef HAVE_BERKELIUM 00421 Berkelium::Window* webView; 00422 00423 // Get Rect used for blitting that doesn't include borders. 00424 Berkelium::Rect getBorderlessRect(Ogre::HardwarePixelBufferSharedPtr pixelBuffer) const; 00425 // Get Rect offset by border 00426 Berkelium::Rect getBorderedRect(const Berkelium::Rect& orig) const; 00427 00435 void blitNewImage( 00436 Ogre::HardwarePixelBufferSharedPtr pixelBuffer, 00437 const unsigned char* srcBuffer, const Berkelium::Rect& srcRect, 00438 const Berkelium::Rect& copyRect, 00439 bool updateAlphaCache 00440 ); 00448 void blitScrollImage( 00449 Ogre::HardwarePixelBufferSharedPtr pixelBuffer, 00450 const Berkelium::Rect& scrollOrigRect, 00451 int scroll_dx, int scroll_dy, 00452 bool updateAlphaCache 00453 ); 00454 00455 void compositeWidgets(Berkelium::Window*); 00456 #endif 00457 00458 std::string viewName; 00460 std::string viewType; 00463 String viewURL; 00465 unsigned short viewWidth; 00467 unsigned short viewHeight; 00469 unsigned short mBorderLeft; 00471 unsigned short mBorderRight; 00473 unsigned short mBorderTop; 00475 unsigned short mBorderBottom; 00476 00478 ViewportOverlay* overlay; 00480 unsigned int maxUpdatePS; 00482 Ogre::Timer timer; 00484 unsigned long lastUpdateTime; 00486 float opacity; 00488 bool usingMask; 00490 unsigned char* alphaCache; 00492 size_t alphaCachePitch; 00493 00495 Ogre::Pass* matPass; 00496 Ogre::TextureUnitState* baseTexUnit; 00497 Ogre::TextureUnitState* maskTexUnit; 00498 00500 bool ignoringTrans; 00501 float transparent; 00502 bool isWebViewTransparent; 00503 bool ignoringBounds; 00504 00505 Ogre::TexturePtr viewTexture; 00506 Ogre::TexturePtr backingTexture; 00507 std::map<Berkelium::Widget*,Ogre::TexturePtr>widgetTextures; 00508 double fadeValue; 00509 bool isFading; 00510 double deltaFadePerMS; 00511 double lastFadeTimeMS; 00513 bool compensateNPOT; 00514 00515 bool okayToDelete; 00517 unsigned short texWidth; 00519 unsigned short texHeight; 00521 std::map<std::string, JSDelegate> delegateMap; 00522 Invokable* mExceptionHandler; 00523 Ogre::FilterOptions texFiltering; 00524 std::pair<std::string, std::string> maskImageParameters; 00525 00526 UpdateViewportCallback mUpdateViewportCallback; 00527 bool mReady; 00528 ReadyCallback mReadyCallback; 00529 ResetReadyCallback mResetReadyCallback; 00530 NavigatedCallback mNavigatedCallback; 00531 00532 bool mUnresponsive; 00533 00534 friend class WebViewManager; 00535 00536 00537 WebView( 00538 Context* ctx, const std::string& name,const std::string& type, 00539 unsigned short width, unsigned short height, 00540 const OverlayPosition &viewPosition,Ogre::uchar zOrder, 00541 Tier tier, Ogre::Viewport* viewport, 00542 Network::IOStrandPtr postBackStrand, 00543 const WebViewBorderSize& border = mDefaultBorder); 00544 00545 00546 WebView( 00547 Context* ctx, const std::string& name, const std::string& type, 00548 unsigned short width, unsigned short height, 00549 Ogre::FilterOptions texFiltering, 00550 Network::IOStrandPtr postBackStrand); 00551 00552 ~WebView(); 00553 00554 void createWebView(bool resetting); 00555 00556 void initializeWebView( 00557 #ifdef HAVE_BERKELIUM 00558 Berkelium::Window *win, 00559 #endif 00560 bool resetting 00561 ); 00562 void cleanupWebView(); 00563 00564 void createMaterial(); 00565 00566 void loadResource(Ogre::Resource* resource); 00567 00568 void update(); 00569 00570 void updateFade(); 00571 00572 bool isPointOverMe(int x, int y); 00573 00574 // helper for onJavascriptCallback, defaultEvent 00575 boost::any dispatchToDelegate(const String& name, const JSArguments& args); 00576 void dispatchToDelegateNoReturn(const String& name, const JSArguments& args) 00577 { 00578 dispatchToDelegate(name,args); 00579 } 00580 00581 00582 #ifdef HAVE_BERKELIUM 00583 void onAddressBarChanged(Berkelium::Window*, URLString url); 00584 void onStartLoading(Berkelium::Window*, URLString url); 00585 void onTitleChanged(Berkelium::Window*, WideString title); 00586 void onTooltipChanged(Berkelium::Window*, WideString text); 00587 void onLoad(Berkelium::Window*); 00588 void onConsoleMessage(Berkelium::Window *win, WideString message, 00589 WideString sourceId, int line_no); 00590 void onScriptAlert(Berkelium::Window *win, WideString message, 00591 WideString defaultValue, URLString url, 00592 int flags, bool &success, WideString &value); 00593 void onPaint(Berkelium::Window *win, 00594 const unsigned char *sourceBuffer, 00595 const Berkelium::Rect &sourceBufferRect, 00596 size_t numCopyRects, const Berkelium::Rect *copyRects, 00597 int dx, int dy, const Berkelium::Rect &scrollRect); 00598 00599 void onJavascriptCallback(Berkelium::Window *win, void* replyMsg, URLString origin, WideString funcName, Berkelium::Script::Variant *args, size_t numArgs); 00600 00601 void onCrashed(Berkelium::Window*); 00602 void onResponsive(Berkelium::Window*); 00603 void onUnresponsive(Berkelium::Window*); 00604 00605 void onCreatedWindow(Berkelium::Window*, Berkelium::Window*); 00606 00607 void onWidgetCreated(Berkelium::Window *win, Berkelium::Widget *newWidget, int zIndex); 00608 void onWidgetDestroyed(Berkelium::Window *win, Berkelium::Widget *newWidget); 00609 void onWidgetResize(Berkelium::Window *win, Berkelium::Widget *widg, int w, int h); 00610 void onWidgetMove(Berkelium::Window *win, Berkelium::Widget *widg, int x, int y); 00611 void onWidgetPaint( 00612 Berkelium::Window *win, 00613 Berkelium::Widget *wid, 00614 const unsigned char *sourceBuffer, 00615 const Berkelium::Rect &rect, 00616 size_t numCopyRects, 00617 const Berkelium::Rect *copyRects, 00618 int dx, int dy, 00619 const Berkelium::Rect &scrollRect); 00620 #endif // HAVE_BERKELIUM 00621 00622 00623 virtual boost::any invoke(std::vector<boost::any>& params); 00624 // Helpers for invokable calls that produce callbacks later 00625 boost::any translateParamsAndInvoke(Invokable*, WebView*, const JSArguments&); 00626 void forwardOnNavigateToInvokable(Invokable* _invokable, const String& url); 00627 00628 00633 boost::any handleReadyCallback(WebView* wv, const JSArguments& args); 00634 00638 boost::any handleSetUIViewport(WebView* wv, const JSArguments& args); 00639 00641 boost::any userLog(WebView* wv, const JSArguments& args); 00642 00647 boost::any handleOpenBrowser(WebView* wv, const JSArguments& args); 00648 00651 boost::any handleListenToBrowser(WebView* wv, const JSArguments& args); 00652 00655 boost::any handleGetBrowserURL(WebView* wv, const JSArguments& args); 00656 00657 // Helper handler for forwarding navigation events to listener 00658 void forwardBrowserNavigatedCallback(Liveness::Token alive, const String& cb_name, const String& url); 00659 00662 boost::any handleCloseBrowser(WebView* wv, const JSArguments& args); 00663 00664 // Helper handler for when a webview goes unresponsive. 00665 void handleUnresponsiveTimeout(Liveness::Token alive); 00666 00667 // Does the real work for crashing/unresponsive pages, clearing out old 00668 // one and starting up a new copy 00669 void restartPage(); 00670 00671 }; 00672 } 00673 } 00674 00675 #endif