OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/declarative/declarative_api.h" | 5 #include "chrome/browser/extensions/api/declarative/declarative_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 namespace extensions { | 39 namespace extensions { |
40 | 40 |
41 RulesFunction::RulesFunction() : rules_registry_(NULL) {} | 41 RulesFunction::RulesFunction() : rules_registry_(NULL) {} |
42 | 42 |
43 RulesFunction::~RulesFunction() {} | 43 RulesFunction::~RulesFunction() {} |
44 | 44 |
45 bool RulesFunction::HasPermission() { | |
46 return Init() && extension_->HasAPIPermission(event_name_); | |
47 } | |
48 | |
45 bool RulesFunction::RunImpl() { | 49 bool RulesFunction::RunImpl() { |
46 std::string event_name; | 50 EXTENSION_FUNCTION_VALIDATE(Init()); |
47 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); | |
48 | 51 |
49 RulesRegistryService* rules_registry_service = | 52 RulesRegistryService* rules_registry_service = |
50 ExtensionSystemFactory::GetForProfile(profile())-> | 53 ExtensionSystemFactory::GetForProfile(profile())-> |
51 rules_registry_service(); | 54 rules_registry_service(); |
52 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); | 55 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name_); |
53 // Raw access to this function is not available to extensions, therefore | 56 // Raw access to this function is not available to extensions, therefore |
54 // there should never be a request for a nonexisting rules registry. | 57 // there should never be a request for a nonexisting rules registry. |
55 EXTENSION_FUNCTION_VALIDATE(rules_registry_); | 58 EXTENSION_FUNCTION_VALIDATE(rules_registry_); |
56 | 59 |
57 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { | 60 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { |
58 bool success = RunImplOnCorrectThread(); | 61 bool success = RunImplOnCorrectThread(); |
59 SendResponse(success); | 62 SendResponse(success); |
60 } else { | 63 } else { |
61 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 64 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
62 content::BrowserThread::GetMessageLoopProxyForThread( | 65 content::BrowserThread::GetMessageLoopProxyForThread( |
63 rules_registry_->GetOwnerThread()); | 66 rules_registry_->GetOwnerThread()); |
64 base::PostTaskAndReplyWithResult( | 67 base::PostTaskAndReplyWithResult( |
65 message_loop_proxy, | 68 message_loop_proxy, |
66 FROM_HERE, | 69 FROM_HERE, |
67 base::Bind(&RulesFunction::RunImplOnCorrectThread, this), | 70 base::Bind(&RulesFunction::RunImplOnCorrectThread, this), |
68 base::Bind(&RulesFunction::SendResponse, this)); | 71 base::Bind(&RulesFunction::SendResponse, this)); |
69 } | 72 } |
70 | 73 |
71 return true; | 74 return true; |
72 } | 75 } |
73 | 76 |
77 bool RulesFunction::Init() { | |
Matt Perry
2012/08/09 01:31:50
optional: I would get rid of the Init() step, and
not at google - send to devlin
2012/08/09 01:41:44
Done.
| |
78 if (!event_name_.empty()) | |
79 return true; | |
80 return args_->GetString(0, &event_name_); | |
81 } | |
82 | |
74 bool AddRulesFunction::RunImplOnCorrectThread() { | 83 bool AddRulesFunction::RunImplOnCorrectThread() { |
75 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); | 84 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
76 EXTENSION_FUNCTION_VALIDATE(params.get()); | 85 EXTENSION_FUNCTION_VALIDATE(params.get()); |
77 | 86 |
78 error_ = rules_registry_->AddRules(extension_id(), params->rules); | 87 error_ = rules_registry_->AddRules(extension_id(), params->rules); |
79 | 88 |
80 if (error_.empty()) | 89 if (error_.empty()) |
81 results_ = AddRules::Results::Create(params->rules); | 90 results_ = AddRules::Results::Create(params->rules); |
82 | 91 |
83 return error_.empty(); | 92 return error_.empty(); |
(...skipping 26 matching lines...) Expand all Loading... | |
110 error_ = rules_registry_->GetAllRules(extension_id(), &rules); | 119 error_ = rules_registry_->GetAllRules(extension_id(), &rules); |
111 } | 120 } |
112 | 121 |
113 if (error_.empty()) | 122 if (error_.empty()) |
114 results_ = GetRules::Results::Create(rules); | 123 results_ = GetRules::Results::Create(rules); |
115 | 124 |
116 return error_.empty(); | 125 return error_.empty(); |
117 } | 126 } |
118 | 127 |
119 } // namespace extensions | 128 } // namespace extensions |
OLD | NEW |