1 // Copyright (c) 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 EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_
6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_
8 #include "extensions/browser/extension_function.h"
9 #include "extensions/browser/script_executor.h"
10 #include "extensions/common/api/extension_types.h"
11 #include "extensions/common/host_id.h"
13 namespace extensions
{
15 // Base class for javascript code injection.
16 // This is used by both chrome.webview.executeScript and
17 // chrome.tabs.executeScript.
18 class ExecuteCodeFunction
: public AsyncExtensionFunction
{
20 ExecuteCodeFunction();
23 ~ExecuteCodeFunction() override
;
25 // ExtensionFunction implementation.
26 bool HasPermission() override
;
27 bool RunAsync() override
;
29 // Initialize |details_| if it hasn't already been.
30 virtual bool Init() = 0;
31 virtual bool ShouldInsertCSS() const = 0;
32 virtual bool CanExecuteScriptOnPage() = 0;
33 virtual ScriptExecutor
* GetScriptExecutor() = 0;
34 virtual bool IsWebView() const = 0;
35 virtual const GURL
& GetWebViewSrc() const = 0;
36 virtual void OnExecuteCodeFinished(const std::string
& error
,
38 const base::ListValue
& result
);
40 virtual bool LoadFile(const std::string
& file
);
42 // Called when contents from the loaded file have been localized.
43 void DidLoadAndLocalizeFile(const std::string
& file
,
45 const std::string
& data
);
47 const HostID
& host_id() const { return host_id_
; }
48 void set_host_id(HostID host_id
) {
52 // The injection details.
53 scoped_ptr
<api::extension_types::InjectDetails
> details_
;
56 // Called when contents from the file whose path is specified in JSON
57 // arguments has been loaded.
58 void DidLoadFile(bool success
, const std::string
& data
);
60 // Runs on FILE thread. Loads message bundles for the extension and
61 // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread.
62 void GetFileURLAndLocalizeCSS(ScriptExecutor::ScriptType script_type
,
63 const std::string
& data
,
64 const std::string
& extension_id
,
65 const base::FilePath
& extension_path
,
66 const std::string
& extension_default_locale
);
68 // Run in UI thread. Code string contains the code to be executed. Returns
69 // true on success. If true is returned, this does an AddRef.
70 bool Execute(const std::string
& code_string
);
72 // Contains extension resource built from path of file which is
73 // specified in JSON arguments.
74 ExtensionResource resource_
;
76 // The URL of the file being injected into the page.
79 // The ID of the injection host.
83 } // namespace extensions
85 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_