| 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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 return source_context_type_; | 308 return source_context_type_; |
| 309 } | 309 } |
| 310 | 310 |
| 311 void set_source_process_id(int source_process_id) { | 311 void set_source_process_id(int source_process_id) { |
| 312 source_process_id_ = source_process_id; | 312 source_process_id_ = source_process_id; |
| 313 } | 313 } |
| 314 int source_process_id() const { | 314 int source_process_id() const { |
| 315 return source_process_id_; | 315 return source_process_id_; |
| 316 } | 316 } |
| 317 | 317 |
| 318 ResponseType* response_type() const { return response_type_.get(); } |
| 319 |
| 318 // Sets did_respond_ to true so that the function won't DCHECK if it never | 320 // Sets did_respond_ to true so that the function won't DCHECK if it never |
| 319 // sends a response. Typically, this shouldn't be used, even in testing. It's | 321 // sends a response. Typically, this shouldn't be used, even in testing. It's |
| 320 // only for when you want to test functionality that doesn't exercise the | 322 // only for when you want to test functionality that doesn't exercise the |
| 321 // Run() aspect of an extension function. | 323 // Run() aspect of an extension function. |
| 322 void ignore_did_respond_for_testing() { did_respond_ = true; } | 324 void ignore_did_respond_for_testing() { did_respond_ = true; } |
| 323 // Same as above, but global. Yuck. Do not add any more uses of this. | 325 // Same as above, but global. Yuck. Do not add any more uses of this. |
| 324 static bool ignore_all_did_respond_for_testing_do_not_use; | 326 static bool ignore_all_did_respond_for_testing_do_not_use; |
| 325 | 327 |
| 326 protected: | 328 protected: |
| 327 friend struct ExtensionFunctionDeleteTraits; | 329 friend struct ExtensionFunctionDeleteTraits; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 // The process ID of the page that triggered this function call, or -1 | 470 // The process ID of the page that triggered this function call, or -1 |
| 469 // if unknown. | 471 // if unknown. |
| 470 int source_process_id_; | 472 int source_process_id_; |
| 471 | 473 |
| 472 // Whether this function has responded. | 474 // Whether this function has responded. |
| 473 bool did_respond_; | 475 bool did_respond_; |
| 474 | 476 |
| 475 private: | 477 private: |
| 476 base::ElapsedTimer timer_; | 478 base::ElapsedTimer timer_; |
| 477 | 479 |
| 480 // The response type of the function, if the response has been sent. |
| 481 std::unique_ptr<ResponseType> response_type_; |
| 482 |
| 478 void OnRespondingLater(ResponseValue response); | 483 void OnRespondingLater(ResponseValue response); |
| 479 | 484 |
| 480 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 485 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
| 481 }; | 486 }; |
| 482 | 487 |
| 483 // Extension functions that run on the UI thread. Most functions fall into | 488 // Extension functions that run on the UI thread. Most functions fall into |
| 484 // this category. | 489 // this category. |
| 485 class UIThreadExtensionFunction : public ExtensionFunction { | 490 class UIThreadExtensionFunction : public ExtensionFunction { |
| 486 public: | 491 public: |
| 487 // TODO(yzshen): We should be able to remove this interface now that we | |
| 488 // support overriding the response callback. | |
| 489 // A delegate for use in testing, to intercept the call to SendResponse. | |
| 490 class DelegateForTests { | |
| 491 public: | |
| 492 virtual void OnSendResponse(UIThreadExtensionFunction* function, | |
| 493 bool success, | |
| 494 bool bad_message) = 0; | |
| 495 }; | |
| 496 | |
| 497 UIThreadExtensionFunction(); | 492 UIThreadExtensionFunction(); |
| 498 | 493 |
| 499 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; | 494 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; |
| 500 | 495 |
| 501 bool PreRunValidation(std::string* error) override; | 496 bool PreRunValidation(std::string* error) override; |
| 502 | 497 |
| 503 void set_test_delegate(DelegateForTests* delegate) { | |
| 504 delegate_ = delegate; | |
| 505 } | |
| 506 | |
| 507 // Called when a message was received. | 498 // Called when a message was received. |
| 508 // Should return true if it processed the message. | 499 // Should return true if it processed the message. |
| 509 virtual bool OnMessageReceived(const IPC::Message& message); | 500 virtual bool OnMessageReceived(const IPC::Message& message); |
| 510 | 501 |
| 511 // Set the browser context which contains the extension that has originated | 502 // Set the browser context which contains the extension that has originated |
| 512 // this function call. | 503 // this function call. |
| 513 void set_browser_context(content::BrowserContext* context) { | 504 void set_browser_context(content::BrowserContext* context) { |
| 514 context_ = context; | 505 context_ = context; |
| 515 } | 506 } |
| 516 content::BrowserContext* browser_context() const { return context_; } | 507 content::BrowserContext* browser_context() const { return context_; } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 | 563 |
| 573 // The RenderFrameHost we will send responses to. | 564 // The RenderFrameHost we will send responses to. |
| 574 content::RenderFrameHost* render_frame_host_; | 565 content::RenderFrameHost* render_frame_host_; |
| 575 | 566 |
| 576 // Whether or not this ExtensionFunction was called by an extension Service | 567 // Whether or not this ExtensionFunction was called by an extension Service |
| 577 // Worker. | 568 // Worker. |
| 578 bool is_from_service_worker_; | 569 bool is_from_service_worker_; |
| 579 | 570 |
| 580 std::unique_ptr<RenderFrameHostTracker> tracker_; | 571 std::unique_ptr<RenderFrameHostTracker> tracker_; |
| 581 | 572 |
| 582 DelegateForTests* delegate_; | |
| 583 | |
| 584 // The blobs transferred to the renderer process. | 573 // The blobs transferred to the renderer process. |
| 585 std::vector<std::string> transferred_blob_uuids_; | 574 std::vector<std::string> transferred_blob_uuids_; |
| 586 | 575 |
| 587 DISALLOW_COPY_AND_ASSIGN(UIThreadExtensionFunction); | 576 DISALLOW_COPY_AND_ASSIGN(UIThreadExtensionFunction); |
| 588 }; | 577 }; |
| 589 | 578 |
| 590 // Extension functions that run on the IO thread. This type of function avoids | 579 // Extension functions that run on the IO thread. This type of function avoids |
| 591 // a roundtrip to and from the UI thread (because communication with the | 580 // a roundtrip to and from the UI thread (because communication with the |
| 592 // extension process happens on the IO thread). It's intended to be used when | 581 // extension process happens on the IO thread). It's intended to be used when |
| 593 // performance is critical (e.g. the webRequest API which can block network | 582 // performance is critical (e.g. the webRequest API which can block network |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 private: | 651 private: |
| 663 // If you're hitting a compile error here due to "final" - great! You're | 652 // If you're hitting a compile error here due to "final" - great! You're |
| 664 // doing the right thing, you just need to extend UIThreadExtensionFunction | 653 // doing the right thing, you just need to extend UIThreadExtensionFunction |
| 665 // instead of AsyncExtensionFunction. | 654 // instead of AsyncExtensionFunction. |
| 666 ResponseAction Run() final; | 655 ResponseAction Run() final; |
| 667 | 656 |
| 668 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); | 657 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); |
| 669 }; | 658 }; |
| 670 | 659 |
| 671 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 660 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
| OLD | NEW |