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
|
// 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.
#include "chrome/browser/extensions/api/declarative/declarative_api.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/task_runner_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
#include "chrome/browser/extensions/extension_system_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/events.h"
#include "chrome/common/extensions/api/extension_api.h"
#include "content/public/browser/browser_thread.h"
using extensions::api::events::Rule;
namespace AddRules = extensions::api::events::Event::AddRules;
namespace GetRules = extensions::api::events::Event::GetRules;
namespace RemoveRules = extensions::api::events::Event::RemoveRules;
namespace extensions {
RulesFunction::RulesFunction() : rules_registry_(NULL) {}
RulesFunction::~RulesFunction() {}
bool RulesFunction::HasPermission() {
std::string event_name;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
Feature::Availability availability =
ExtensionAPI::GetSharedInstance()->IsAvailable(
event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
source_url());
return availability.is_available();
}
bool RulesFunction::RunImpl() {
std::string event_name;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
RulesRegistryService* rules_registry_service =
RulesRegistryService::Get(profile());
rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
// Raw access to this function is not available to extensions, therefore
// there should never be a request for a nonexisting rules registry.
EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
bool success = RunImplOnCorrectThread();
SendResponse(success);
} else {
scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
content::BrowserThread::GetMessageLoopProxyForThread(
rules_registry_->owner_thread());
base::PostTaskAndReplyWithResult(
message_loop_proxy.get(),
FROM_HERE,
base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
base::Bind(&RulesFunction::SendResponse, this));
}
return true;
}
bool EventsEventAddRulesFunction::RunImplOnCorrectThread() {
scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
error_ = rules_registry_->AddRules(extension_id(), params->rules);
if (error_.empty())
results_ = AddRules::Results::Create(params->rules);
return error_.empty();
}
bool EventsEventRemoveRulesFunction::RunImplOnCorrectThread() {
scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
if (params->rule_identifiers.get()) {
error_ = rules_registry_->RemoveRules(extension_id(),
*params->rule_identifiers);
} else {
error_ = rules_registry_->RemoveAllRules(extension_id());
}
return error_.empty();
}
bool EventsEventGetRulesFunction::RunImplOnCorrectThread() {
scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
std::vector<linked_ptr<Rule> > rules;
if (params->rule_identifiers.get()) {
error_ = rules_registry_->GetRules(extension_id(),
*params->rule_identifiers,
&rules);
} else {
error_ = rules_registry_->GetAllRules(extension_id(), &rules);
}
if (error_.empty())
results_ = GetRules::Results::Create(rules);
return error_.empty();
}
} // namespace extensions
|