blob: ef61b20f95c33468776b51f2595fc95a1ba7e6c1 [file] [log] [blame]
[email protected]c11e6592014-06-27 17:07:341// 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.
4
5#ifndef EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_
6#define EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_
7
dchengf6f80662016-04-20 20:26:048#include <memory>
9
[email protected]c11e6592014-06-27 17:07:3410#include "base/macros.h"
rdevlin.cronin4bb32d72015-06-02 21:55:0111#include "base/values.h"
[email protected]c11e6592014-06-27 17:07:3412#include "extensions/renderer/script_injection.h"
[email protected]cde18a2d2014-07-01 03:13:0413#include "url/gurl.h"
[email protected]c11e6592014-06-27 17:07:3414
15struct ExtensionMsg_ExecuteCode_Params;
16
[email protected]cde18a2d2014-07-01 03:13:0417namespace content {
rdevlin.cronin3e11c9862015-06-04 19:54:2518class RenderFrame;
[email protected]cde18a2d2014-07-01 03:13:0419}
20
[email protected]c11e6592014-06-27 17:07:3421namespace extensions {
[email protected]c11e6592014-06-27 17:07:3422
23// A ScriptInjector to handle tabs.executeScript().
24class ProgrammaticScriptInjector : public ScriptInjector {
25 public:
jam69e71152016-11-02 01:15:4326 explicit ProgrammaticScriptInjector(
27 const ExtensionMsg_ExecuteCode_Params& params);
dcheng9168b2f2014-10-21 12:38:2428 ~ProgrammaticScriptInjector() override;
[email protected]c11e6592014-06-27 17:07:3429
30 private:
31 // ScriptInjector implementation.
dcheng9168b2f2014-10-21 12:38:2432 UserScript::InjectionType script_type() const override;
dcheng9168b2f2014-10-21 12:38:2433 bool ShouldExecuteInMainWorld() const override;
34 bool IsUserGesture() const override;
Manish Jethani9494d722018-01-20 00:28:4735 base::Optional<CSSOrigin> GetCssOrigin() const override;
dcheng9168b2f2014-10-21 12:38:2436 bool ExpectsResults() const override;
catmullingsd4faad4f2016-09-08 19:55:3037 bool ShouldInjectJs(
38 UserScript::RunLocation run_location,
39 const std::set<std::string>& executing_scripts) const override;
40 bool ShouldInjectCss(
41 UserScript::RunLocation run_location,
42 const std::set<std::string>& injected_stylesheets) const override;
dcheng9168b2f2014-10-21 12:38:2443 PermissionsData::AccessType CanExecuteOnFrame(
hanxia5c856cf2015-02-13 20:51:5844 const InjectionHost* injection_host,
rdevlin.cronin3e11c9862015-06-04 19:54:2545 blink::WebLocalFrame* web_frame,
jam69e71152016-11-02 01:15:4346 int tab_id) override;
dcheng9168b2f2014-10-21 12:38:2447 std::vector<blink::WebScriptSource> GetJsSources(
catmullingsd4faad4f2016-09-08 19:55:3048 UserScript::RunLocation run_location,
49 std::set<std::string>* executing_scripts,
50 size_t* num_injected_js_scripts) const override;
lazyboy49cc0b3a2016-08-18 21:55:1251 std::vector<blink::WebString> GetCssSources(
catmullingsd4faad4f2016-09-08 19:55:3052 UserScript::RunLocation run_location,
53 std::set<std::string>* injected_stylesheets,
54 size_t* num_injected_stylesheets) const override;
dchengf6f80662016-04-20 20:26:0455 void OnInjectionComplete(std::unique_ptr<base::Value> execution_result,
rdevlin.cronind533be962015-10-02 17:01:1856 UserScript::RunLocation run_location,
57 content::RenderFrame* render_frame) override;
58 void OnWillNotInject(InjectFailureReason reason,
59 content::RenderFrame* render_frame) override;
[email protected]c11e6592014-06-27 17:07:3460
rob52277c82016-02-07 17:28:5761 // Whether it is safe to include information about the URL in error messages.
62 bool CanShowUrlInError() const;
63
[email protected]c11e6592014-06-27 17:07:3464 // Notify the browser that the script was injected (or never will be), and
65 // send along any results or errors.
rdevlin.cronind533be962015-10-02 17:01:1866 void Finish(const std::string& error, content::RenderFrame* render_frame);
[email protected]c11e6592014-06-27 17:07:3467
68 // The parameters for injecting the script.
dchengf6f80662016-04-20 20:26:0469 std::unique_ptr<ExtensionMsg_ExecuteCode_Params> params_;
[email protected]c11e6592014-06-27 17:07:3470
[email protected]cde18a2d2014-07-01 03:13:0471 // The url of the frame into which we are injecting.
72 GURL url_;
73
rob52277c82016-02-07 17:28:5774 // The serialization of the frame's origin if the frame is an about:-URL. This
75 // is used to provide user-friendly messages.
76 std::string origin_for_about_error_;
roba9e6e6422015-03-30 21:14:0277
[email protected]c11e6592014-06-27 17:07:3478 // The results of the script execution.
rdevlin.cronin4bb32d72015-06-02 21:55:0179 base::ListValue results_;
[email protected]c11e6592014-06-27 17:07:3480
81 // Whether or not this script injection has finished.
82 bool finished_;
83
84 DISALLOW_COPY_AND_ASSIGN(ProgrammaticScriptInjector);
85};
86
87} // namespace extensions
88
89#endif // EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_