Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/common/extensions/manifest_handlers/automation.cc

Issue 1880143002: Convert chrome/common to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/common/extensions/manifest_handlers/automation.h" 5 #include "chrome/common/extensions/manifest_handlers/automation.h"
6 6
7 #include <memory>
7 #include <utility> 8 #include <utility>
8 9
10 #include "base/memory/ptr_util.h"
9 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/common/extensions/api/manifest_types.h" 12 #include "chrome/common/extensions/api/manifest_types.h"
11 #include "chrome/grit/generated_resources.h" 13 #include "chrome/grit/generated_resources.h"
12 #include "extensions/common/error_utils.h" 14 #include "extensions/common/error_utils.h"
13 #include "extensions/common/extensions_client.h" 15 #include "extensions/common/extensions_client.h"
14 #include "extensions/common/manifest_constants.h" 16 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/permissions/api_permission_set.h" 17 #include "extensions/common/permissions/api_permission_set.h"
16 #include "extensions/common/permissions/manifest_permission.h" 18 #include "extensions/common/permissions/manifest_permission.h"
17 #include "extensions/common/permissions/permission_message_util.h" 19 #include "extensions/common/permissions/permission_message_util.h"
18 #include "extensions/common/permissions/permissions_data.h" 20 #include "extensions/common/permissions/permissions_data.h"
(...skipping 15 matching lines...) Expand all
34 const char kErrorNoMatchesProvided[] = "No valid match patterns provided."; 36 const char kErrorNoMatchesProvided[] = "No valid match patterns provided.";
35 } 37 }
36 38
37 namespace errors = manifest_errors; 39 namespace errors = manifest_errors;
38 namespace keys = extensions::manifest_keys; 40 namespace keys = extensions::manifest_keys;
39 using api::manifest_types::Automation; 41 using api::manifest_types::Automation;
40 42
41 class AutomationManifestPermission : public ManifestPermission { 43 class AutomationManifestPermission : public ManifestPermission {
42 public: 44 public:
43 explicit AutomationManifestPermission( 45 explicit AutomationManifestPermission(
44 scoped_ptr<const AutomationInfo> automation_info) 46 std::unique_ptr<const AutomationInfo> automation_info)
45 : automation_info_(std::move(automation_info)) {} 47 : automation_info_(std::move(automation_info)) {}
46 48
47 // extensions::ManifestPermission overrides. 49 // extensions::ManifestPermission overrides.
48 std::string name() const override; 50 std::string name() const override;
49 51
50 std::string id() const override; 52 std::string id() const override;
51 53
52 PermissionIDSet GetPermissions() const override; 54 PermissionIDSet GetPermissions() const override;
53 55
54 bool FromValue(const base::Value* value) override; 56 bool FromValue(const base::Value* value) override;
55 57
56 scoped_ptr<base::Value> ToValue() const override; 58 std::unique_ptr<base::Value> ToValue() const override;
57 59
58 ManifestPermission* Diff(const ManifestPermission* rhs) const override; 60 ManifestPermission* Diff(const ManifestPermission* rhs) const override;
59 61
60 ManifestPermission* Union(const ManifestPermission* rhs) const override; 62 ManifestPermission* Union(const ManifestPermission* rhs) const override;
61 63
62 ManifestPermission* Intersect(const ManifestPermission* rhs) const override; 64 ManifestPermission* Intersect(const ManifestPermission* rhs) const override;
63 65
64 private: 66 private:
65 scoped_ptr<const AutomationInfo> automation_info_; 67 std::unique_ptr<const AutomationInfo> automation_info_;
66 }; 68 };
67 69
68 std::string AutomationManifestPermission::name() const { 70 std::string AutomationManifestPermission::name() const {
69 return keys::kAutomation; 71 return keys::kAutomation;
70 } 72 }
71 73
72 std::string AutomationManifestPermission::id() const { 74 std::string AutomationManifestPermission::id() const {
73 return keys::kAutomation; 75 return keys::kAutomation;
74 } 76 }
75 77
(...skipping 25 matching lines...) Expand all
101 } 103 }
102 104
103 bool AutomationManifestPermission::FromValue(const base::Value* value) { 105 bool AutomationManifestPermission::FromValue(const base::Value* value) {
104 base::string16 error; 106 base::string16 error;
105 automation_info_.reset(AutomationInfo::FromValue(*value, 107 automation_info_.reset(AutomationInfo::FromValue(*value,
106 NULL /* install_warnings */, 108 NULL /* install_warnings */,
107 &error).release()); 109 &error).release());
108 return error.empty(); 110 return error.empty();
109 } 111 }
110 112
111 scoped_ptr<base::Value> AutomationManifestPermission::ToValue() const { 113 std::unique_ptr<base::Value> AutomationManifestPermission::ToValue() const {
112 return AutomationInfo::ToValue(*automation_info_); 114 return AutomationInfo::ToValue(*automation_info_);
113 } 115 }
114 116
115 ManifestPermission* AutomationManifestPermission::Diff( 117 ManifestPermission* AutomationManifestPermission::Diff(
116 const ManifestPermission* rhs) const { 118 const ManifestPermission* rhs) const {
117 const AutomationManifestPermission* other = 119 const AutomationManifestPermission* other =
118 static_cast<const AutomationManifestPermission*>(rhs); 120 static_cast<const AutomationManifestPermission*>(rhs);
119 121
120 bool desktop = automation_info_->desktop && !other->automation_info_->desktop; 122 bool desktop = automation_info_->desktop && !other->automation_info_->desktop;
121 bool interact = 123 bool interact =
122 automation_info_->interact && !other->automation_info_->interact; 124 automation_info_->interact && !other->automation_info_->interact;
123 URLPatternSet matches = URLPatternSet::CreateDifference( 125 URLPatternSet matches = URLPatternSet::CreateDifference(
124 automation_info_->matches, other->automation_info_->matches); 126 automation_info_->matches, other->automation_info_->matches);
125 return new AutomationManifestPermission( 127 return new AutomationManifestPermission(
126 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); 128 base::WrapUnique(new const AutomationInfo(desktop, matches, interact)));
127 } 129 }
128 130
129 ManifestPermission* AutomationManifestPermission::Union( 131 ManifestPermission* AutomationManifestPermission::Union(
130 const ManifestPermission* rhs) const { 132 const ManifestPermission* rhs) const {
131 const AutomationManifestPermission* other = 133 const AutomationManifestPermission* other =
132 static_cast<const AutomationManifestPermission*>(rhs); 134 static_cast<const AutomationManifestPermission*>(rhs);
133 135
134 bool desktop = automation_info_->desktop || other->automation_info_->desktop; 136 bool desktop = automation_info_->desktop || other->automation_info_->desktop;
135 bool interact = 137 bool interact =
136 automation_info_->interact || other->automation_info_->interact; 138 automation_info_->interact || other->automation_info_->interact;
137 URLPatternSet matches = URLPatternSet::CreateUnion( 139 URLPatternSet matches = URLPatternSet::CreateUnion(
138 automation_info_->matches, other->automation_info_->matches); 140 automation_info_->matches, other->automation_info_->matches);
139 return new AutomationManifestPermission( 141 return new AutomationManifestPermission(
140 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); 142 base::WrapUnique(new const AutomationInfo(desktop, matches, interact)));
141 } 143 }
142 144
143 ManifestPermission* AutomationManifestPermission::Intersect( 145 ManifestPermission* AutomationManifestPermission::Intersect(
144 const ManifestPermission* rhs) const { 146 const ManifestPermission* rhs) const {
145 const AutomationManifestPermission* other = 147 const AutomationManifestPermission* other =
146 static_cast<const AutomationManifestPermission*>(rhs); 148 static_cast<const AutomationManifestPermission*>(rhs);
147 149
148 bool desktop = automation_info_->desktop && other->automation_info_->desktop; 150 bool desktop = automation_info_->desktop && other->automation_info_->desktop;
149 bool interact = 151 bool interact =
150 automation_info_->interact && other->automation_info_->interact; 152 automation_info_->interact && other->automation_info_->interact;
151 URLPatternSet matches = URLPatternSet::CreateIntersection( 153 URLPatternSet matches = URLPatternSet::CreateIntersection(
152 automation_info_->matches, other->automation_info_->matches); 154 automation_info_->matches, other->automation_info_->matches);
153 return new AutomationManifestPermission( 155 return new AutomationManifestPermission(
154 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); 156 base::WrapUnique(new const AutomationInfo(desktop, matches, interact)));
155 } 157 }
156 158
157 AutomationHandler::AutomationHandler() { 159 AutomationHandler::AutomationHandler() {
158 } 160 }
159 161
160 AutomationHandler::~AutomationHandler() { 162 AutomationHandler::~AutomationHandler() {
161 } 163 }
162 164
163 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { 165 bool AutomationHandler::Parse(Extension* extension, base::string16* error) {
164 const base::Value* automation = NULL; 166 const base::Value* automation = NULL;
165 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); 167 CHECK(extension->manifest()->Get(keys::kAutomation, &automation));
166 std::vector<InstallWarning> install_warnings; 168 std::vector<InstallWarning> install_warnings;
167 scoped_ptr<AutomationInfo> info = 169 std::unique_ptr<AutomationInfo> info =
168 AutomationInfo::FromValue(*automation, &install_warnings, error); 170 AutomationInfo::FromValue(*automation, &install_warnings, error);
169 if (!error->empty()) 171 if (!error->empty())
170 return false; 172 return false;
171 173
172 extension->AddInstallWarnings(install_warnings); 174 extension->AddInstallWarnings(install_warnings);
173 175
174 if (!info) 176 if (!info)
175 return true; 177 return true;
176 178
177 extension->SetManifestData(keys::kAutomation, info.release()); 179 extension->SetManifestData(keys::kAutomation, info.release());
178 return true; 180 return true;
179 } 181 }
180 182
181 const std::vector<std::string> AutomationHandler::Keys() const { 183 const std::vector<std::string> AutomationHandler::Keys() const {
182 return SingleKey(keys::kAutomation); 184 return SingleKey(keys::kAutomation);
183 } 185 }
184 186
185 ManifestPermission* AutomationHandler::CreatePermission() { 187 ManifestPermission* AutomationHandler::CreatePermission() {
186 return new AutomationManifestPermission( 188 return new AutomationManifestPermission(
187 make_scoped_ptr(new const AutomationInfo)); 189 base::WrapUnique(new const AutomationInfo));
188 } 190 }
189 191
190 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission( 192 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission(
191 const Extension* extension) { 193 const Extension* extension) {
192 const AutomationInfo* info = AutomationInfo::Get(extension); 194 const AutomationInfo* info = AutomationInfo::Get(extension);
193 if (info) { 195 if (info) {
194 return new AutomationManifestPermission( 196 return new AutomationManifestPermission(
195 make_scoped_ptr(new const AutomationInfo( 197 base::WrapUnique(new const AutomationInfo(info->desktop, info->matches,
196 info->desktop, info->matches, info->interact))); 198 info->interact)));
197 } 199 }
198 return NULL; 200 return NULL;
199 } 201 }
200 202
201 // static 203 // static
202 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { 204 const AutomationInfo* AutomationInfo::Get(const Extension* extension) {
203 return static_cast<AutomationInfo*>( 205 return static_cast<AutomationInfo*>(
204 extension->GetManifestData(keys::kAutomation)); 206 extension->GetManifestData(keys::kAutomation));
205 } 207 }
206 208
207 // static 209 // static
208 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( 210 std::unique_ptr<AutomationInfo> AutomationInfo::FromValue(
209 const base::Value& value, 211 const base::Value& value,
210 std::vector<InstallWarning>* install_warnings, 212 std::vector<InstallWarning>* install_warnings,
211 base::string16* error) { 213 base::string16* error) {
212 scoped_ptr<Automation> automation = Automation::FromValue(value, error); 214 std::unique_ptr<Automation> automation = Automation::FromValue(value, error);
213 if (!automation) 215 if (!automation)
214 return scoped_ptr<AutomationInfo>(); 216 return std::unique_ptr<AutomationInfo>();
215 217
216 if (automation->as_boolean) { 218 if (automation->as_boolean) {
217 if (*automation->as_boolean) 219 if (*automation->as_boolean)
218 return make_scoped_ptr(new AutomationInfo()); 220 return base::WrapUnique(new AutomationInfo());
219 return scoped_ptr<AutomationInfo>(); 221 return std::unique_ptr<AutomationInfo>();
220 } 222 }
221 const Automation::Object& automation_object = *automation->as_object; 223 const Automation::Object& automation_object = *automation->as_object;
222 224
223 bool desktop = false; 225 bool desktop = false;
224 bool interact = false; 226 bool interact = false;
225 if (automation_object.desktop && *automation_object.desktop) { 227 if (automation_object.desktop && *automation_object.desktop) {
226 desktop = true; 228 desktop = true;
227 interact = true; 229 interact = true;
228 if (automation_object.interact && !*automation_object.interact) { 230 if (automation_object.interact && !*automation_object.interact) {
229 // TODO(aboxhall): Do we want to allow this? 231 // TODO(aboxhall): Do we want to allow this?
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 267
266 matches.AddPattern(pattern); 268 matches.AddPattern(pattern);
267 } 269 }
268 } 270 }
269 } 271 }
270 if (specified_matches && matches.is_empty()) { 272 if (specified_matches && matches.is_empty()) {
271 install_warnings->push_back( 273 install_warnings->push_back(
272 InstallWarning(automation_errors::kErrorNoMatchesProvided)); 274 InstallWarning(automation_errors::kErrorNoMatchesProvided));
273 } 275 }
274 276
275 return make_scoped_ptr(new AutomationInfo(desktop, matches, interact)); 277 return base::WrapUnique(new AutomationInfo(desktop, matches, interact));
276 } 278 }
277 279
278 // static 280 // static
279 scoped_ptr<base::Value> AutomationInfo::ToValue(const AutomationInfo& info) { 281 std::unique_ptr<base::Value> AutomationInfo::ToValue(
282 const AutomationInfo& info) {
280 return AsManifestType(info)->ToValue(); 283 return AsManifestType(info)->ToValue();
281 } 284 }
282 285
283 // static 286 // static
284 scoped_ptr<Automation> AutomationInfo::AsManifestType( 287 std::unique_ptr<Automation> AutomationInfo::AsManifestType(
285 const AutomationInfo& info) { 288 const AutomationInfo& info) {
286 scoped_ptr<Automation> automation(new Automation); 289 std::unique_ptr<Automation> automation(new Automation);
287 if (!info.desktop && !info.interact && info.matches.size() == 0) { 290 if (!info.desktop && !info.interact && info.matches.size() == 0) {
288 automation->as_boolean.reset(new bool(true)); 291 automation->as_boolean.reset(new bool(true));
289 return automation; 292 return automation;
290 } 293 }
291 294
292 Automation::Object* as_object = new Automation::Object; 295 Automation::Object* as_object = new Automation::Object;
293 as_object->desktop.reset(new bool(info.desktop)); 296 as_object->desktop.reset(new bool(info.desktop));
294 as_object->interact.reset(new bool(info.interact)); 297 as_object->interact.reset(new bool(info.interact));
295 if (info.matches.size() > 0) { 298 if (info.matches.size() > 0) {
296 as_object->matches.reset(info.matches.ToStringVector().release()); 299 as_object->matches.reset(info.matches.ToStringVector().release());
297 } 300 }
298 automation->as_object.reset(as_object); 301 automation->as_object.reset(as_object);
299 return automation; 302 return automation;
300 } 303 }
301 304
302 AutomationInfo::AutomationInfo() : desktop(false), interact(false) { 305 AutomationInfo::AutomationInfo() : desktop(false), interact(false) {
303 } 306 }
304 307
305 AutomationInfo::AutomationInfo(bool desktop, 308 AutomationInfo::AutomationInfo(bool desktop,
306 const URLPatternSet matches, 309 const URLPatternSet matches,
307 bool interact) 310 bool interact)
308 : desktop(desktop), matches(matches), interact(interact) { 311 : desktop(desktop), matches(matches), interact(interact) {
309 } 312 }
310 313
311 AutomationInfo::~AutomationInfo() { 314 AutomationInfo::~AutomationInfo() {
312 } 315 }
313 316
314 } // namespace extensions 317 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698