| 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 "extensions/browser/api/declarative/declarative_api.h" | 5 #include "extensions/browser/api/declarative/declarative_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 namespace extensions { | 34 namespace extensions { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 const char kDeclarativeEventPrefix[] = "declarative"; | 38 const char kDeclarativeEventPrefix[] = "declarative"; |
| 39 | 39 |
| 40 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict); | 40 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict); |
| 41 | 41 |
| 42 // Encodes |binary| as base64 and returns a new StringValue populated with the | 42 // Encodes |binary| as base64 and returns a new StringValue populated with the |
| 43 // encoded string. | 43 // encoded string. |
| 44 scoped_ptr<base::StringValue> ConvertBinaryToBase64(base::BinaryValue* binary) { | 44 std::unique_ptr<base::StringValue> ConvertBinaryToBase64( |
| 45 base::BinaryValue* binary) { |
| 45 std::string binary_data = std::string(binary->GetBuffer(), binary->GetSize()); | 46 std::string binary_data = std::string(binary->GetBuffer(), binary->GetSize()); |
| 46 std::string data64; | 47 std::string data64; |
| 47 base::Base64Encode(binary_data, &data64); | 48 base::Base64Encode(binary_data, &data64); |
| 48 return scoped_ptr<base::StringValue>(new base::StringValue(data64)); | 49 return std::unique_ptr<base::StringValue>(new base::StringValue(data64)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // Parses through |args| replacing any BinaryValues with base64 encoded | 52 // Parses through |args| replacing any BinaryValues with base64 encoded |
| 52 // StringValues. Recurses over any nested ListValues, and calls | 53 // StringValues. Recurses over any nested ListValues, and calls |
| 53 // ConvertBinaryDictionaryValuesToBase64 for any nested DictionaryValues. | 54 // ConvertBinaryDictionaryValuesToBase64 for any nested DictionaryValues. |
| 54 void ConvertBinaryListElementsToBase64(base::ListValue* args) { | 55 void ConvertBinaryListElementsToBase64(base::ListValue* args) { |
| 55 size_t index = 0; | 56 size_t index = 0; |
| 56 for (base::ListValue::iterator iter = args->begin(); iter != args->end(); | 57 for (base::ListValue::iterator iter = args->begin(); iter != args->end(); |
| 57 ++iter, ++index) { | 58 ++iter, ++index) { |
| 58 if ((*iter)->IsType(base::Value::TYPE_BINARY)) { | 59 if ((*iter)->IsType(base::Value::TYPE_BINARY)) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 thread_task_runner.get(), FROM_HERE, | 162 thread_task_runner.get(), FROM_HERE, |
| 162 base::Bind(&RulesFunction::RunAsyncOnCorrectThread, this), | 163 base::Bind(&RulesFunction::RunAsyncOnCorrectThread, this), |
| 163 base::Bind(&RulesFunction::SendResponse, this)); | 164 base::Bind(&RulesFunction::SendResponse, this)); |
| 164 } | 165 } |
| 165 | 166 |
| 166 return true; | 167 return true; |
| 167 } | 168 } |
| 168 | 169 |
| 169 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() { | 170 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() { |
| 170 ConvertBinaryListElementsToBase64(args_.get()); | 171 ConvertBinaryListElementsToBase64(args_.get()); |
| 171 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); | 172 std::unique_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| 172 EXTENSION_FUNCTION_VALIDATE(params.get()); | 173 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 173 | 174 |
| 174 // TODO(devlin): Remove the dependency on linked_ptr here. | 175 // TODO(devlin): Remove the dependency on linked_ptr here. |
| 175 std::vector<linked_ptr<api::events::Rule>> linked_rules; | 176 std::vector<linked_ptr<api::events::Rule>> linked_rules; |
| 176 for (api::events::Rule& rule : params->rules) { | 177 for (api::events::Rule& rule : params->rules) { |
| 177 linked_rules.push_back( | 178 linked_rules.push_back( |
| 178 make_linked_ptr(new api::events::Rule(std::move(rule)))); | 179 make_linked_ptr(new api::events::Rule(std::move(rule)))); |
| 179 } | 180 } |
| 180 error_ = rules_registry_->AddRules(extension_id(), linked_rules); | 181 error_ = rules_registry_->AddRules(extension_id(), linked_rules); |
| 181 | 182 |
| 182 if (error_.empty()) { | 183 if (error_.empty()) { |
| 183 scoped_ptr<base::ListValue> rules_value(new base::ListValue()); | 184 std::unique_ptr<base::ListValue> rules_value(new base::ListValue()); |
| 184 for (const auto& rule : linked_rules) | 185 for (const auto& rule : linked_rules) |
| 185 rules_value->Append(rule->ToValue()); | 186 rules_value->Append(rule->ToValue()); |
| 186 SetResult(std::move(rules_value)); | 187 SetResult(std::move(rules_value)); |
| 187 } | 188 } |
| 188 | 189 |
| 189 return error_.empty(); | 190 return error_.empty(); |
| 190 } | 191 } |
| 191 | 192 |
| 192 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() { | 193 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() { |
| 193 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); | 194 std::unique_ptr<RemoveRules::Params> params( |
| 195 RemoveRules::Params::Create(*args_)); |
| 194 EXTENSION_FUNCTION_VALIDATE(params.get()); | 196 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 195 | 197 |
| 196 if (params->rule_identifiers.get()) { | 198 if (params->rule_identifiers.get()) { |
| 197 error_ = rules_registry_->RemoveRules(extension_id(), | 199 error_ = rules_registry_->RemoveRules(extension_id(), |
| 198 *params->rule_identifiers); | 200 *params->rule_identifiers); |
| 199 } else { | 201 } else { |
| 200 error_ = rules_registry_->RemoveAllRules(extension_id()); | 202 error_ = rules_registry_->RemoveAllRules(extension_id()); |
| 201 } | 203 } |
| 202 | 204 |
| 203 return error_.empty(); | 205 return error_.empty(); |
| 204 } | 206 } |
| 205 | 207 |
| 206 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() { | 208 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() { |
| 207 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); | 209 std::unique_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); |
| 208 EXTENSION_FUNCTION_VALIDATE(params.get()); | 210 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 209 | 211 |
| 210 std::vector<linked_ptr<Rule> > rules; | 212 std::vector<linked_ptr<Rule> > rules; |
| 211 if (params->rule_identifiers.get()) { | 213 if (params->rule_identifiers.get()) { |
| 212 rules_registry_->GetRules( | 214 rules_registry_->GetRules( |
| 213 extension_id(), *params->rule_identifiers, &rules); | 215 extension_id(), *params->rule_identifiers, &rules); |
| 214 } else { | 216 } else { |
| 215 rules_registry_->GetAllRules(extension_id(), &rules); | 217 rules_registry_->GetAllRules(extension_id(), &rules); |
| 216 } | 218 } |
| 217 | 219 |
| 218 scoped_ptr<base::ListValue> rules_value(new base::ListValue()); | 220 std::unique_ptr<base::ListValue> rules_value(new base::ListValue()); |
| 219 for (const auto& rule : rules) | 221 for (const auto& rule : rules) |
| 220 rules_value->Append(rule->ToValue()); | 222 rules_value->Append(rule->ToValue()); |
| 221 SetResult(std::move(rules_value)); | 223 SetResult(std::move(rules_value)); |
| 222 | 224 |
| 223 return true; | 225 return true; |
| 224 } | 226 } |
| 225 | 227 |
| 226 } // namespace extensions | 228 } // namespace extensions |
| OLD | NEW |