OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extension_function.h" | 5 #include "extensions/browser/extension_function.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
13 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
14 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
15 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
18 #include "extensions/browser/extension_function_dispatcher.h" | 19 #include "extensions/browser/extension_function_dispatcher.h" |
19 #include "extensions/browser/extension_message_filter.h" | 20 #include "extensions/browser/extension_message_filter.h" |
20 #include "extensions/common/error_utils.h" | 21 #include "extensions/common/error_utils.h" |
21 #include "extensions/common/extension_api.h" | 22 #include "extensions/common/extension_api.h" |
22 #include "extensions/common/extension_messages.h" | 23 #include "extensions/common/extension_messages.h" |
23 | 24 |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
25 using content::RenderViewHost; | 26 using content::RenderViewHost; |
26 using content::WebContents; | 27 using content::WebContents; |
27 using extensions::ErrorUtils; | 28 using extensions::ErrorUtils; |
28 using extensions::ExtensionAPI; | 29 using extensions::ExtensionAPI; |
29 using extensions::Feature; | 30 using extensions::Feature; |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 class ArgumentListResponseValue | 34 class ArgumentListResponseValue |
34 : public ExtensionFunction::ResponseValueObject { | 35 : public ExtensionFunction::ResponseValueObject { |
35 public: | 36 public: |
36 ArgumentListResponseValue(const std::string& function_name, | 37 ArgumentListResponseValue(const std::string& function_name, |
37 const char* title, | 38 const char* title, |
38 ExtensionFunction* function, | 39 ExtensionFunction* function, |
39 scoped_ptr<base::ListValue> result) | 40 std::unique_ptr<base::ListValue> result) |
40 : function_name_(function_name), title_(title) { | 41 : function_name_(function_name), title_(title) { |
41 if (function->GetResultList()) { | 42 if (function->GetResultList()) { |
42 DCHECK_EQ(function->GetResultList(), result.get()) | 43 DCHECK_EQ(function->GetResultList(), result.get()) |
43 << "The result set on this function (" << function_name_ << ") " | 44 << "The result set on this function (" << function_name_ << ") " |
44 << "either by calling SetResult() or directly modifying |result_| is " | 45 << "either by calling SetResult() or directly modifying |result_| is " |
45 << "different to the one passed to " << title_ << "(). " | 46 << "different to the one passed to " << title_ << "(). " |
46 << "The best way to fix this problem is to exclusively use " << title_ | 47 << "The best way to fix this problem is to exclusively use " << title_ |
47 << "(). SetResult() and |result_| are deprecated."; | 48 << "(). SetResult() and |result_| are deprecated."; |
48 } else { | 49 } else { |
49 function->SetResultList(std::move(result)); | 50 function->SetResultList(std::move(result)); |
(...skipping 10 matching lines...) Expand all Loading... |
60 private: | 61 private: |
61 std::string function_name_; | 62 std::string function_name_; |
62 const char* title_; | 63 const char* title_; |
63 }; | 64 }; |
64 | 65 |
65 class ErrorWithArgumentsResponseValue : public ArgumentListResponseValue { | 66 class ErrorWithArgumentsResponseValue : public ArgumentListResponseValue { |
66 public: | 67 public: |
67 ErrorWithArgumentsResponseValue(const std::string& function_name, | 68 ErrorWithArgumentsResponseValue(const std::string& function_name, |
68 const char* title, | 69 const char* title, |
69 ExtensionFunction* function, | 70 ExtensionFunction* function, |
70 scoped_ptr<base::ListValue> result, | 71 std::unique_ptr<base::ListValue> result, |
71 const std::string& error) | 72 const std::string& error) |
72 : ArgumentListResponseValue(function_name, | 73 : ArgumentListResponseValue(function_name, |
73 title, | 74 title, |
74 function, | 75 function, |
75 std::move(result)) { | 76 std::move(result)) { |
76 function->SetError(error); | 77 function->SetError(error); |
77 } | 78 } |
78 | 79 |
79 ~ErrorWithArgumentsResponseValue() override {} | 80 ~ErrorWithArgumentsResponseValue() override {} |
80 | 81 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 void ExtensionFunction::SetArgs(const base::ListValue* args) { | 256 void ExtensionFunction::SetArgs(const base::ListValue* args) { |
256 DCHECK(!args_.get()); // Should only be called once. | 257 DCHECK(!args_.get()); // Should only be called once. |
257 args_.reset(args->DeepCopy()); | 258 args_.reset(args->DeepCopy()); |
258 } | 259 } |
259 | 260 |
260 void ExtensionFunction::SetResult(base::Value* result) { | 261 void ExtensionFunction::SetResult(base::Value* result) { |
261 results_.reset(new base::ListValue()); | 262 results_.reset(new base::ListValue()); |
262 results_->Append(result); | 263 results_->Append(result); |
263 } | 264 } |
264 | 265 |
265 void ExtensionFunction::SetResult(scoped_ptr<base::Value> result) { | 266 void ExtensionFunction::SetResult(std::unique_ptr<base::Value> result) { |
266 results_.reset(new base::ListValue()); | 267 results_.reset(new base::ListValue()); |
267 results_->Append(std::move(result)); | 268 results_->Append(std::move(result)); |
268 } | 269 } |
269 | 270 |
270 void ExtensionFunction::SetResultList(scoped_ptr<base::ListValue> results) { | 271 void ExtensionFunction::SetResultList( |
| 272 std::unique_ptr<base::ListValue> results) { |
271 results_ = std::move(results); | 273 results_ = std::move(results); |
272 } | 274 } |
273 | 275 |
274 const base::ListValue* ExtensionFunction::GetResultList() const { | 276 const base::ListValue* ExtensionFunction::GetResultList() const { |
275 return results_.get(); | 277 return results_.get(); |
276 } | 278 } |
277 | 279 |
278 std::string ExtensionFunction::GetError() const { | 280 std::string ExtensionFunction::GetError() const { |
279 return error_; | 281 return error_; |
280 } | 282 } |
281 | 283 |
282 void ExtensionFunction::SetError(const std::string& error) { | 284 void ExtensionFunction::SetError(const std::string& error) { |
283 error_ = error; | 285 error_ = error; |
284 } | 286 } |
285 | 287 |
286 bool ExtensionFunction::user_gesture() const { | 288 bool ExtensionFunction::user_gesture() const { |
287 return user_gesture_ || UserGestureForTests::GetInstance()->HaveGesture(); | 289 return user_gesture_ || UserGestureForTests::GetInstance()->HaveGesture(); |
288 } | 290 } |
289 | 291 |
290 ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() { | 292 ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() { |
291 return ResponseValue(new ArgumentListResponseValue( | 293 return ResponseValue(new ArgumentListResponseValue( |
292 name(), "NoArguments", this, make_scoped_ptr(new base::ListValue()))); | 294 name(), "NoArguments", this, base::WrapUnique(new base::ListValue()))); |
293 } | 295 } |
294 | 296 |
295 ExtensionFunction::ResponseValue ExtensionFunction::OneArgument( | 297 ExtensionFunction::ResponseValue ExtensionFunction::OneArgument( |
296 base::Value* arg) { | 298 base::Value* arg) { |
297 scoped_ptr<base::ListValue> args(new base::ListValue()); | 299 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
298 args->Append(arg); | 300 args->Append(arg); |
299 return ResponseValue(new ArgumentListResponseValue(name(), "OneArgument", | 301 return ResponseValue(new ArgumentListResponseValue(name(), "OneArgument", |
300 this, std::move(args))); | 302 this, std::move(args))); |
301 } | 303 } |
302 | 304 |
303 ExtensionFunction::ResponseValue ExtensionFunction::OneArgument( | 305 ExtensionFunction::ResponseValue ExtensionFunction::OneArgument( |
304 scoped_ptr<base::Value> arg) { | 306 std::unique_ptr<base::Value> arg) { |
305 return OneArgument(arg.release()); | 307 return OneArgument(arg.release()); |
306 } | 308 } |
307 | 309 |
308 ExtensionFunction::ResponseValue ExtensionFunction::TwoArguments( | 310 ExtensionFunction::ResponseValue ExtensionFunction::TwoArguments( |
309 base::Value* arg1, | 311 base::Value* arg1, |
310 base::Value* arg2) { | 312 base::Value* arg2) { |
311 scoped_ptr<base::ListValue> args(new base::ListValue()); | 313 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
312 args->Append(arg1); | 314 args->Append(arg1); |
313 args->Append(arg2); | 315 args->Append(arg2); |
314 return ResponseValue(new ArgumentListResponseValue(name(), "TwoArguments", | 316 return ResponseValue(new ArgumentListResponseValue(name(), "TwoArguments", |
315 this, std::move(args))); | 317 this, std::move(args))); |
316 } | 318 } |
317 | 319 |
318 ExtensionFunction::ResponseValue ExtensionFunction::ArgumentList( | 320 ExtensionFunction::ResponseValue ExtensionFunction::ArgumentList( |
319 scoped_ptr<base::ListValue> args) { | 321 std::unique_ptr<base::ListValue> args) { |
320 return ResponseValue(new ArgumentListResponseValue(name(), "ArgumentList", | 322 return ResponseValue(new ArgumentListResponseValue(name(), "ArgumentList", |
321 this, std::move(args))); | 323 this, std::move(args))); |
322 } | 324 } |
323 | 325 |
324 ExtensionFunction::ResponseValue ExtensionFunction::Error( | 326 ExtensionFunction::ResponseValue ExtensionFunction::Error( |
325 const std::string& error) { | 327 const std::string& error) { |
326 return ResponseValue(new ErrorResponseValue(this, error)); | 328 return ResponseValue(new ErrorResponseValue(this, error)); |
327 } | 329 } |
328 | 330 |
329 ExtensionFunction::ResponseValue ExtensionFunction::Error( | 331 ExtensionFunction::ResponseValue ExtensionFunction::Error( |
(...skipping 14 matching lines...) Expand all Loading... |
344 ExtensionFunction::ResponseValue ExtensionFunction::Error( | 346 ExtensionFunction::ResponseValue ExtensionFunction::Error( |
345 const std::string& format, | 347 const std::string& format, |
346 const std::string& s1, | 348 const std::string& s1, |
347 const std::string& s2, | 349 const std::string& s2, |
348 const std::string& s3) { | 350 const std::string& s3) { |
349 return ResponseValue(new ErrorResponseValue( | 351 return ResponseValue(new ErrorResponseValue( |
350 this, ErrorUtils::FormatErrorMessage(format, s1, s2, s3))); | 352 this, ErrorUtils::FormatErrorMessage(format, s1, s2, s3))); |
351 } | 353 } |
352 | 354 |
353 ExtensionFunction::ResponseValue ExtensionFunction::ErrorWithArguments( | 355 ExtensionFunction::ResponseValue ExtensionFunction::ErrorWithArguments( |
354 scoped_ptr<base::ListValue> args, | 356 std::unique_ptr<base::ListValue> args, |
355 const std::string& error) { | 357 const std::string& error) { |
356 return ResponseValue(new ErrorWithArgumentsResponseValue( | 358 return ResponseValue(new ErrorWithArgumentsResponseValue( |
357 name(), "ErrorWithArguments", this, std::move(args), error)); | 359 name(), "ErrorWithArguments", this, std::move(args), error)); |
358 } | 360 } |
359 | 361 |
360 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { | 362 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { |
361 return ResponseValue(new BadMessageResponseValue(this)); | 363 return ResponseValue(new BadMessageResponseValue(this)); |
362 } | 364 } |
363 | 365 |
364 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( | 366 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { | 559 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { |
558 return RespondNow(RunSync() ? ArgumentList(std::move(results_)) | 560 return RespondNow(RunSync() ? ArgumentList(std::move(results_)) |
559 : Error(error_)); | 561 : Error(error_)); |
560 } | 562 } |
561 | 563 |
562 // static | 564 // static |
563 bool SyncIOThreadExtensionFunction::ValidationFailure( | 565 bool SyncIOThreadExtensionFunction::ValidationFailure( |
564 SyncIOThreadExtensionFunction* function) { | 566 SyncIOThreadExtensionFunction* function) { |
565 return false; | 567 return false; |
566 } | 568 } |
OLD | NEW |