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 thre response is sent, allowing the function to perform any |
lazyboy
2016/09/20 20:21:33
s/thre/the
Devlin
2016/09/20 21:51:04
Done.
| |
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); | 482 void OnRespondingLater(ResponseValue response); |
484 | 483 |
485 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 484 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
486 }; | 485 }; |
487 | 486 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 // Emits a message to the extension's devtools console. | 536 // Emits a message to the extension's devtools console. |
538 void WriteToConsole(content::ConsoleMessageLevel level, | 537 void WriteToConsole(content::ConsoleMessageLevel level, |
539 const std::string& message); | 538 const std::string& message); |
540 | 539 |
541 friend struct content::BrowserThread::DeleteOnThread< | 540 friend struct content::BrowserThread::DeleteOnThread< |
542 content::BrowserThread::UI>; | 541 content::BrowserThread::UI>; |
543 friend class base::DeleteHelper<UIThreadExtensionFunction>; | 542 friend class base::DeleteHelper<UIThreadExtensionFunction>; |
544 | 543 |
545 ~UIThreadExtensionFunction() override; | 544 ~UIThreadExtensionFunction() override; |
546 | 545 |
547 void SendResponse(bool success) override; | 546 void OnResponded() override; |
548 | 547 |
549 // Sets the Blob UUIDs whose ownership is being transferred to the renderer. | 548 // Sets the Blob UUIDs whose ownership is being transferred to the renderer. |
550 void SetTransferredBlobUUIDs(const std::vector<std::string>& blob_uuids); | 549 void SetTransferredBlobUUIDs(const std::vector<std::string>& blob_uuids); |
551 | 550 |
552 // The BrowserContext of this function's extension. | 551 // The BrowserContext of this function's extension. |
553 // TODO(devlin): Grr... protected members. Move this to be private. | 552 // TODO(devlin): Grr... protected members. Move this to be private. |
554 content::BrowserContext* context_; | 553 content::BrowserContext* context_; |
555 | 554 |
556 private: | 555 private: |
557 class RenderFrameHostTracker; | 556 class RenderFrameHostTracker; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 | 610 |
612 protected: | 611 protected: |
613 friend struct content::BrowserThread::DeleteOnThread< | 612 friend struct content::BrowserThread::DeleteOnThread< |
614 content::BrowserThread::IO>; | 613 content::BrowserThread::IO>; |
615 friend class base::DeleteHelper<IOThreadExtensionFunction>; | 614 friend class base::DeleteHelper<IOThreadExtensionFunction>; |
616 | 615 |
617 ~IOThreadExtensionFunction() override; | 616 ~IOThreadExtensionFunction() override; |
618 | 617 |
619 void Destruct() const override; | 618 void Destruct() const override; |
620 | 619 |
621 void SendResponse(bool success) override; | |
622 | |
623 private: | 620 private: |
624 base::WeakPtr<extensions::IOThreadExtensionMessageFilter> ipc_sender_; | 621 base::WeakPtr<extensions::IOThreadExtensionMessageFilter> ipc_sender_; |
625 int routing_id_; | 622 int routing_id_; |
626 | 623 |
627 scoped_refptr<const extensions::InfoMap> extension_info_map_; | 624 scoped_refptr<const extensions::InfoMap> extension_info_map_; |
628 | 625 |
629 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); | 626 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); |
630 }; | 627 }; |
631 | 628 |
632 // Base class for an extension function that runs asynchronously *relative to | 629 // Base class for an extension function that runs asynchronously *relative to |
633 // the browser's UI thread*. | 630 // the browser's UI thread*. |
634 class AsyncExtensionFunction : public UIThreadExtensionFunction { | 631 class AsyncExtensionFunction : public UIThreadExtensionFunction { |
635 public: | 632 public: |
636 AsyncExtensionFunction(); | 633 AsyncExtensionFunction(); |
637 | 634 |
638 protected: | 635 protected: |
639 ~AsyncExtensionFunction() override; | 636 ~AsyncExtensionFunction() override; |
640 | 637 |
641 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. | 638 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. |
642 // | 639 // |
643 // AsyncExtensionFunctions implement this method. Return true to indicate that | 640 // AsyncExtensionFunctions implement this method. Return true to indicate that |
644 // nothing has gone wrong yet; SendResponse must be called later. Return false | 641 // nothing has gone wrong yet; SendResponse must be called later. Return false |
645 // to respond immediately with an error. | 642 // to respond immediately with an error. |
646 virtual bool RunAsync() = 0; | 643 virtual bool RunAsync() = 0; |
647 | 644 |
648 // ValidationFailure override to match RunAsync(). | 645 // ValidationFailure override to match RunAsync(). |
649 static bool ValidationFailure(AsyncExtensionFunction* function); | 646 static bool ValidationFailure(AsyncExtensionFunction* function); |
650 | 647 |
648 // Responds with success/failure. |results_| or |error_| should be set | |
649 // accordingly. | |
650 void SendResponse(bool success); | |
651 | |
651 private: | 652 private: |
652 // If you're hitting a compile error here due to "final" - great! You're | 653 // 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 | 654 // doing the right thing, you just need to extend UIThreadExtensionFunction |
654 // instead of AsyncExtensionFunction. | 655 // instead of AsyncExtensionFunction. |
655 ResponseAction Run() final; | 656 ResponseAction Run() final; |
656 | 657 |
657 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); | 658 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); |
658 }; | 659 }; |
659 | 660 |
660 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 661 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
OLD | NEW |