execute_code_function.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_API_EXECUTE_CODE_FUNCTION_H_
6#define CHROME_BROWSER_EXTENSIONS_API_EXECUTE_CODE_FUNCTION_H_
7
8#include "chrome/browser/extensions/extension_function.h"
9#include "chrome/common/extensions/api/tabs.h"
10
11namespace extensions {
12
13class ScriptExecutor;
14
15// Base class for javascript code injection.
16// This is used by both chrome.webview.executeScript and
17// chrome.tabs.executeScript.
18class ExecuteCodeFunction : public AsyncExtensionFunction {
19 public:
20  ExecuteCodeFunction();
21
22 protected:
23  virtual ~ExecuteCodeFunction();
24
25  // ExtensionFunction implementation.
26  virtual bool HasPermission() OVERRIDE;
27  virtual bool RunImpl() OVERRIDE;
28
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
36  virtual void OnExecuteCodeFinished(const std::string& error,
37                                     int32 on_page_id,
38                                     const GURL& on_url,
39                                     const ListValue& result);
40
41  // The injection details.
42  scoped_ptr<api::tabs::InjectDetails> details_;
43
44 private:
45  // Called when contents from the file whose path is specified in JSON
46  // arguments has been loaded.
47  void DidLoadFile(bool success, const std::string& data);
48
49  // Runs on FILE thread. Loads message bundles for the extension and
50  // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread.
51  void LocalizeCSS(
52      const std::string& data,
53      const std::string& extension_id,
54      const base::FilePath& extension_path,
55      const std::string& extension_default_locale);
56
57  // Called when contents from the loaded file have been localized.
58  void DidLoadAndLocalizeFile(bool success, const std::string& data);
59
60  // Run in UI thread.  Code string contains the code to be executed. Returns
61  // true on success. If true is returned, this does an AddRef.
62  bool Execute(const std::string& code_string);
63
64  // Contains extension resource built from path of file which is
65  // specified in JSON arguments.
66  ExtensionResource resource_;
67};
68
69}  // namespace extensions
70
71#endif  // CHROME_BROWSER_EXTENSIONS_API_EXECUTE_CODE_FUNCTION_H_
72
73