1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef WebFrame_h 32 #define WebFrame_h 33 34 #include "WebCanvas.h" 35 #include "WebFileSystem.h" 36 #include "WebNode.h" 37 #include "WebURL.h" 38 39 struct NPObject; 40 41 #if WEBKIT_USING_V8 42 namespace v8 { 43 class Context; 44 class Value; 45 template <class T> class Handle; 46 template <class T> class Local; 47 } 48 #endif 49 50 namespace WebKit { 51 52 class WebAnimationController; 53 class WebData; 54 class WebDataSource; 55 class WebDocument; 56 class WebElement; 57 class WebFormElement; 58 class WebHistoryItem; 59 class WebInputElement; 60 class WebPasswordAutocompleteListener; 61 class WebPerformance; 62 class WebRange; 63 class WebSecurityOrigin; 64 class WebString; 65 class WebURL; 66 class WebURLLoader; 67 class WebURLRequest; 68 class WebView; 69 struct WebConsoleMessage; 70 struct WebFindOptions; 71 struct WebPoint; 72 struct WebRect; 73 struct WebScriptSource; 74 struct WebSize; 75 struct WebURLLoaderOptions; 76 77 template <typename T> class WebVector; 78 79 class WebFrame { 80 public: 81 // Returns the number of live WebFrame objects, used for leak checking. 82 WEBKIT_API static int instanceCount(); 83 84 // The two functions below retrieve the WebFrame instances relating the 85 // currently executing JavaScript. Since JavaScript can make function 86 // calls across frames, though, we need to be more precise. 87 // 88 // For example, imagine that a JS function in frame A calls a function 89 // in frame B, which calls native code, which wants to know what the 90 // 'active' frame is. 91 // 92 // The 'entered context' is the context where execution first entered 93 // the script engine; the context that is at the bottom of the JS 94 // function stack. frameForEnteredContext() would return frame A in 95 // our example. 96 // 97 // The 'current context' is the context the JS engine is currently 98 // inside of; the context that is at the top of the JS function stack. 99 // frameForCurrentContext() would return frame B in our example. 100 WEBKIT_API static WebFrame* frameForEnteredContext(); 101 WEBKIT_API static WebFrame* frameForCurrentContext(); 102 103 #if WEBKIT_USING_V8 104 // Returns the frame corresponding to the given context. This can return 0 105 // if the context is detached from the frame, or if the context doesn't 106 // correspond to a frame (e.g., workers). 107 WEBKIT_API static WebFrame* frameForContext(v8::Handle<v8::Context>); 108 #endif 109 110 // Returns the frame inside a given frame or iframe element. Returns 0 if 111 // the given element is not a frame, iframe or if the frame is empty. 112 WEBKIT_API static WebFrame* fromFrameOwnerElement(const WebElement&); 113 114 115 // Basic properties --------------------------------------------------- 116 117 // The name of this frame. 118 virtual WebString name() const = 0; 119 120 // Sets the name of this frame. For child frames (frames that are not a 121 // top-most frame) the actual name may have a suffix appended to make the 122 // frame name unique within the hierarchy. 123 virtual void setName(const WebString&) = 0; 124 125 // A globally unique identifier for this frame. 126 virtual long long identifier() const = 0; 127 128 // The url of the document loaded in this frame. This is equivalent to 129 // dataSource()->request().url(). 130 virtual WebURL url() const = 0; 131 132 // The url of the favicon (if any) specified by the document loaded in 133 // this frame. 134 virtual WebURL favIconURL() const = 0; 135 136 // The url of the OpenSearch Desription Document (if any) specified by 137 // the document loaded in this frame. 138 virtual WebURL openSearchDescriptionURL() const = 0; 139 140 // Return the frame's encoding. 141 virtual WebString encoding() const = 0; 142 143 144 // Geometry ----------------------------------------------------------- 145 146 // NOTE: These routines do not force page layout so their results may 147 // not be accurate if the page layout is out-of-date. 148 149 // If set to false, do not draw scrollbars on this frame's view. 150 virtual void setCanHaveScrollbars(bool) = 0; 151 152 // The scroll offset from the top-left corner of the frame in pixels. 153 virtual WebSize scrollOffset() const = 0; 154 virtual void setScrollOffset(const WebSize&) = 0; 155 156 // The size of the contents area. 157 virtual WebSize contentsSize() const = 0; 158 159 // Returns the minimum preferred width of the content contained in the 160 // current document. 161 virtual int contentsPreferredWidth() const = 0; 162 163 // Returns the scroll height of the document element. This is 164 // equivalent to the DOM property of the same name, and is the minimum 165 // height required to display the document without scrollbars. 166 virtual int documentElementScrollHeight() const = 0; 167 168 // Returns true if the contents (minus scrollbars) has non-zero area. 169 virtual bool hasVisibleContent() const = 0; 170 171 172 // Hierarchy ---------------------------------------------------------- 173 174 // Returns the containing view. 175 virtual WebView* view() const = 0; 176 177 // Returns the frame that opened this frame or 0 if there is none. 178 virtual WebFrame* opener() const = 0; 179 180 // Reset the frame that opened this frame to 0. 181 // This is executed between layout tests runs 182 virtual void clearOpener() = 0; 183 184 // Returns the parent frame or 0 if this is a top-most frame. 185 virtual WebFrame* parent() const = 0; 186 187 // Returns the top-most frame in the hierarchy containing this frame. 188 virtual WebFrame* top() const = 0; 189 190 // Returns the first/last child frame. 191 virtual WebFrame* firstChild() const = 0; 192 virtual WebFrame* lastChild() const = 0; 193 194 // Returns the next/previous sibling frame. 195 virtual WebFrame* nextSibling() const = 0; 196 virtual WebFrame* previousSibling() const = 0; 197 198 // Returns the next/previous frame in "frame traversal order" 199 // optionally wrapping around. 200 virtual WebFrame* traverseNext(bool wrap) const = 0; 201 virtual WebFrame* traversePrevious(bool wrap) const = 0; 202 203 // Returns the child frame identified by the given name. 204 virtual WebFrame* findChildByName(const WebString& name) const = 0; 205 206 // Returns the child frame identified by the given xpath expression. 207 virtual WebFrame* findChildByExpression(const WebString& xpath) const = 0; 208 209 210 // Content ------------------------------------------------------------ 211 212 virtual WebDocument document() const = 0; 213 214 virtual void forms(WebVector<WebFormElement>&) const = 0; 215 216 virtual WebAnimationController* animationController() = 0; 217 218 virtual WebPerformance performance() const = 0; 219 220 221 // Scripting ---------------------------------------------------------- 222 223 // Returns the security origin of the current document. 224 virtual WebSecurityOrigin securityOrigin() const = 0; 225 226 // This grants the currently loaded document access to all security 227 // origins (including file URLs). Use with care. The access is 228 // revoked when a new document is loaded into this frame. 229 virtual void grantUniversalAccess() = 0; 230 231 // Returns a NPObject corresponding to this frame's DOMWindow. 232 virtual NPObject* windowObject() const = 0; 233 234 // Binds a NPObject as a property of this frame's DOMWindow. 235 virtual void bindToWindowObject(const WebString& name, NPObject*) = 0; 236 237 // Executes script in the context of the current page. 238 virtual void executeScript(const WebScriptSource&) = 0; 239 240 // Executes JavaScript in a new world associated with the web frame. 241 // The script gets its own global scope and its own prototypes for 242 // intrinsic JavaScript objects (String, Array, and so-on). It also 243 // gets its own wrappers for all DOM nodes and DOM constructors. 244 // extensionGroup is an embedder-provided specifier that controls which 245 // v8 extensions are loaded into the new context - see 246 // WebKit::registerExtension for the corresponding specifier. 247 virtual void executeScriptInIsolatedWorld( 248 int worldId, const WebScriptSource* sources, unsigned numSources, 249 int extensionGroup) = 0; 250 251 // Logs to the console associated with this frame. 252 virtual void addMessageToConsole(const WebConsoleMessage&) = 0; 253 254 // Calls window.gc() if it is defined. 255 virtual void collectGarbage() = 0; 256 257 #if WEBKIT_USING_V8 258 // Executes script in the context of the current page and returns the value 259 // that the script evaluated to. 260 virtual v8::Handle<v8::Value> executeScriptAndReturnValue( 261 const WebScriptSource&) = 0; 262 263 // Returns the V8 context for this frame, or an empty handle if there 264 // is none. 265 virtual v8::Local<v8::Context> mainWorldScriptContext() const = 0; 266 267 // Creates an instance of file system object. 268 virtual v8::Handle<v8::Value> createFileSystem(WebFileSystem::Type, 269 const WebString& name, 270 const WebString& path) = 0; 271 // Creates an instance of file or directory entry object. 272 virtual v8::Handle<v8::Value> createFileEntry(WebFileSystem::Type, 273 const WebString& fileSystemName, 274 const WebString& fileSystemPath, 275 const WebString& filePath, 276 bool isDirectory) = 0; 277 #endif 278 279 280 // Styling ------------------------------------------------------------- 281 282 // Insert the given text as a STYLE element at the beginning of the 283 // document. |elementId| can be empty, but if specified then it is used 284 // as the id for the newly inserted element (replacing an existing one 285 // with the same id, if any). 286 virtual bool insertStyleText(const WebString& styleText, 287 const WebString& elementId) = 0; 288 289 290 // Navigation ---------------------------------------------------------- 291 292 // Reload the current document. 293 // True |ignoreCache| explicitly bypasses caches. 294 // False |ignoreCache| revalidates any existing cache entries. 295 virtual void reload(bool ignoreCache = false) = 0; 296 297 // Load the given URL. 298 virtual void loadRequest(const WebURLRequest&) = 0; 299 300 // Load the given history state, corresponding to a back/forward 301 // navigation. 302 virtual void loadHistoryItem(const WebHistoryItem&) = 0; 303 304 // Loads the given data with specific mime type and optional text 305 // encoding. For HTML data, baseURL indicates the security origin of 306 // the document and is used to resolve links. If specified, 307 // unreachableURL is reported via WebDataSource::unreachableURL. If 308 // replace is false, then this data will be loaded as a normal 309 // navigation. Otherwise, the current history item will be replaced. 310 virtual void loadData(const WebData& data, 311 const WebString& mimeType, 312 const WebString& textEncoding, 313 const WebURL& baseURL, 314 const WebURL& unreachableURL = WebURL(), 315 bool replace = false) = 0; 316 317 // This method is short-hand for calling LoadData, where mime_type is 318 // "text/html" and text_encoding is "UTF-8". 319 virtual void loadHTMLString(const WebData& html, 320 const WebURL& baseURL, 321 const WebURL& unreachableURL = WebURL(), 322 bool replace = false) = 0; 323 324 // Returns true if the current frame is busy loading content. 325 virtual bool isLoading() const = 0; 326 327 // Stops any pending loads on the frame and its children. 328 virtual void stopLoading() = 0; 329 330 // Returns the data source that is currently loading. May be null. 331 virtual WebDataSource* provisionalDataSource() const = 0; 332 333 // Returns the data source that is currently loaded. 334 virtual WebDataSource* dataSource() const = 0; 335 336 // Returns the previous history item. Check WebHistoryItem::isNull() 337 // before using. 338 virtual WebHistoryItem previousHistoryItem() const = 0; 339 340 // Returns the current history item. Check WebHistoryItem::isNull() 341 // before using. 342 virtual WebHistoryItem currentHistoryItem() const = 0; 343 344 // View-source rendering mode. Set this before loading an URL to cause 345 // it to be rendered in view-source mode. 346 virtual void enableViewSourceMode(bool) = 0; 347 virtual bool isViewSourceModeEnabled() const = 0; 348 349 // Sets the referrer for the given request to be the specified URL or 350 // if that is null, then it sets the referrer to the referrer that the 351 // frame would use for subresources. NOTE: This method also filters 352 // out invalid referrers (e.g., it is invalid to send a HTTPS URL as 353 // the referrer for a HTTP request). 354 virtual void setReferrerForRequest(WebURLRequest&, const WebURL&) = 0; 355 356 // Called to associate the WebURLRequest with this frame. The request 357 // will be modified to inherit parameters that allow it to be loaded. 358 // This method ends up triggering WebFrameClient::willSendRequest. 359 // DEPRECATED: Please use createAssociatedURLLoader instead. 360 virtual void dispatchWillSendRequest(WebURLRequest&) = 0; 361 362 // FIXME: Remove this overload when clients have been changed to pass options. 363 virtual WebURLLoader* createAssociatedURLLoader() = 0; 364 365 // Returns a WebURLLoader that is associated with this frame. The loader 366 // will, for example, be cancelled when WebFrame::stopLoading is called. 367 // FIXME: stopLoading does not yet cancel an associated loader!! 368 virtual WebURLLoader* createAssociatedURLLoader(const WebURLLoaderOptions&) = 0; 369 370 // Called from within WebFrameClient::didReceiveDocumentData to commit 371 // data for the frame that will be used to construct the frame's 372 // document. 373 virtual void commitDocumentData(const char* data, size_t length) = 0; 374 375 // Returns the number of registered unload listeners. 376 virtual unsigned unloadListenerCount() const = 0; 377 378 // Returns true if a user gesture is currently being processed. 379 virtual bool isProcessingUserGesture() const = 0; 380 381 // Returns true if this frame is in the process of opening a new frame 382 // with a suppressed opener. 383 virtual bool willSuppressOpenerInNewFrame() const = 0; 384 385 386 // Editing ------------------------------------------------------------- 387 388 // Replaces the selection with the given text. 389 virtual void replaceSelection(const WebString& text) = 0; 390 391 virtual void insertText(const WebString& text) = 0; 392 393 virtual void setMarkedText(const WebString& text, unsigned location, unsigned length) = 0; 394 virtual void unmarkText() = 0; 395 virtual bool hasMarkedText() const = 0; 396 397 virtual WebRange markedRange() const = 0; 398 399 virtual bool firstRectForCharacterRange(unsigned location, unsigned length, WebRect&) const = 0; 400 401 // Supports commands like Undo, Redo, Cut, Copy, Paste, SelectAll, 402 // Unselect, etc. See EditorCommand.cpp for the full list of supported 403 // commands. 404 virtual bool executeCommand(const WebString&) = 0; 405 virtual bool executeCommand(const WebString&, const WebString& value) = 0; 406 virtual bool isCommandEnabled(const WebString&) const = 0; 407 408 // Spell-checking support. 409 virtual void enableContinuousSpellChecking(bool) = 0; 410 virtual bool isContinuousSpellCheckingEnabled() const = 0; 411 412 413 // Selection ----------------------------------------------------------- 414 415 virtual bool hasSelection() const = 0; 416 417 virtual WebRange selectionRange() const = 0; 418 419 virtual WebString selectionAsText() const = 0; 420 virtual WebString selectionAsMarkup() const = 0; 421 422 // Expands the selection to a word around the caret and returns 423 // true. Does nothing and returns false if there is no caret or 424 // there is ranged selection. 425 virtual bool selectWordAroundCaret() = 0; 426 427 virtual void selectRange(const WebPoint& start, const WebPoint& end) = 0; 428 429 430 // Printing ------------------------------------------------------------ 431 432 // Reformats the WebFrame for printing. pageSize is the page size in 433 // points (a point in 1/72 of an inch). If |constrainToNode| node is 434 // specified, then only the given node is printed (for now only plugins are 435 // supported), instead of the entire frame. printerDPI is the user 436 // selected, DPI for the printer. Returns the number of pages that can be 437 // printed at the given page size. The out param useBrowserOverlays 438 // specifies whether the browser process should use its overlays (header, 439 // footer, margins etc) or whether the renderer controls this. 440 virtual int printBegin(const WebSize& pageSize, 441 const WebNode& constrainToNode = WebNode(), 442 int printerDPI = 72, 443 bool* useBrowserOverlays = 0) = 0; 444 445 // Returns the page shrinking factor calculated by webkit (usually 446 // between 1/1.25 and 1/2). Returns 0 if the page number is invalid or 447 // not in printing mode. 448 virtual float getPrintPageShrink(int page) = 0; 449 450 // Prints one page, and returns the calculated page shrinking factor 451 // (usually between 1/1.25 and 1/2). Returns 0 if the page number is 452 // invalid or not in printing mode. 453 virtual float printPage(int pageToPrint, WebCanvas*) = 0; 454 455 // Reformats the WebFrame for screen display. 456 virtual void printEnd() = 0; 457 458 // CSS3 Paged Media ---------------------------------------------------- 459 460 // Returns true if page box (margin boxes and page borders) is visible. 461 virtual bool isPageBoxVisible(int pageIndex) = 0; 462 463 // Returns the preferred page size and margins in pixels, assuming 96 464 // pixels per inch. pageSize, marginTop, marginRight, marginBottom, 465 // marginLeft must be initialized to the default values that are used if 466 // auto is specified. 467 virtual void pageSizeAndMarginsInPixels(int pageIndex, 468 WebSize& pageSize, 469 int& marginTop, 470 int& marginRight, 471 int& marginBottom, 472 int& marginLeft) = 0; 473 474 // Find-in-page -------------------------------------------------------- 475 476 // Searches a frame for a given string. 477 // 478 // If a match is found, this function will select it (scrolling down to 479 // make it visible if needed) and fill in selectionRect with the 480 // location of where the match was found (in window coordinates). 481 // 482 // If no match is found, this function clears all tickmarks and 483 // highlighting. 484 // 485 // Returns true if the search string was found, false otherwise. 486 virtual bool find(int identifier, 487 const WebString& searchText, 488 const WebFindOptions& options, 489 bool wrapWithinFrame, 490 WebRect* selectionRect) = 0; 491 492 // Notifies the frame that we are no longer interested in searching. 493 // This will abort any asynchronous scoping effort already under way 494 // (see the function scopeStringMatches for details) and erase all 495 // tick-marks and highlighting from the previous search. If 496 // clearSelection is true, it will also make sure the end state for the 497 // find operation does not leave a selection. This can occur when the 498 // user clears the search string but does not close the find box. 499 virtual void stopFinding(bool clearSelection) = 0; 500 501 // Counts how many times a particular string occurs within the frame. 502 // It also retrieves the location of the string and updates a vector in 503 // the frame so that tick-marks and highlighting can be drawn. This 504 // function does its work asynchronously, by running for a certain 505 // time-slice and then scheduling itself (co-operative multitasking) to 506 // be invoked later (repeating the process until all matches have been 507 // found). This allows multiple frames to be searched at the same time 508 // and provides a way to cancel at any time (see 509 // cancelPendingScopingEffort). The parameter searchText specifies 510 // what to look for and |reset| signals whether this is a brand new 511 // request or a continuation of the last scoping effort. 512 virtual void scopeStringMatches(int identifier, 513 const WebString& searchText, 514 const WebFindOptions& options, 515 bool reset) = 0; 516 517 // Cancels any outstanding requests for scoping string matches on a frame. 518 virtual void cancelPendingScopingEffort() = 0; 519 520 // This function is called on the main frame during the scoping effort 521 // to keep a running tally of the accumulated total match-count for all 522 // frames. After updating the count it will notify the WebViewClient 523 // about the new count. 524 virtual void increaseMatchCount(int count, int identifier) = 0; 525 526 // This function is called on the main frame to reset the total number 527 // of matches found during the scoping effort. 528 virtual void resetMatchCount() = 0; 529 530 531 // Password autocompletion --------------------------------------------- 532 533 // Registers a listener for the specified user name input element. The 534 // listener will receive notifications for blur and when autocomplete 535 // should be triggered. 536 // An element can have only one listener. If a listener already exists, 537 // this method returns false and does not add the new one. 538 // Either way, the WebFrame becomes the owner of the passed listener. 539 virtual bool registerPasswordListener( 540 WebInputElement, 541 WebPasswordAutocompleteListener*) = 0; 542 543 // Dispatches an Autocompletion notification to registered listener if one 544 // exists that is registered against the WebInputElement specified. 545 virtual void notifiyPasswordListenerOfAutocomplete( 546 const WebInputElement&) = 0; 547 548 549 // Utility ------------------------------------------------------------- 550 551 // Returns the contents of this frame as a string. If the text is 552 // longer than maxChars, it will be clipped to that length. WARNING: 553 // This function may be slow depending on the number of characters 554 // retrieved and page complexity. For a typically sized page, expect 555 // it to take on the order of milliseconds. 556 // 557 // If there is room, subframe text will be recursively appended. Each 558 // frame will be separated by an empty line. 559 virtual WebString contentAsText(size_t maxChars) const = 0; 560 561 // Returns HTML text for the contents of this frame. This is generated 562 // from the DOM. 563 virtual WebString contentAsMarkup() const = 0; 564 565 // Returns a text representation of the render tree. This method is used 566 // to support layout tests. 567 virtual WebString renderTreeAsText(bool showDebugInfo = false) const = 0; 568 569 // Returns the counter value for the specified element. This method is 570 // used to support layout tests. 571 virtual WebString counterValueForElementById(const WebString& id) const = 0; 572 573 // Calls markerTextForListItem() defined in WebCore/rendering/RenderTreeAsText.h. 574 virtual WebString markerTextForListItem(const WebElement&) const = 0; 575 576 // Returns the number of page where the specified element will be put. 577 // This method is used to support layout tests. 578 virtual int pageNumberForElementById(const WebString& id, 579 float pageWidthInPixels, 580 float pageHeightInPixels) const = 0; 581 582 // Returns the bounds rect for current selection. If selection is performed 583 // on transformed text, the rect will still bound the selection but will 584 // not be transformed itself. If no selection is present, the rect will be 585 // empty ((0,0), (0,0)). 586 virtual WebRect selectionBoundsRect() const = 0; 587 588 // Only for testing purpose: 589 // Returns true if selection.anchorNode has a marker on range from |from| with |length|. 590 virtual bool selectionStartHasSpellingMarkerFor(int from, int length) const = 0; 591 592 // Pauses and samples an SVG animation. Returns false if there's no svg 593 // animation to pause. This is only for testing. 594 virtual bool pauseSVGAnimation(const WebString& animationId, 595 double time, 596 const WebString& elementId) = 0; 597 598 // Dumps the layer tree, used by the accelerated compositor, in 599 // text form. This is used only by layout tests. 600 virtual WebString layerTreeAsText(bool showDebugInfo = false) const = 0; 601 602 protected: ~WebFrame()603 ~WebFrame() { } 604 }; 605 606 } // namespace WebKit 607 608 #endif 609