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 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 5 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 // | 390 // |
391 // More specifically: call this iff Run() has already executed, it returned | 391 // More specifically: call this iff Run() has already executed, it returned |
392 // RespondLater(), and Respond(...) hasn't already been called. | 392 // RespondLater(), and Respond(...) hasn't already been called. |
393 void Respond(ResponseValue result); | 393 void Respond(ResponseValue result); |
394 | 394 |
395 virtual ~ExtensionFunction(); | 395 virtual ~ExtensionFunction(); |
396 | 396 |
397 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object. | 397 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object. |
398 virtual void Destruct() const = 0; | 398 virtual void Destruct() const = 0; |
399 | 399 |
400 // Do not call this function directly, return the appropriate ResponseAction | 400 // Called after the response is sent, allowing the function to perform any |
401 // from Run() instead. If using RespondLater then call Respond(). | 401 // additional work or cleanup. |
402 // | 402 virtual void OnResponded() {} |
403 // Call with true to indicate success, false to indicate failure, in which | |
404 // case please set |error_|. | |
405 virtual void SendResponse(bool success) = 0; | |
406 | |
407 // Common implementation for SendResponse. | |
408 void SendResponseImpl(bool success); | |
409 | 403 |
410 // Return true if the argument to this function at |index| was provided and | 404 // Return true if the argument to this function at |index| was provided and |
411 // is non-null. | 405 // is non-null. |
412 bool HasOptionalArgument(size_t index); | 406 bool HasOptionalArgument(size_t index); |
413 | 407 |
414 // Id of this request, used to map the response back to the caller. | 408 // Id of this request, used to map the response back to the caller. |
415 int request_id_; | 409 int request_id_; |
416 | 410 |
417 // The id of the profile of this function's extension. | 411 // The id of the profile of this function's extension. |
418 void* profile_id_; | 412 void* profile_id_; |
(...skipping 16 matching lines...) Expand all Loading... |
435 // mode extension, this will always be false, and we will limit access to | 429 // mode extension, this will always be false, and we will limit access to |
436 // data from within the same profile_ (either incognito or not). | 430 // data from within the same profile_ (either incognito or not). |
437 bool include_incognito_; | 431 bool include_incognito_; |
438 | 432 |
439 // True if the call was made in response of user gesture. | 433 // True if the call was made in response of user gesture. |
440 bool user_gesture_; | 434 bool user_gesture_; |
441 | 435 |
442 // The arguments to the API. Only non-null if argument were specified. | 436 // The arguments to the API. Only non-null if argument were specified. |
443 std::unique_ptr<base::ListValue> args_; | 437 std::unique_ptr<base::ListValue> args_; |
444 | 438 |
445 // The results of the API. This should be populated by the derived class | 439 // The results of the API. This should be populated through the Respond()/ |
446 // before SendResponse() is called. | 440 // RespondNow() methods. In legacy implementations, this is set directly, and |
| 441 // should be set before calling SendResponse(). |
447 std::unique_ptr<base::ListValue> results_; | 442 std::unique_ptr<base::ListValue> results_; |
448 | 443 |
449 // Any detailed error from the API. This should be populated by the derived | 444 // Any detailed error from the API. This should be populated by the derived |
450 // class before Run() returns. | 445 // class before Run() returns. |
451 std::string error_; | 446 std::string error_; |
452 | 447 |
453 // Any class that gets a malformed message should set this to true before | 448 // Any class that gets a malformed message should set this to true before |
454 // returning. Usually we want to kill the message sending process. | 449 // returning. Usually we want to kill the message sending process. |
455 bool bad_message_; | 450 bool bad_message_; |
456 | 451 |
(...skipping 11 matching lines...) Expand all Loading... |
468 extensions::Feature::Context source_context_type_; | 463 extensions::Feature::Context source_context_type_; |
469 | 464 |
470 // The process ID of the page that triggered this function call, or -1 | 465 // The process ID of the page that triggered this function call, or -1 |
471 // if unknown. | 466 // if unknown. |
472 int source_process_id_; | 467 int source_process_id_; |
473 | 468 |
474 // Whether this function has responded. | 469 // Whether this function has responded. |
475 bool did_respond_; | 470 bool did_respond_; |
476 | 471 |
477 private: | 472 private: |
| 473 // Call with true to indicate success, false to indicate failure. If this |
| 474 // failed, |error_| should be set. |
| 475 void SendResponseImpl(bool success); |
| 476 |
478 base::ElapsedTimer timer_; | 477 base::ElapsedTimer timer_; |
479 | 478 |
480 // The response type of the function, if the response has been sent. | 479 // The response type of the function, if the response has been sent. |
481 std::unique_ptr<ResponseType> response_type_; | 480 std::unique_ptr<ResponseType> response_type_; |
482 | 481 |
483 void OnRespondingLater(ResponseValue response); | |
484 | |
485 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 482 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
486 }; | 483 }; |
487 | 484 |
488 // Extension functions that run on the UI thread. Most functions fall into | 485 // Extension functions that run on the UI thread. Most functions fall into |
489 // this category. | 486 // this category. |
490 class UIThreadExtensionFunction : public ExtensionFunction { | 487 class UIThreadExtensionFunction : public ExtensionFunction { |
491 public: | 488 public: |
492 UIThreadExtensionFunction(); | 489 UIThreadExtensionFunction(); |
493 | 490 |
494 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; | 491 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 // Emits a message to the extension's devtools console. | 534 // Emits a message to the extension's devtools console. |
538 void WriteToConsole(content::ConsoleMessageLevel level, | 535 void WriteToConsole(content::ConsoleMessageLevel level, |
539 const std::string& message); | 536 const std::string& message); |
540 | 537 |
541 friend struct content::BrowserThread::DeleteOnThread< | 538 friend struct content::BrowserThread::DeleteOnThread< |
542 content::BrowserThread::UI>; | 539 content::BrowserThread::UI>; |
543 friend class base::DeleteHelper<UIThreadExtensionFunction>; | 540 friend class base::DeleteHelper<UIThreadExtensionFunction>; |
544 | 541 |
545 ~UIThreadExtensionFunction() override; | 542 ~UIThreadExtensionFunction() override; |
546 | 543 |
547 void SendResponse(bool success) override; | 544 void OnResponded() override; |
548 | 545 |
549 // Sets the Blob UUIDs whose ownership is being transferred to the renderer. | 546 // Sets the Blob UUIDs whose ownership is being transferred to the renderer. |
550 void SetTransferredBlobUUIDs(const std::vector<std::string>& blob_uuids); | 547 void SetTransferredBlobUUIDs(const std::vector<std::string>& blob_uuids); |
551 | 548 |
552 // The BrowserContext of this function's extension. | 549 // The BrowserContext of this function's extension. |
553 // TODO(devlin): Grr... protected members. Move this to be private. | 550 // TODO(devlin): Grr... protected members. Move this to be private. |
554 content::BrowserContext* context_; | 551 content::BrowserContext* context_; |
555 | 552 |
556 private: | 553 private: |
557 class RenderFrameHostTracker; | 554 class RenderFrameHostTracker; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 | 608 |
612 protected: | 609 protected: |
613 friend struct content::BrowserThread::DeleteOnThread< | 610 friend struct content::BrowserThread::DeleteOnThread< |
614 content::BrowserThread::IO>; | 611 content::BrowserThread::IO>; |
615 friend class base::DeleteHelper<IOThreadExtensionFunction>; | 612 friend class base::DeleteHelper<IOThreadExtensionFunction>; |
616 | 613 |
617 ~IOThreadExtensionFunction() override; | 614 ~IOThreadExtensionFunction() override; |
618 | 615 |
619 void Destruct() const override; | 616 void Destruct() const override; |
620 | 617 |
621 void SendResponse(bool success) override; | |
622 | |
623 private: | 618 private: |
624 base::WeakPtr<extensions::IOThreadExtensionMessageFilter> ipc_sender_; | 619 base::WeakPtr<extensions::IOThreadExtensionMessageFilter> ipc_sender_; |
625 int routing_id_; | 620 int routing_id_; |
626 | 621 |
627 scoped_refptr<const extensions::InfoMap> extension_info_map_; | 622 scoped_refptr<const extensions::InfoMap> extension_info_map_; |
628 | 623 |
629 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); | 624 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); |
630 }; | 625 }; |
631 | 626 |
632 // Base class for an extension function that runs asynchronously *relative to | 627 // Base class for an extension function that runs asynchronously *relative to |
633 // the browser's UI thread*. | 628 // the browser's UI thread*. |
634 class AsyncExtensionFunction : public UIThreadExtensionFunction { | 629 class AsyncExtensionFunction : public UIThreadExtensionFunction { |
635 public: | 630 public: |
636 AsyncExtensionFunction(); | 631 AsyncExtensionFunction(); |
637 | 632 |
638 protected: | 633 protected: |
639 ~AsyncExtensionFunction() override; | 634 ~AsyncExtensionFunction() override; |
640 | 635 |
641 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. | 636 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. |
642 // | 637 // |
643 // AsyncExtensionFunctions implement this method. Return true to indicate that | 638 // AsyncExtensionFunctions implement this method. Return true to indicate that |
644 // nothing has gone wrong yet; SendResponse must be called later. Return false | 639 // nothing has gone wrong yet; SendResponse must be called later. Return false |
645 // to respond immediately with an error. | 640 // to respond immediately with an error. |
646 virtual bool RunAsync() = 0; | 641 virtual bool RunAsync() = 0; |
647 | 642 |
648 // ValidationFailure override to match RunAsync(). | 643 // ValidationFailure override to match RunAsync(). |
649 static bool ValidationFailure(AsyncExtensionFunction* function); | 644 static bool ValidationFailure(AsyncExtensionFunction* function); |
650 | 645 |
| 646 // Responds with success/failure. |results_| or |error_| should be set |
| 647 // accordingly. |
| 648 void SendResponse(bool success); |
| 649 |
651 private: | 650 private: |
652 // If you're hitting a compile error here due to "final" - great! You're | 651 // If you're hitting a compile error here due to "final" - great! You're |
653 // doing the right thing, you just need to extend UIThreadExtensionFunction | 652 // doing the right thing, you just need to extend UIThreadExtensionFunction |
654 // instead of AsyncExtensionFunction. | 653 // instead of AsyncExtensionFunction. |
655 ResponseAction Run() final; | 654 ResponseAction Run() final; |
656 | 655 |
657 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); | 656 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); |
658 }; | 657 }; |
659 | 658 |
660 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 659 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
OLD | NEW |