1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_RENDER_FRAME_IMPL_H_
6 #define CONTENT_RENDERER_RENDER_FRAME_IMPL_H_
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/id_map.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/process/process_handle.h"
17 #include "content/public/common/referrer.h"
18 #include "content/public/renderer/render_frame.h"
19 #include "content/renderer/renderer_webcookiejar_impl.h"
20 #include "ipc/ipc_message.h"
21 #include "third_party/WebKit/public/web/WebDataSource.h"
22 #include "third_party/WebKit/public/web/WebFrameClient.h"
23 #include "ui/gfx/range/range.h"
26 struct FrameMsg_BuffersSwapped_Params;
27 struct FrameMsg_CompositorFrameSwapped_Params;
28 struct FrameMsg_Navigate_Params;
33 class WebContentDecryptionModule;
34 class WebSecurityOrigin;
35 struct WebCompositionUnderline;
36 struct WebContextMenuData;
48 class ChildFrameCompositingHelper;
49 class PepperPluginInstanceImpl;
50 class RendererPpapiHost;
51 class RenderFrameObserver;
54 class RenderWidgetFullscreenPepper;
55 struct CustomContextMenuContext;
57 class CONTENT_EXPORT RenderFrameImpl
59 NON_EXPORTED_BASE(public blink::WebFrameClient) {
61 // Creates a new RenderFrame. |render_view| is the RenderView object that this
63 // Callers *must* call |SetWebFrame| immediately after creation.
64 // TODO(creis): We should structure this so that |SetWebFrame| isn't needed.
65 static RenderFrameImpl* Create(RenderViewImpl* render_view, int32 routing_id);
67 // Just like RenderFrame::FromWebFrame but returns the implementation.
68 static RenderFrameImpl* FromWebFrame(blink::WebFrame* web_frame);
70 // Used by content_layouttest_support to hook into the creation of
72 static void InstallCreateHook(
73 RenderFrameImpl* (*create_render_frame_impl)(RenderViewImpl*, int32));
75 virtual ~RenderFrameImpl();
77 bool is_swapped_out() const {
78 return is_swapped_out_;
81 // Out-of-process child frames receive a signal from RenderWidgetCompositor
82 // when a compositor frame has committed.
83 void DidCommitCompositorFrame();
85 // TODO(jam): this is a temporary getter until all the code is transitioned
86 // to using RenderFrame instead of RenderView.
87 RenderViewImpl* render_view() { return render_view_.get(); }
89 RendererWebCookieJarImpl* cookie_jar() { return &cookie_jar_; }
91 // Returns the RenderWidget associated with this frame.
92 RenderWidget* GetRenderWidget();
94 // This is called right after creation with the WebFrame for this RenderFrame.
95 void SetWebFrame(blink::WebFrame* web_frame);
97 // Notification from RenderView.
98 virtual void OnStop();
100 // Start/Stop loading notifications.
101 // TODO(nasko): Those are page-level methods at this time and come from
102 // WebViewClient. We should move them to be WebFrameClient calls and put
103 // logic in the browser side to balance starts/stops.
104 // |to_different_document| will be true unless the load is a fragment
105 // navigation, or triggered by history.pushState/replaceState.
106 virtual void didStartLoading(bool to_different_document);
107 virtual void didStopLoading();
108 virtual void didChangeLoadProgress(double load_progress);
110 #if defined(ENABLE_PLUGINS)
111 // Notification that a PPAPI plugin has been created.
112 void PepperPluginCreated(RendererPpapiHost* host);
114 // Notifies that |instance| has changed the cursor.
115 // This will update the cursor appearance if it is currently over the plugin
117 void PepperDidChangeCursor(PepperPluginInstanceImpl* instance,
118 const blink::WebCursorInfo& cursor);
120 // Notifies that |instance| has received a mouse event.
121 void PepperDidReceiveMouseEvent(PepperPluginInstanceImpl* instance);
123 // Informs the render view that a PPAPI plugin has changed text input status.
124 void PepperTextInputTypeChanged(PepperPluginInstanceImpl* instance);
125 void PepperCaretPositionChanged(PepperPluginInstanceImpl* instance);
127 // Cancels current composition.
128 void PepperCancelComposition(PepperPluginInstanceImpl* instance);
130 // Informs the render view that a PPAPI plugin has changed selection.
131 void PepperSelectionChanged(PepperPluginInstanceImpl* instance);
133 // Creates a fullscreen container for a pepper plugin instance.
134 RenderWidgetFullscreenPepper* CreatePepperFullscreenContainer(
135 PepperPluginInstanceImpl* plugin);
137 bool IsPepperAcceptingCompositionEvents() const;
139 // Notification that the given plugin has crashed.
140 void PluginCrashed(const base::FilePath& plugin_path,
141 base::ProcessId plugin_pid);
143 // Simulates IME events for testing purpose.
144 void SimulateImeSetComposition(
145 const base::string16& text,
146 const std::vector<blink::WebCompositionUnderline>& underlines,
149 void SimulateImeConfirmComposition(const base::string16& text,
150 const gfx::Range& replacement_range);
152 // TODO(jam): remove these once the IPC handler moves from RenderView to
154 void OnImeSetComposition(
155 const base::string16& text,
156 const std::vector<blink::WebCompositionUnderline>& underlines,
159 void OnImeConfirmComposition(
160 const base::string16& text,
161 const gfx::Range& replacement_range,
162 bool keep_selection);
163 #endif // ENABLE_PLUGINS
166 virtual bool Send(IPC::Message* msg) OVERRIDE;
169 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
171 // RenderFrame implementation:
172 virtual RenderView* GetRenderView() OVERRIDE;
173 virtual int GetRoutingID() OVERRIDE;
174 virtual blink::WebFrame* GetWebFrame() OVERRIDE;
175 virtual WebPreferences& GetWebkitPreferences() OVERRIDE;
176 virtual int ShowContextMenu(ContextMenuClient* client,
177 const ContextMenuParams& params) OVERRIDE;
178 virtual void CancelContextMenu(int request_id) OVERRIDE;
179 virtual blink::WebNode GetContextMenuNode() const OVERRIDE;
180 virtual blink::WebPlugin* CreatePlugin(
181 blink::WebFrame* frame,
182 const WebPluginInfo& info,
183 const blink::WebPluginParams& params) OVERRIDE;
184 virtual void LoadURLExternally(
185 blink::WebFrame* frame,
186 const blink::WebURLRequest& request,
187 blink::WebNavigationPolicy policy) OVERRIDE;
188 virtual void ExecuteJavaScript(const base::string16& javascript) OVERRIDE;
190 // blink::WebFrameClient implementation -------------------------------------
191 virtual blink::WebPlugin* createPlugin(
192 blink::WebFrame* frame,
193 const blink::WebPluginParams& params);
194 virtual blink::WebMediaPlayer* createMediaPlayer(
195 blink::WebFrame* frame,
196 const blink::WebURL& url,
197 blink::WebMediaPlayerClient* client);
198 virtual blink::WebContentDecryptionModule* createContentDecryptionModule(
199 blink::WebFrame* frame,
200 const blink::WebSecurityOrigin& security_origin,
201 const blink::WebString& key_system);
202 virtual blink::WebApplicationCacheHost* createApplicationCacheHost(
203 blink::WebFrame* frame,
204 blink::WebApplicationCacheHostClient* client);
205 virtual blink::WebWorkerPermissionClientProxy*
206 createWorkerPermissionClientProxy(blink::WebFrame* frame);
207 virtual blink::WebCookieJar* cookieJar(blink::WebFrame* frame);
208 virtual blink::WebServiceWorkerProvider* createServiceWorkerProvider(
209 blink::WebFrame* frame);
210 virtual void didAccessInitialDocument(blink::WebFrame* frame);
211 virtual blink::WebFrame* createChildFrame(blink::WebFrame* parent,
212 const blink::WebString& name);
213 virtual void didDisownOpener(blink::WebFrame* frame);
214 virtual void frameDetached(blink::WebFrame* frame);
215 virtual void frameFocused();
216 virtual void willClose(blink::WebFrame* frame);
217 virtual void didChangeName(blink::WebFrame* frame,
218 const blink::WebString& name);
219 virtual void didMatchCSS(
220 blink::WebFrame* frame,
221 const blink::WebVector<blink::WebString>& newly_matching_selectors,
222 const blink::WebVector<blink::WebString>& stopped_matching_selectors);
223 virtual void loadURLExternally(blink::WebFrame* frame,
224 const blink::WebURLRequest& request,
225 blink::WebNavigationPolicy policy);
226 virtual void loadURLExternally(
227 blink::WebFrame* frame,
228 const blink::WebURLRequest& request,
229 blink::WebNavigationPolicy policy,
230 const blink::WebString& suggested_name);
231 // The WebDataSource::ExtraData* is assumed to be a DocumentState* subclass.
232 virtual blink::WebNavigationPolicy decidePolicyForNavigation(
233 blink::WebFrame* frame,
234 blink::WebDataSource::ExtraData* extra_data,
235 const blink::WebURLRequest& request,
236 blink::WebNavigationType type,
237 blink::WebNavigationPolicy default_policy,
240 virtual blink::WebNavigationPolicy decidePolicyForNavigation(
241 blink::WebFrame* frame,
242 const blink::WebURLRequest& request,
243 blink::WebNavigationType type,
244 blink::WebNavigationPolicy default_policy,
246 virtual void willSendSubmitEvent(blink::WebFrame* frame,
247 const blink::WebFormElement& form);
248 virtual void willSubmitForm(blink::WebFrame* frame,
249 const blink::WebFormElement& form);
250 virtual void didCreateDataSource(blink::WebFrame* frame,
251 blink::WebDataSource* datasource);
252 virtual void didStartProvisionalLoad(blink::WebFrame* frame);
253 virtual void didReceiveServerRedirectForProvisionalLoad(
254 blink::WebFrame* frame);
255 virtual void didFailProvisionalLoad(
256 blink::WebFrame* frame,
257 const blink::WebURLError& error);
258 virtual void didCommitProvisionalLoad(blink::WebFrame* frame,
259 bool is_new_navigation);
260 virtual void didClearWindowObject(blink::WebFrame* frame, int world_id);
261 virtual void didCreateDocumentElement(blink::WebFrame* frame);
262 virtual void didReceiveTitle(blink::WebFrame* frame,
263 const blink::WebString& title,
264 blink::WebTextDirection direction);
265 virtual void didChangeIcon(blink::WebFrame* frame,
266 blink::WebIconURL::Type icon_type);
267 virtual void didFinishDocumentLoad(blink::WebFrame* frame);
268 virtual void didHandleOnloadEvents(blink::WebFrame* frame);
269 virtual void didFailLoad(blink::WebFrame* frame,
270 const blink::WebURLError& error);
271 virtual void didFinishLoad(blink::WebFrame* frame);
272 virtual void didNavigateWithinPage(blink::WebFrame* frame,
273 bool is_new_navigation);
274 virtual void didUpdateCurrentHistoryItem(blink::WebFrame* frame);
275 virtual void didChangeSelection(bool is_empty_selection);
276 virtual void showContextMenu(const blink::WebContextMenuData& data);
277 virtual void clearContextMenu();
278 virtual void willRequestAfterPreconnect(blink::WebFrame* frame,
279 blink::WebURLRequest& request);
280 virtual void willSendRequest(
281 blink::WebFrame* frame,
283 blink::WebURLRequest& request,
284 const blink::WebURLResponse& redirect_response);
285 virtual void didReceiveResponse(
286 blink::WebFrame* frame,
288 const blink::WebURLResponse& response);
289 virtual void didFinishResourceLoad(blink::WebFrame* frame,
290 unsigned identifier);
291 virtual void didLoadResourceFromMemoryCache(
292 blink::WebFrame* frame,
293 const blink::WebURLRequest& request,
294 const blink::WebURLResponse& response);
295 virtual void didDisplayInsecureContent(blink::WebFrame* frame);
296 virtual void didRunInsecureContent(blink::WebFrame* frame,
297 const blink::WebSecurityOrigin& origin,
298 const blink::WebURL& target);
299 virtual void didAbortLoading(blink::WebFrame* frame);
300 virtual void didCreateScriptContext(blink::WebFrame* frame,
301 v8::Handle<v8::Context> context,
304 virtual void willReleaseScriptContext(blink::WebFrame* frame,
305 v8::Handle<v8::Context> context,
307 virtual void didFirstVisuallyNonEmptyLayout(blink::WebFrame* frame);
308 virtual void didChangeContentsSize(blink::WebFrame* frame,
309 const blink::WebSize& size);
310 virtual void didChangeScrollOffset(blink::WebFrame* frame);
311 virtual void willInsertBody(blink::WebFrame* frame);
312 virtual void reportFindInPageMatchCount(int request_id,
315 virtual void reportFindInPageSelection(int request_id,
316 int active_match_ordinal,
317 const blink::WebRect& sel);
318 virtual void requestStorageQuota(
319 blink::WebFrame* frame,
320 blink::WebStorageQuotaType type,
321 unsigned long long requested_size,
322 blink::WebStorageQuotaCallbacks callbacks);
323 virtual void willOpenSocketStream(
324 blink::WebSocketStreamHandle* handle);
325 virtual void willStartUsingPeerConnectionHandler(
326 blink::WebFrame* frame,
327 blink::WebRTCPeerConnectionHandler* handler);
328 virtual bool willCheckAndDispatchMessageEvent(
329 blink::WebFrame* sourceFrame,
330 blink::WebFrame* targetFrame,
331 blink::WebSecurityOrigin targetOrigin,
332 blink::WebDOMMessageEvent event);
333 virtual blink::WebString userAgentOverride(
334 blink::WebFrame* frame,
335 const blink::WebURL& url);
336 virtual blink::WebString doNotTrackValue(blink::WebFrame* frame);
337 virtual bool allowWebGL(blink::WebFrame* frame, bool default_value);
338 virtual void didLoseWebGLContext(blink::WebFrame* frame,
339 int arb_robustness_status_code);
340 virtual void forwardInputEvent(const blink::WebInputEvent* event);
341 virtual void initializeChildFrame(const blink::WebRect& frame_rect,
344 // TODO(nasko): Make all tests in RenderViewImplTest friends and then move
345 // this back to private member.
346 void OnNavigate(const FrameMsg_Navigate_Params& params);
349 RenderFrameImpl(RenderViewImpl* render_view, int32 routing_id);
352 friend class RenderFrameObserver;
353 FRIEND_TEST_ALL_PREFIXES(RendererAccessibilityTest,
354 AccessibilityMessagesQueueWhileSwappedOut);
355 FRIEND_TEST_ALL_PREFIXES(RenderFrameImplTest,
356 ShouldUpdateSelectionTextFromContextMenuParams);
357 FRIEND_TEST_ALL_PREFIXES(RenderViewImplTest,
358 OnExtendSelectionAndDelete);
359 FRIEND_TEST_ALL_PREFIXES(RenderViewImplTest, ReloadWhileSwappedOut);
360 FRIEND_TEST_ALL_PREFIXES(RenderViewImplTest, SendSwapOutACK);
361 FRIEND_TEST_ALL_PREFIXES(RenderViewImplTest,
362 SetEditableSelectionAndComposition);
364 typedef std::map<GURL, double> HostZoomLevels;
366 // Functions to add and remove observers for this object.
367 void AddObserver(RenderFrameObserver* observer);
368 void RemoveObserver(RenderFrameObserver* observer);
370 void UpdateURL(blink::WebFrame* frame);
372 // Gets the focused element. If no such element exists then the element will
374 blink::WebElement GetFocusedElement();
376 // IPC message handlers ------------------------------------------------------
378 // The documentation for these functions should be in
379 // content/common/*_messages.h for the message that the function is handling.
380 void OnBeforeUnload();
382 void OnChildFrameProcessGone();
383 void OnBuffersSwapped(const FrameMsg_BuffersSwapped_Params& params);
384 void OnCompositorFrameSwapped(const IPC::Message& message);
385 void OnShowContextMenu(const gfx::Point& location);
386 void OnContextMenuClosed(const CustomContextMenuContext& custom_context);
387 void OnCustomContextMenuAction(const CustomContextMenuContext& custom_context,
394 void OnPasteAndMatchStyle();
397 void OnSelectRange(const gfx::Point& start, const gfx::Point& end);
399 void OnCSSInsertRequest(const std::string& css);
400 void OnJavaScriptExecuteRequest(const base::string16& javascript,
403 void OnSetEditableSelectionOffsets(int start, int end);
404 void OnSetCompositionFromExistingText(
406 const std::vector<blink::WebCompositionUnderline>& underlines);
407 void OnExtendSelectionAndDelete(int before, int after);
408 #if defined(OS_MACOSX)
409 void OnCopyToFindPboard();
412 // Virtual since overridden by WebTestProxy for layout tests.
413 virtual blink::WebNavigationPolicy DecidePolicyForNavigation(
414 RenderFrame* render_frame,
415 blink::WebFrame* frame,
416 blink::WebDataSource::ExtraData* extraData,
417 const blink::WebURLRequest& request,
418 blink::WebNavigationType type,
419 blink::WebNavigationPolicy default_policy,
421 void OpenURL(blink::WebFrame* frame,
423 const Referrer& referrer,
424 blink::WebNavigationPolicy policy);
426 // Dispatches the current state of selection on the webpage to the browser if
428 // TODO(varunjain): delete this method once we figure out how to keep
429 // selection handles in sync with the webpage.
430 void SyncSelectionIfRequired();
432 // Returns whether |params.selection_text| should be synchronized to the
433 // browser before bringing up the context menu. Static for testing.
434 static bool ShouldUpdateSelectionTextFromContextMenuParams(
435 const base::string16& selection_text,
436 size_t selection_text_offset,
437 const gfx::Range& selection_range,
438 const ContextMenuParams& params);
440 // Stores the WebFrame we are associated with.
441 blink::WebFrame* frame_;
443 base::WeakPtr<RenderViewImpl> render_view_;
446 bool is_swapped_out_;
449 #if defined(ENABLE_PLUGINS)
450 // Current text input composition text. Empty if no composition is in
452 base::string16 pepper_composition_text_;
455 RendererWebCookieJarImpl cookie_jar_;
457 // All the registered observers.
458 ObserverList<RenderFrameObserver> observers_;
460 scoped_refptr<ChildFrameCompositingHelper> compositing_helper_;
462 // The node that the context menu was pressed over.
463 blink::WebNode context_menu_node_;
465 // External context menu requests we're waiting for. "Internal"
466 // (WebKit-originated) context menu events will have an ID of 0 and will not
469 // We don't want to add internal ones since some of the "special" page
470 // handlers in the browser process just ignore the context menu requests so
471 // avoid showing context menus, and so this will cause right clicks to leak
472 // entries in this map. Most users of the custom context menu (e.g. Pepper
473 // plugins) are normally only on "regular" pages and the regular pages will
474 // always respond properly to the request, so we don't have to worry so
476 IDMap<ContextMenuClient, IDMapExternalPointer> pending_context_menus_;
478 // The text selection the last time DidChangeSelection got called. May contain
479 // additional characters before and after the selected text, for IMEs. The
480 // portion of this string that is the actual selected text starts at index
481 // |selection_range_.GetMin() - selection_text_offset_| and has length
482 // |selection_range_.length()|.
483 base::string16 selection_text_;
484 // The offset corresponding to the start of |selection_text_| in the document.
485 size_t selection_text_offset_;
486 // Range over the document corresponding to the actual selected text (which
487 // could correspond to a substring of |selection_text_|; see above).
488 gfx::Range selection_range_;
489 // Used to inform didChangeSelection() when it is called in the context
490 // of handling a InputMsg_SelectRange IPC.
491 bool handling_select_range_;
493 DISALLOW_COPY_AND_ASSIGN(RenderFrameImpl);
496 } // namespace content
498 #endif // CONTENT_RENDERER_RENDER_FRAME_IMPL_H_