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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// 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 <vector>
#include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/declarative_user_script_master.h"
#include "extensions/browser/api/declarative/declarative_rule.h"
namespace base {
class Time;
class Value;
}
namespace content {
class BrowserContext;
class WebContents;
}
namespace extensions {
class Extension;
// Base class for all ContentActions of the declarative content API.
class ContentAction : public base::RefCounted<ContentAction> {
public:
// Type identifiers for concrete ContentActions.
enum Type {
ACTION_SHOW_PAGE_ACTION,
ACTION_REQUEST_CONTENT_SCRIPT,
ACTION_SET_ICON,
};
struct ApplyInfo {
content::BrowserContext* browser_context;
content::WebContents* tab;
int priority;
};
ContentAction();
virtual Type GetType() const = 0;
// 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 std::string& extension_id,
const base::Time& extension_install_time,
ApplyInfo* apply_info) const = 0;
virtual void Reapply(const std::string& extension_id,
const base::Time& extension_install_time,
ApplyInfo* apply_info) const = 0;
virtual void Revert(const std::string& extension_id,
const base::Time& extension_install_time,
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 a semantic error that cannot
// be caught by schema validation. Sets |bad_message| and returns NULL
// in case the input is syntactically unexpected.
static scoped_refptr<ContentAction> Create(
content::BrowserContext* browser_context,
const Extension* extension,
const base::Value& json_action,
std::string* error,
bool* bad_message);
// Shared procedure for resetting error state within factories.
static void ResetErrorData(std::string* error, bool* bad_message) {
*error = "";
*bad_message = false;
}
// Shared procedure for validating JSON data.
static bool Validate(const base::Value& json_action,
std::string* error,
bool* bad_message,
const base::DictionaryValue** action_dict,
std::string* instance_type);
protected:
friend class base::RefCounted<ContentAction>;
virtual ~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);
static scoped_refptr<ContentAction> Create(
content::BrowserContext* browser_context,
const Extension* extension,
const base::DictionaryValue* dict,
std::string* error,
bool* bad_message);
static scoped_refptr<ContentAction> CreateForTest(
DeclarativeUserScriptMaster* master,
const Extension* extension,
const base::Value& json_action,
std::string* error,
bool* bad_message);
static bool InitScriptData(const base::DictionaryValue* dict,
std::string* error,
bool* bad_message,
ScriptData* script_data);
// Implementation of ContentAction:
Type GetType() const override;
void Apply(const std::string& extension_id,
const base::Time& extension_install_time,
ApplyInfo* apply_info) const override;
void Reapply(const std::string& extension_id,
const base::Time& extension_install_time,
ApplyInfo* apply_info) const override;
void Revert(const std::string& extension_id,
const base::Time& extension_install_time,
ApplyInfo* apply_info) const override;
private:
void InitScript(const Extension* extension, const ScriptData& script_data);
void AddScript() {
DCHECK(master_);
master_->AddScript(script_);
}
~RequestContentScript() override;
void InstructRenderProcessToInject(content::WebContents* contents,
const std::string& extension_id) const;
UserScript script_;
DeclarativeUserScriptMaster* master_;
DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
};
typedef DeclarativeActionSet<ContentAction> ContentActionSet;
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_
|