1 // Copyright 2014 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 EXTENSIONS_RENDERER_USER_SCRIPT_SCHEDULER_H_
6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SCHEDULER_H_
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/common/user_script.h"
16 struct ExtensionMsg_ExecuteCode_Params
;
22 namespace extensions
{
25 // Implements support for injecting scripts at different times in the document
26 // loading process. The different possible time are described in
27 // UserScript::RunLocation.
29 // Currently, determining idleness is simple: it is whichever of the following
32 // a) When the initial DOM for a page is complete + kUserScriptIdleTimeout,
33 // b) or when the page has completely loaded including all subresources.
35 // The intent of this mechanism is to prevent user scripts from slowing down
36 // fast pages (run after load), while still allowing them to run relatively
37 // timely for pages with lots of slow subresources.
39 // NOTE: this class does not inherit from RenderViewObserver on purpose. The
40 // reason is that this object is per frame, and a frame can move across
41 // RenderViews thanks to adoptNode. So we have each RenderView's
42 // ExtensionHelper proxy these calls to the renderer process' Dispatcher,
43 // which contains the mapping from WebFrame to us.
44 class UserScriptScheduler
{
46 UserScriptScheduler(blink::WebFrame
* frame
, Dispatcher
* dispatcher
);
47 ~UserScriptScheduler();
49 void ExecuteCode(const ExtensionMsg_ExecuteCode_Params
& params
);
50 void DidCreateDocumentElement();
51 void DidFinishDocumentLoad();
53 void DidStartProvisionalLoad();
57 std::queue
<linked_ptr
<ExtensionMsg_ExecuteCode_Params
> >
60 // Run user scripts, except if they've already run for this frame, or the
61 // frame has been destroyed.
64 // Backend for the IPC Message ExecuteCode in addition to being used
66 void ExecuteCodeImpl(const ExtensionMsg_ExecuteCode_Params
& params
);
68 // Get all child frames of parent_frame, returned by frames_vector.
69 bool GetAllChildFrames(blink::WebFrame
* parent_frame
,
70 std::vector
<blink::WebFrame
*>* frames_vector
) const;
72 // Call to signify thet the idle timeout has expired.
75 base::WeakPtrFactory
<UserScriptScheduler
> weak_factory_
;
77 // The Frame we will run scripts in.
78 blink::WebFrame
* frame_
;
80 // The current location in the document loading process.
81 // Will be UserScript::UNDEFINED if it is before any scripts should be run.
82 UserScript::RunLocation current_location_
;
84 // Whether we have already run the idle scripts.
87 // This is only used if we're for the main frame.
88 std::map
<UserScript::RunLocation
, ExecutionQueue
> pending_execution_map_
;
90 Dispatcher
* dispatcher_
;
93 } // namespace extensions
95 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SCHEDULER_H_