summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/declarative_content/content_action.h
blob: a4b91876d417f82d30e48b454cad262f159b9599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_
#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_

#include <string>

#include "base/memory/scoped_ptr.h"
#include "extensions/browser/declarative_user_script_master.h"
#include "extensions/common/user_script.h"

namespace base {
class Value;
class DictionaryValue;
}

namespace content {
class BrowserContext;
class WebContents;
}

namespace extensions {

class Extension;

// Base class for all ContentActions of the declarative content API.
class ContentAction {
 public:
  struct ApplyInfo {
    const Extension* extension;
    content::BrowserContext* browser_context;
    content::WebContents* tab;
    int priority;
  };

  virtual ~ContentAction();

  // Applies or reverts this ContentAction on a particular tab for a particular
  // extension.  Revert exists to keep the actions up to date as the page
  // changes.  Reapply exists to reapply changes to a new page, even if the
  // previous page also matched relevant conditions.
  virtual void Apply(const ApplyInfo& apply_info) const = 0;
  virtual void Reapply(const ApplyInfo& apply_info) const = 0;
  virtual void Revert(const ApplyInfo& apply_info) const = 0;

  // Factory method that instantiates a concrete ContentAction implementation
  // according to |json_action|, the representation of the ContentAction as
  // received from the extension API.  Sets |error| and returns NULL in case of
  // an error.
  static scoped_ptr<ContentAction> Create(
      content::BrowserContext* browser_context,
      const Extension* extension,
      const base::Value& json_action,
      std::string* error);

 protected:
  ContentAction();
};

// Action that injects a content script.
class RequestContentScript : public ContentAction {
 public:
  struct ScriptData;

  RequestContentScript(content::BrowserContext* browser_context,
                       const Extension* extension,
                       const ScriptData& script_data);
  RequestContentScript(DeclarativeUserScriptMaster* master,
                       const Extension* extension,
                       const ScriptData& script_data);

  ~RequestContentScript() override;

  static scoped_ptr<ContentAction> Create(
      content::BrowserContext* browser_context,
      const Extension* extension,
      const base::DictionaryValue* dict,
      std::string* error);

  static scoped_ptr<ContentAction> CreateForTest(
      DeclarativeUserScriptMaster* master,
      const Extension* extension,
      const base::Value& json_action,
      std::string* error);

  static bool InitScriptData(const base::DictionaryValue* dict,
                             std::string* error,
                             ScriptData* script_data);

  // Implementation of ContentAction:
  void Apply(const ApplyInfo& apply_info) const override;
  void Reapply(const ApplyInfo& apply_info) const override;
  void Revert(const ApplyInfo& apply_info) const override;

 private:
  void InitScript(const HostID& host_id,
                  const Extension* extension,
                  const ScriptData& script_data);

  void AddScript() {
    DCHECK(master_);
    master_->AddScript(script_);
  }

  void InstructRenderProcessToInject(content::WebContents* contents,
                                     const Extension* extension) const;

  UserScript script_;
  DeclarativeUserScriptMaster* master_;

  DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_