Home | History | Annotate | Download | only in web
      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 WebFrameImpl_h
     32 #define WebFrameImpl_h
     33 
     34 #include "WebFrame.h"
     35 
     36 #include "FrameLoaderClientImpl.h"
     37 #include "core/page/Frame.h"
     38 #include "core/page/FrameDestructionObserver.h"
     39 #include "core/platform/graphics/FloatRect.h"
     40 #include "public/platform/WebFileSystemType.h"
     41 #include "wtf/Compiler.h"
     42 #include "wtf/OwnPtr.h"
     43 #include "wtf/RefCounted.h"
     44 #include "wtf/text/WTFString.h"
     45 
     46 namespace WebCore {
     47 class GraphicsContext;
     48 class HTMLInputElement;
     49 class HistoryItem;
     50 class IntSize;
     51 class KURL;
     52 class Node;
     53 class Range;
     54 class SubstituteData;
     55 struct FrameLoadRequest;
     56 struct WindowFeatures;
     57 }
     58 
     59 namespace WebKit {
     60 class ChromePrintContext;
     61 class WebDataSourceImpl;
     62 class WebInputElement;
     63 class WebFrameClient;
     64 class WebPerformance;
     65 class WebPluginContainerImpl;
     66 class WebView;
     67 class WebViewImpl;
     68 struct WebPrintParams;
     69 
     70 template <typename T> class WebVector;
     71 
     72 // Implementation of WebFrame, note that this is a reference counted object.
     73 class WebFrameImpl
     74     : public WebFrame
     75     , public RefCounted<WebFrameImpl>
     76     , public WebCore::FrameDestructionObserver {
     77 public:
     78     // WebFrame methods:
     79     virtual WebString uniqueName() const;
     80     virtual WebString assignedName() const;
     81     virtual void setName(const WebString&);
     82     virtual long long identifier() const;
     83     virtual WebVector<WebIconURL> iconURLs(int iconTypesMask) const;
     84     virtual WebSize scrollOffset() const;
     85     virtual void setScrollOffset(const WebSize&);
     86     virtual WebSize minimumScrollOffset() const;
     87     virtual WebSize maximumScrollOffset() const;
     88     virtual WebSize contentsSize() const;
     89     virtual bool hasVisibleContent() const;
     90     virtual WebRect visibleContentRect() const;
     91     virtual bool hasHorizontalScrollbar() const;
     92     virtual bool hasVerticalScrollbar() const;
     93     virtual WebView* view() const;
     94     virtual WebFrame* opener() const;
     95     virtual void setOpener(const WebFrame*);
     96     virtual WebFrame* parent() const;
     97     virtual WebFrame* top() const;
     98     virtual WebFrame* firstChild() const;
     99     virtual WebFrame* lastChild() const;
    100     virtual WebFrame* nextSibling() const;
    101     virtual WebFrame* previousSibling() const;
    102     virtual WebFrame* traverseNext(bool wrap) const;
    103     virtual WebFrame* traversePrevious(bool wrap) const;
    104     virtual WebFrame* findChildByName(const WebString&) const;
    105     virtual WebFrame* findChildByExpression(const WebString&) const;
    106     virtual WebDocument document() const;
    107     virtual WebPerformance performance() const;
    108     virtual NPObject* windowObject() const;
    109     virtual void bindToWindowObject(const WebString& name, NPObject*);
    110     virtual void bindToWindowObject(const WebString& name, NPObject*, void*);
    111     virtual void executeScript(const WebScriptSource&);
    112     virtual void executeScriptInIsolatedWorld(
    113         int worldID, const WebScriptSource* sources, unsigned numSources,
    114         int extensionGroup);
    115     virtual void setIsolatedWorldSecurityOrigin(int worldID, const WebSecurityOrigin&);
    116     virtual void setIsolatedWorldContentSecurityPolicy(int worldID, const WebString&);
    117     virtual void addMessageToConsole(const WebConsoleMessage&);
    118     virtual void collectGarbage();
    119     virtual bool checkIfRunInsecureContent(const WebURL&) const;
    120     virtual v8::Handle<v8::Value> executeScriptAndReturnValue(
    121         const WebScriptSource&);
    122     virtual void executeScriptInIsolatedWorld(
    123         int worldID, const WebScriptSource* sourcesIn, unsigned numSources,
    124         int extensionGroup, WebVector<v8::Local<v8::Value> >* results);
    125     virtual v8::Handle<v8::Value> callFunctionEvenIfScriptDisabled(
    126         v8::Handle<v8::Function>,
    127         v8::Handle<v8::Object>,
    128         int argc,
    129         v8::Handle<v8::Value> argv[]);
    130     virtual v8::Local<v8::Context> mainWorldScriptContext() const;
    131     virtual v8::Handle<v8::Value> createFileSystem(WebFileSystemType,
    132         const WebString& name,
    133         const WebString& path);
    134     virtual v8::Handle<v8::Value> createSerializableFileSystem(WebFileSystemType,
    135         const WebString& name,
    136         const WebString& path);
    137     virtual v8::Handle<v8::Value> createFileEntry(WebFileSystemType,
    138         const WebString& fileSystemName,
    139         const WebString& fileSystemPath,
    140         const WebString& filePath,
    141         bool isDirectory);
    142     virtual void reload(bool ignoreCache);
    143     virtual void reloadWithOverrideURL(const WebURL& overrideUrl, bool ignoreCache);
    144     virtual void loadRequest(const WebURLRequest&);
    145     virtual void loadHistoryItem(const WebHistoryItem&);
    146     virtual void loadData(
    147         const WebData&, const WebString& mimeType, const WebString& textEncoding,
    148         const WebURL& baseURL, const WebURL& unreachableURL, bool replace);
    149     virtual void loadHTMLString(
    150         const WebData& html, const WebURL& baseURL, const WebURL& unreachableURL,
    151         bool replace);
    152     virtual bool isLoading() const;
    153     virtual void stopLoading();
    154     virtual WebDataSource* provisionalDataSource() const;
    155     virtual WebDataSource* dataSource() const;
    156     virtual WebHistoryItem previousHistoryItem() const;
    157     virtual WebHistoryItem currentHistoryItem() const;
    158     virtual void enableViewSourceMode(bool enable);
    159     virtual bool isViewSourceModeEnabled() const;
    160     virtual void setReferrerForRequest(WebURLRequest&, const WebURL& referrer);
    161     virtual void dispatchWillSendRequest(WebURLRequest&);
    162     virtual WebURLLoader* createAssociatedURLLoader(const WebURLLoaderOptions&);
    163     virtual unsigned unloadListenerCount() const;
    164     virtual bool willSuppressOpenerInNewFrame() const;
    165     virtual void replaceSelection(const WebString&);
    166     virtual void insertText(const WebString&);
    167     virtual void setMarkedText(const WebString&, unsigned location, unsigned length);
    168     virtual void unmarkText();
    169     virtual bool hasMarkedText() const;
    170     virtual WebRange markedRange() const;
    171     virtual bool firstRectForCharacterRange(unsigned location, unsigned length, WebRect&) const;
    172     virtual size_t characterIndexForPoint(const WebPoint&) const;
    173     virtual bool executeCommand(const WebString&, const WebNode& = WebNode());
    174     virtual bool executeCommand(const WebString&, const WebString& value, const WebNode& = WebNode());
    175     virtual bool isCommandEnabled(const WebString&) const;
    176     virtual void enableContinuousSpellChecking(bool);
    177     virtual bool isContinuousSpellCheckingEnabled() const;
    178     virtual void requestTextChecking(const WebElement&);
    179     virtual void replaceMisspelledRange(const WebString&);
    180     virtual void removeSpellingMarkers();
    181     virtual bool hasSelection() const;
    182     virtual WebRange selectionRange() const;
    183     virtual WebString selectionAsText() const;
    184     virtual WebString selectionAsMarkup() const;
    185     virtual bool selectWordAroundCaret();
    186     virtual void selectRange(const WebPoint& base, const WebPoint& extent);
    187     virtual void selectRange(const WebRange&);
    188     virtual void moveCaretSelectionTowardsWindowPoint(const WebPoint&);
    189     virtual void moveRangeSelection(const WebPoint& base, const WebPoint& extent);
    190     virtual void moveCaretSelection(const WebPoint&);
    191     virtual int printBegin(const WebPrintParams&,
    192                            const WebNode& constrainToNode,
    193                            bool* useBrowserOverlays);
    194     virtual float printPage(int pageToPrint, WebCanvas*);
    195     virtual float getPrintPageShrink(int page);
    196     virtual void printEnd();
    197     virtual bool isPrintScalingDisabledForPlugin(const WebNode&);
    198     virtual bool hasCustomPageSizeStyle(int pageIndex);
    199     virtual bool isPageBoxVisible(int pageIndex);
    200     virtual void pageSizeAndMarginsInPixels(int pageIndex,
    201                                             WebSize& pageSize,
    202                                             int& marginTop,
    203                                             int& marginRight,
    204                                             int& marginBottom,
    205                                             int& marginLeft);
    206     virtual WebString pageProperty(const WebString& propertyName, int pageIndex);
    207     virtual void printPagesWithBoundaries(WebCanvas*, const WebSize&);
    208     virtual bool find(
    209         int identifier, const WebString& searchText, const WebFindOptions&,
    210         bool wrapWithinFrame, WebRect* selectionRect);
    211     virtual void stopFinding(bool clearSelection);
    212     virtual void scopeStringMatches(
    213         int identifier, const WebString& searchText, const WebFindOptions&,
    214         bool reset);
    215     virtual void cancelPendingScopingEffort();
    216     virtual void increaseMatchCount(int count, int identifier);
    217     virtual void resetMatchCount();
    218     virtual int findMatchMarkersVersion() const;
    219     virtual WebFloatRect activeFindMatchRect();
    220     virtual void findMatchRects(WebVector<WebFloatRect>&);
    221     virtual int selectNearestFindMatch(const WebFloatPoint&, WebRect* selectionRect);
    222 
    223     virtual void sendOrientationChangeEvent(int orientation);
    224 
    225     virtual void dispatchMessageEventWithOriginCheck(
    226         const WebSecurityOrigin& intendedTargetOrigin,
    227         const WebDOMEvent&);
    228 
    229     virtual WebString contentAsText(size_t maxChars) const;
    230     virtual WebString contentAsMarkup() const;
    231     virtual WebString renderTreeAsText(RenderAsTextControls toShow = RenderAsTextNormal) const;
    232     virtual WebString markerTextForListItem(const WebElement&) const;
    233     virtual WebRect selectionBoundsRect() const;
    234 
    235     virtual bool selectionStartHasSpellingMarkerFor(int from, int length) const;
    236     virtual WebString layerTreeAsText(bool showDebugInfo = false) const;
    237 
    238     // WebCore::FrameDestructionObserver methods.
    239     virtual void willDetachPage();
    240 
    241     static PassRefPtr<WebFrameImpl> create(WebFrameClient* client);
    242     virtual ~WebFrameImpl();
    243 
    244     // Called by the WebViewImpl to initialize the main frame for the page.
    245     void initializeAsMainFrame(WebCore::Page*);
    246 
    247     PassRefPtr<WebCore::Frame> createChildFrame(
    248         const WebCore::FrameLoadRequest&, WebCore::HTMLFrameOwnerElement*);
    249 
    250     void didChangeContentsSize(const WebCore::IntSize&);
    251 
    252     void createFrameView();
    253 
    254     static WebFrameImpl* fromFrame(WebCore::Frame* frame);
    255     static WebFrameImpl* fromFrameOwnerElement(WebCore::Element* element);
    256 
    257     // If the frame hosts a PluginDocument, this method returns the WebPluginContainerImpl
    258     // that hosts the plugin.
    259     static WebPluginContainerImpl* pluginContainerFromFrame(WebCore::Frame*);
    260 
    261     // If the frame hosts a PluginDocument, this method returns the WebPluginContainerImpl
    262     // that hosts the plugin. If the provided node is a plugin, then it runs its
    263     // WebPluginContainerImpl.
    264     static WebPluginContainerImpl* pluginContainerFromNode(WebCore::Frame*, const WebNode&);
    265 
    266     WebViewImpl* viewImpl() const;
    267 
    268     WebCore::FrameView* frameView() const { return frame() ? frame()->view() : 0; }
    269 
    270     // Getters for the impls corresponding to Get(Provisional)DataSource. They
    271     // may return 0 if there is no corresponding data source.
    272     WebDataSourceImpl* dataSourceImpl() const;
    273     WebDataSourceImpl* provisionalDataSourceImpl() const;
    274 
    275     // Returns which frame has an active match. This function should only be
    276     // called on the main frame, as it is the only frame keeping track. Returned
    277     // value can be 0 if no frame has an active match.
    278     WebFrameImpl* activeMatchFrame() const { return m_currentActiveMatchFrame; }
    279 
    280     // Returns the active match in the current frame. Could be a null range if
    281     // the local frame has no active match.
    282     WebCore::Range* activeMatch() const { return m_activeMatch.get(); }
    283 
    284     // When a Find operation ends, we want to set the selection to what was active
    285     // and set focus to the first focusable node we find (starting with the first
    286     // node in the matched range and going up the inheritance chain). If we find
    287     // nothing to focus we focus the first focusable node in the range. This
    288     // allows us to set focus to a link (when we find text inside a link), which
    289     // allows us to navigate by pressing Enter after closing the Find box.
    290     void setFindEndstateFocusAndSelection();
    291 
    292     void didFail(const WebCore::ResourceError&, bool wasProvisional);
    293 
    294     // Sets whether the WebFrameImpl allows its document to be scrolled.
    295     // If the parameter is true, allow the document to be scrolled.
    296     // Otherwise, disallow scrolling.
    297     void setCanHaveScrollbars(bool);
    298 
    299     WebFrameClient* client() const { return m_client; }
    300     void setClient(WebFrameClient* client) { m_client = client; }
    301 
    302     static void selectWordAroundPosition(WebCore::Frame*, WebCore::VisiblePosition);
    303 
    304 private:
    305     class DeferredScopeStringMatches;
    306     friend class DeferredScopeStringMatches;
    307     friend class FrameLoaderClientImpl;
    308 
    309     struct FindMatch {
    310         RefPtr<WebCore::Range> m_range;
    311 
    312         // 1-based index within this frame.
    313         int m_ordinal;
    314 
    315         // In find-in-page coordinates.
    316         // Lazily calculated by updateFindMatchRects.
    317         WebCore::FloatRect m_rect;
    318 
    319         FindMatch(PassRefPtr<WebCore::Range>, int ordinal);
    320     };
    321 
    322     // A bit mask specifying area of the frame to invalidate.
    323     enum AreaToInvalidate {
    324       InvalidateNothing,
    325       InvalidateContentArea,
    326       InvalidateScrollbar,   // Vertical scrollbar only.
    327       InvalidateAll          // Both content area and the scrollbar.
    328     };
    329 
    330     explicit WebFrameImpl(WebFrameClient*);
    331 
    332     // Sets the local WebCore frame and registers destruction observers.
    333     void setWebCoreFrame(WebCore::Frame*);
    334 
    335     // Notifies the delegate about a new selection rect.
    336     void reportFindInPageSelection(
    337         const WebRect& selectionRect, int activeMatchOrdinal, int identifier);
    338 
    339     // Clear the find-in-page matches cache forcing rects to be fully
    340     // calculated again next time updateFindMatchRects is called.
    341     void clearFindMatchesCache();
    342 
    343     // Check if the activeMatchFrame still exists in the frame tree.
    344     bool isActiveMatchFrameValid() const;
    345 
    346     // Return the index in the find-in-page cache of the match closest to the
    347     // provided point in find-in-page coordinates, or -1 in case of error.
    348     // The squared distance to the closest match is returned in the distanceSquared parameter.
    349     int nearestFindMatch(const WebCore::FloatPoint&, float& distanceSquared);
    350 
    351     // Select a find-in-page match marker in the current frame using a cache
    352     // match index returned by nearestFindMatch. Returns the ordinal of the new
    353     // selected match or -1 in case of error. Also provides the bounding box of
    354     // the marker in window coordinates if selectionRect is not null.
    355     int selectFindMatch(unsigned index, WebRect* selectionRect);
    356 
    357     // Compute and cache the rects for FindMatches if required.
    358     // Rects are automatically invalidated in case of content size changes,
    359     // propagating the invalidation to child frames.
    360     void updateFindMatchRects();
    361 
    362     // Append the find-in-page match rects of the current frame to the provided vector.
    363     void appendFindMatchRects(Vector<WebFloatRect>& frameRects);
    364 
    365     // Invalidates a certain area within the frame.
    366     void invalidateArea(AreaToInvalidate);
    367 
    368     // Add a WebKit TextMatch-highlight marker to nodes in a range.
    369     void addMarker(WebCore::Range*, bool activeMatch);
    370 
    371     // Sets the markers within a range as active or inactive.
    372     void setMarkerActive(WebCore::Range*, bool active);
    373 
    374     // Returns the ordinal of the first match in the frame specified. This
    375     // function enumerates the frames, starting with the main frame and up to (but
    376     // not including) the frame passed in as a parameter and counts how many
    377     // matches have been found.
    378     int ordinalOfFirstMatchForFrame(WebFrameImpl*) const;
    379 
    380     // Determines whether the scoping effort is required for a particular frame.
    381     // It is not necessary if the frame is invisible, for example, or if this
    382     // is a repeat search that already returned nothing last time the same prefix
    383     // was searched.
    384     bool shouldScopeMatches(const WTF::String& searchText);
    385 
    386     // Removes the current frame from the global scoping effort and triggers any
    387     // updates if appropriate. This method does not mark the scoping operation
    388     // as finished.
    389     void flushCurrentScopingEffort(int identifier);
    390 
    391     // Finishes the current scoping effort and triggers any updates if appropriate.
    392     void finishCurrentScopingEffort(int identifier);
    393 
    394     // Queue up a deferred call to scopeStringMatches.
    395     void scopeStringMatchesSoon(
    396         int identifier, const WebString& searchText, const WebFindOptions&,
    397         bool reset);
    398 
    399     // Called by a DeferredScopeStringMatches instance.
    400     void callScopeStringMatches(
    401         DeferredScopeStringMatches*, int identifier, const WebString& searchText,
    402         const WebFindOptions&, bool reset);
    403 
    404     // Determines whether to invalidate the content area and scrollbar.
    405     void invalidateIfNecessary();
    406 
    407     void loadJavaScriptURL(const WebCore::KURL&);
    408 
    409     // Returns a hit-tested VisiblePosition for the given point
    410     WebCore::VisiblePosition visiblePositionForWindowPoint(const WebPoint&);
    411 
    412     FrameLoaderClientImpl m_frameLoaderClient;
    413 
    414     WebFrameClient* m_client;
    415 
    416     // A way for the main frame to keep track of which frame has an active
    417     // match. Should be 0 for all other frames.
    418     WebFrameImpl* m_currentActiveMatchFrame;
    419 
    420     // The range of the active match for the current frame.
    421     RefPtr<WebCore::Range> m_activeMatch;
    422 
    423     // The index of the active match for the current frame.
    424     int m_activeMatchIndexInCurrentFrame;
    425 
    426     // This flag is used by the scoping effort to determine if we need to figure
    427     // out which rectangle is the active match. Once we find the active
    428     // rectangle we clear this flag.
    429     bool m_locatingActiveRect;
    430 
    431     // The scoping effort can time out and we need to keep track of where we
    432     // ended our last search so we can continue from where we left of.
    433     RefPtr<WebCore::Range> m_resumeScopingFromRange;
    434 
    435     // Keeps track of the last string this frame searched for. This is used for
    436     // short-circuiting searches in the following scenarios: When a frame has
    437     // been searched and returned 0 results, we don't need to search that frame
    438     // again if the user is just adding to the search (making it more specific).
    439     WTF::String m_lastSearchString;
    440 
    441     // Keeps track of how many matches this frame has found so far, so that we
    442     // don't loose count between scoping efforts, and is also used (in conjunction
    443     // with m_lastSearchString) to figure out if we need to search the frame again.
    444     int m_lastMatchCount;
    445 
    446     // This variable keeps a cumulative total of matches found so far for ALL the
    447     // frames on the page, and is only incremented by calling IncreaseMatchCount
    448     // (on the main frame only). It should be -1 for all other frames.
    449     int m_totalMatchCount;
    450 
    451     // This variable keeps a cumulative total of how many frames are currently
    452     // scoping, and is incremented/decremented on the main frame only.
    453     // It should be -1 for all other frames.
    454     int m_framesScopingCount;
    455 
    456     // Identifier of the latest find-in-page request. Required to be stored in
    457     // the frame in order to reply if required in case the frame is detached.
    458     int m_findRequestIdentifier;
    459 
    460     // Keeps track of whether there is an scoping effort ongoing in the frame.
    461     bool m_scopingInProgress;
    462 
    463     // Keeps track of whether the last find request completed its scoping effort
    464     // without finding any matches in this frame.
    465     bool m_lastFindRequestCompletedWithNoMatches;
    466 
    467     // Keeps track of when the scoping effort should next invalidate the scrollbar
    468     // and the frame area.
    469     int m_nextInvalidateAfter;
    470 
    471     // A list of all of the pending calls to scopeStringMatches.
    472     Vector<DeferredScopeStringMatches*> m_deferredScopingWork;
    473 
    474     // Version number incremented on the main frame only whenever the document
    475     // find-in-page match markers change. It should be 0 for all other frames.
    476     int m_findMatchMarkersVersion;
    477 
    478     // Local cache of the find match markers currently displayed for this frame.
    479     Vector<FindMatch> m_findMatchesCache;
    480 
    481     // Determines if the rects in the find-in-page matches cache of this frame
    482     // are invalid and should be recomputed.
    483     bool m_findMatchRectsAreValid;
    484 
    485     // Contents size when find-in-page match rects were last computed for this
    486     // frame's cache.
    487     WebCore::IntSize m_contentsSizeForCurrentFindMatchRects;
    488 
    489     // Valid between calls to BeginPrint() and EndPrint(). Containts the print
    490     // information. Is used by PrintPage().
    491     OwnPtr<ChromePrintContext> m_printContext;
    492 
    493     // The identifier of this frame.
    494     long long m_identifier;
    495 
    496     // Ensure we don't overwrite valid history data during same document loads
    497     // from HistoryItems
    498     bool m_inSameDocumentHistoryLoad;
    499 };
    500 
    501 } // namespace WebKit
    502 
    503 #endif
    504