• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // FIXME: remove this relative path once consumers from glue are removed.
35 #include "../public/WebFrame.h"
36 #include "Frame.h"
37 #include "FrameLoaderClientImpl.h"
38 #include "PlatformString.h"
39 #include <wtf/OwnPtr.h>
40 #include <wtf/RefCounted.h>
41 
42 #include "WebAnimationControllerImpl.h"
43 
44 namespace WebCore {
45 class HistoryItem;
46 class KURL;
47 class Node;
48 class Range;
49 class SubstituteData;
50 struct WindowFeatures;
51 }
52 
53 namespace WebKit {
54 class ChromePrintContext;
55 class WebDataSourceImpl;
56 class WebInputElement;
57 class WebFrameClient;
58 class WebPasswordAutocompleteListener;
59 class WebView;
60 class WebViewImpl;
61 
62 // Implementation of WebFrame, note that this is a reference counted object.
63 class WebFrameImpl : public WebFrame, public RefCounted<WebFrameImpl> {
64 public:
65     // WebFrame methods:
66     virtual WebString name() const;
67     virtual void clearName();
68     virtual WebURL url() const;
69     virtual WebURL favIconURL() const;
70     virtual WebURL openSearchDescriptionURL() const;
71     virtual WebString encoding() const;
72     virtual WebSize scrollOffset() const;
73     virtual WebSize contentsSize() const;
74     virtual int contentsPreferredWidth() const;
75     virtual int documentElementScrollHeight() const;
76     virtual bool hasVisibleContent() const;
77     virtual WebView* view() const;
78     virtual WebFrame* opener() const;
79     virtual WebFrame* parent() const;
80     virtual WebFrame* top() const;
81     virtual WebFrame* firstChild() const;
82     virtual WebFrame* lastChild() const;
83     virtual WebFrame* nextSibling() const;
84     virtual WebFrame* previousSibling() const;
85     virtual WebFrame* traverseNext(bool wrap) const;
86     virtual WebFrame* traversePrevious(bool wrap) const;
87     virtual WebFrame* findChildByName(const WebString&) const;
88     virtual WebFrame* findChildByExpression(const WebString&) const;
89     virtual WebDocument document() const;
90     virtual void forms(WebVector<WebFormElement>&) const;
91     virtual WebAnimationController* animationController();
92     virtual WebSecurityOrigin securityOrigin() const;
93     virtual void grantUniversalAccess();
94     virtual NPObject* windowObject() const;
95     virtual void bindToWindowObject(const WebString& name, NPObject*);
96     virtual void executeScript(const WebScriptSource&);
97     virtual void executeScriptInIsolatedWorld(
98         int worldId, const WebScriptSource* sources, unsigned numSources,
99         int extensionGroup);
100     virtual void addMessageToConsole(const WebConsoleMessage&);
101     virtual void collectGarbage();
102 #if WEBKIT_USING_V8
103     virtual v8::Local<v8::Context> mainWorldScriptContext() const;
104 #endif
105     virtual bool insertStyleText(const WebString& css, const WebString& id);
106     virtual void reload();
107     virtual void loadRequest(const WebURLRequest&);
108     virtual void loadHistoryItem(const WebHistoryItem&);
109     virtual void loadData(
110         const WebData&, const WebString& mimeType, const WebString& textEncoding,
111         const WebURL& baseURL, const WebURL& unreachableURL, bool replace);
112     virtual void loadHTMLString(
113         const WebData& html, const WebURL& baseURL, const WebURL& unreachableURL,
114         bool replace);
115     virtual bool isLoading() const;
116     virtual void stopLoading();
117     virtual WebDataSource* provisionalDataSource() const;
118     virtual WebDataSource* dataSource() const;
119     virtual WebHistoryItem previousHistoryItem() const;
120     virtual WebHistoryItem currentHistoryItem() const;
121     virtual void enableViewSourceMode(bool enable);
122     virtual bool isViewSourceModeEnabled() const;
123     virtual void setReferrerForRequest(WebURLRequest&, const WebURL& referrer);
124     virtual void dispatchWillSendRequest(WebURLRequest&);
125     virtual void commitDocumentData(const char* data, size_t length);
126     virtual unsigned unloadListenerCount() const;
127     virtual bool isProcessingUserGesture() const;
128     virtual bool willSuppressOpenerInNewFrame() const;
129     virtual void replaceSelection(const WebString&);
130     virtual void insertText(const WebString&);
131     virtual void setMarkedText(const WebString&, unsigned location, unsigned length);
132     virtual void unmarkText();
133     virtual bool hasMarkedText() const;
134     virtual WebRange markedRange() const;
135     virtual bool executeCommand(const WebString&);
136     virtual bool executeCommand(const WebString&, const WebString& value);
137     virtual bool isCommandEnabled(const WebString&) const;
138     virtual void enableContinuousSpellChecking(bool);
139     virtual bool isContinuousSpellCheckingEnabled() const;
140     virtual bool hasSelection() const;
141     virtual WebRange selectionRange() const;
142     virtual WebString selectionAsText() const;
143     virtual WebString selectionAsMarkup() const;
144     virtual bool selectWordAroundCaret();
145     virtual int printBegin(const WebSize& pageSize);
146     virtual float printPage(int pageToPrint, WebCanvas*);
147     virtual float getPrintPageShrink(int page);
148     virtual void printEnd();
149     virtual bool find(
150         int identifier, const WebString& searchText, const WebFindOptions&,
151         bool wrapWithinFrame, WebRect* selectionRect);
152     virtual void stopFinding(bool clearSelection);
153     virtual void scopeStringMatches(
154         int identifier, const WebString& searchText, const WebFindOptions&,
155         bool reset);
156     virtual void cancelPendingScopingEffort();
157     virtual void increaseMatchCount(int count, int identifier);
158     virtual void resetMatchCount();
159     virtual void registerPasswordListener(
160         WebInputElement, WebPasswordAutocompleteListener*);
161 
162     virtual WebURL completeURL(const WebString& url) const;
163     virtual WebString contentAsText(size_t maxChars) const;
164     virtual WebString contentAsMarkup() const;
165     virtual WebString renderTreeAsText() const;
166     virtual WebString counterValueForElementById(const WebString& id) const;
167     virtual int pageNumberForElementById(const WebString& id,
168                                          float pageWidthInPixels,
169                                          float pageHeightInPixels) const;
170 
171     static PassRefPtr<WebFrameImpl> create(WebFrameClient* client);
172     ~WebFrameImpl();
173 
174     // Called by the WebViewImpl to initialize its main frame:
175     void initializeAsMainFrame(WebViewImpl*);
176 
177     PassRefPtr<WebCore::Frame> createChildFrame(
178         const WebCore::FrameLoadRequest&, WebCore::HTMLFrameOwnerElement*);
179 
180     void layout();
181     void paint(WebCanvas*, const WebRect&);
182     void createFrameView();
183 
184     static WebFrameImpl* fromFrame(WebCore::Frame* frame);
185     static WebFrameImpl* fromFrameOwnerElement(WebCore::Element* element);
186 
187     WebViewImpl* viewImpl() const;
188 
frame()189     WebCore::Frame* frame() const { return m_frame; }
frameView()190     WebCore::FrameView* frameView() const { return m_frame ? m_frame->view() : 0; }
191 
192     // Getters for the impls corresponding to Get(Provisional)DataSource. They
193     // may return 0 if there is no corresponding data source.
194     WebDataSourceImpl* dataSourceImpl() const;
195     WebDataSourceImpl* provisionalDataSourceImpl() const;
196 
197     // Returns which frame has an active match. This function should only be
198     // called on the main frame, as it is the only frame keeping track. Returned
199     // value can be 0 if no frame has an active match.
activeMatchFrame()200     const WebFrameImpl* activeMatchFrame() const { return m_activeMatchFrame; }
201 
202     // When a Find operation ends, we want to set the selection to what was active
203     // and set focus to the first focusable node we find (starting with the first
204     // node in the matched range and going up the inheritance chain). If we find
205     // nothing to focus we focus the first focusable node in the range. This
206     // allows us to set focus to a link (when we find text inside a link), which
207     // allows us to navigate by pressing Enter after closing the Find box.
208     void setFindEndstateFocusAndSelection();
209 
210     void didFail(const WebCore::ResourceError&, bool wasProvisional);
211 
212     // Sets whether the WebFrameImpl allows its document to be scrolled.
213     // If the parameter is true, allow the document to be scrolled.
214     // Otherwise, disallow scrolling.
215     void setAllowsScrolling(bool);
216 
217     // Returns the password autocomplete listener associated with the passed
218     // user name input element, or 0 if none available.
219     // Note that the returned listener is owner by the WebFrameImpl and should not
220     // be kept around as it is deleted when the page goes away.
221     WebPasswordAutocompleteListener* getPasswordListener(WebCore::HTMLInputElement*);
222 
client()223     WebFrameClient* client() const { return m_client; }
dropClient()224     void dropClient() { m_client = 0; }
225 
226     static void selectWordAroundPosition(WebCore::Frame*, WebCore::VisiblePosition);
227 
228 private:
229     class DeferredScopeStringMatches;
230     friend class DeferredScopeStringMatches;
231     friend class FrameLoaderClientImpl;
232 
233     // A bit mask specifying area of the frame to invalidate.
234     enum AreaToInvalidate {
235       InvalidateNothing,
236       InvalidateContentArea,
237       InvalidateScrollbar,   // Vertical scrollbar only.
238       InvalidateAll          // Both content area and the scrollbar.
239     };
240 
241     WebFrameImpl(WebFrameClient*);
242 
243     // Informs the WebFrame that the Frame is being closed, called by the
244     // WebFrameLoaderClient
245     void closing();
246 
247     // Notifies the delegate about a new selection rect.
248     void reportFindInPageSelection(
249         const WebRect& selectionRect, int activeMatchOrdinal, int identifier);
250 
251     // Invalidates a certain area within the frame.
252     void invalidateArea(AreaToInvalidate);
253 
254     // Add a WebKit TextMatch-highlight marker to nodes in a range.
255     void addMarker(WebCore::Range*, bool activeMatch);
256 
257     // Sets the markers within a range as active or inactive.
258     void setMarkerActive(WebCore::Range*, bool active);
259 
260     // Returns the ordinal of the first match in the frame specified. This
261     // function enumerates the frames, starting with the main frame and up to (but
262     // not including) the frame passed in as a parameter and counts how many
263     // matches have been found.
264     int ordinalOfFirstMatchForFrame(WebFrameImpl*) const;
265 
266     // Determines whether the scoping effort is required for a particular frame.
267     // It is not necessary if the frame is invisible, for example, or if this
268     // is a repeat search that already returned nothing last time the same prefix
269     // was searched.
270     bool shouldScopeMatches(const WebCore::String& searchText);
271 
272     // Queue up a deferred call to scopeStringMatches.
273     void scopeStringMatchesSoon(
274         int identifier, const WebString& searchText, const WebFindOptions&,
275         bool reset);
276 
277     // Called by a DeferredScopeStringMatches instance.
278     void callScopeStringMatches(
279         DeferredScopeStringMatches*, int identifier, const WebString& searchText,
280         const WebFindOptions&, bool reset);
281 
282     // Determines whether to invalidate the content area and scrollbar.
283     void invalidateIfNecessary();
284 
285     // Clears the map of password listeners.
286     void clearPasswordListeners();
287 
288     void loadJavaScriptURL(const WebCore::KURL&);
289 
290     FrameLoaderClientImpl m_frameLoaderClient;
291 
292     WebFrameClient* m_client;
293 
294     // This is a weak pointer to our corresponding WebCore frame.  A reference to
295     // ourselves is held while frame_ is valid.  See our Closing method.
296     WebCore::Frame* m_frame;
297 
298     // A way for the main frame to keep track of which frame has an active
299     // match. Should be 0 for all other frames.
300     WebFrameImpl* m_activeMatchFrame;
301 
302     // The range of the active match for the current frame.
303     RefPtr<WebCore::Range> m_activeMatch;
304 
305     // The index of the active match.
306     int m_activeMatchIndex;
307 
308     // This flag is used by the scoping effort to determine if we need to figure
309     // out which rectangle is the active match. Once we find the active
310     // rectangle we clear this flag.
311     bool m_locatingActiveRect;
312 
313     // The scoping effort can time out and we need to keep track of where we
314     // ended our last search so we can continue from where we left of.
315     RefPtr<WebCore::Range> m_resumeScopingFromRange;
316 
317     // Keeps track of the last string this frame searched for. This is used for
318     // short-circuiting searches in the following scenarios: When a frame has
319     // been searched and returned 0 results, we don't need to search that frame
320     // again if the user is just adding to the search (making it more specific).
321     WebCore::String m_lastSearchString;
322 
323     // Keeps track of how many matches this frame has found so far, so that we
324     // don't loose count between scoping efforts, and is also used (in conjunction
325     // with m_lastSearchString and m_scopingComplete) to figure out if we need to
326     // search the frame again.
327     int m_lastMatchCount;
328 
329     // This variable keeps a cumulative total of matches found so far for ALL the
330     // frames on the page, and is only incremented by calling IncreaseMatchCount
331     // (on the main frame only). It should be -1 for all other frames.
332     size_t m_totalMatchCount;
333 
334     // This variable keeps a cumulative total of how many frames are currently
335     // scoping, and is incremented/decremented on the main frame only.
336     // It should be -1 for all other frames.
337     int m_framesScopingCount;
338 
339     // Keeps track of whether the scoping effort was completed (the user may
340     // interrupt it before it completes by submitting a new search).
341     bool m_scopingComplete;
342 
343     // Keeps track of when the scoping effort should next invalidate the scrollbar
344     // and the frame area.
345     int m_nextInvalidateAfter;
346 
347     // A list of all of the pending calls to scopeStringMatches.
348     Vector<DeferredScopeStringMatches*> m_deferredScopingWork;
349 
350     // Valid between calls to BeginPrint() and EndPrint(). Containts the print
351     // information. Is used by PrintPage().
352     OwnPtr<ChromePrintContext> m_printContext;
353 
354     // The input fields that are interested in edit events and their associated
355     // listeners.
356     typedef HashMap<RefPtr<WebCore::HTMLInputElement>,
357                     WebPasswordAutocompleteListener*> PasswordListenerMap;
358     PasswordListenerMap m_passwordListeners;
359 
360     // Keeps a reference to the frame's WebAnimationController.
361     WebAnimationControllerImpl m_animationController;
362 };
363 
364 } // namespace WebKit
365 
366 #endif
367