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"
12 namespace extensions
{
14 // Base class for javascript code injection.
15 // This is used by both chrome.webview.executeScript and
16 // chrome.tabs.executeScript.
17 class ExecuteCodeFunction
: public AsyncExtensionFunction
{
19 ExecuteCodeFunction();
22 ~ExecuteCodeFunction() override
;
24 // ExtensionFunction implementation.
25 bool HasPermission() override
;
26 bool RunAsync() override
;
28 // Initialize |details_| if it hasn't already been.
29 virtual bool Init() = 0;
30 virtual bool ShouldInsertCSS() const = 0;
31 virtual bool CanExecuteScriptOnPage() = 0;
32 virtual ScriptExecutor
* GetScriptExecutor() = 0;
33 virtual bool IsWebView() const = 0;
34 virtual const GURL
& GetWebViewSrc() const = 0;
35 virtual void OnExecuteCodeFinished(const std::string
& error
,
37 const base::ListValue
& result
);
39 // The injection details.
40 scoped_ptr
<core_api::extension_types::InjectDetails
> details_
;
43 // Called when contents from the file whose path is specified in JSON
44 // arguments has been loaded.
45 void DidLoadFile(bool success
, const std::string
& data
);
47 // Runs on FILE thread. Loads message bundles for the extension and
48 // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread.
49 void GetFileURLAndLocalizeCSS(ScriptExecutor::ScriptType script_type
,
50 const std::string
& data
,
51 const std::string
& extension_id
,
52 const base::FilePath
& extension_path
,
53 const std::string
& extension_default_locale
);
55 // Called when contents from the loaded file have been localized.
56 void DidLoadAndLocalizeFile(bool success
, const std::string
& data
);
58 // Run in UI thread. Code string contains the code to be executed. Returns
59 // true on success. If true is returned, this does an AddRef.
60 bool Execute(const std::string
& code_string
);
62 // Contains extension resource built from path of file which is
63 // specified in JSON arguments.
64 ExtensionResource resource_
;
66 // The URL of the file being injected into the page.
70 } // namespace extensions
72 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_