| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 namespace IPC { | 51 namespace IPC { |
| 52 class Sender; | 52 class Sender; |
| 53 } | 53 } |
| 54 | 54 |
| 55 #ifdef NDEBUG | 55 #ifdef NDEBUG |
| 56 #define EXTENSION_FUNCTION_VALIDATE(test) \ | 56 #define EXTENSION_FUNCTION_VALIDATE(test) \ |
| 57 do { \ | 57 do { \ |
| 58 if (!(test)) { \ | 58 if (!(test)) { \ |
| 59 this->bad_message_ = true; \ | 59 this->set_bad_message(true); \ |
| 60 return ValidationFailure(this); \ | 60 return ValidationFailure(this); \ |
| 61 } \ | 61 } \ |
| 62 } while (0) | 62 } while (0) |
| 63 #else // NDEBUG | 63 #else // NDEBUG |
| 64 #define EXTENSION_FUNCTION_VALIDATE(test) CHECK(test) | 64 #define EXTENSION_FUNCTION_VALIDATE(test) CHECK(test) |
| 65 #endif // NDEBUG | 65 #endif // NDEBUG |
| 66 | 66 |
| 67 #ifdef NDEBUG | 67 #ifdef NDEBUG |
| 68 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) \ | 68 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) \ |
| 69 do { \ | 69 do { \ |
| 70 if (!(test)) { \ | 70 if (!(test)) { \ |
| 71 this->bad_message_ = true; \ | 71 this->set_bad_message(true); \ |
| 72 return false; \ | 72 return false; \ |
| 73 } \ | 73 } \ |
| 74 } while (0) | 74 } while (0) |
| 75 #else // NDEBUG | 75 #else // NDEBUG |
| 76 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) CHECK(test) | 76 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) CHECK(test) |
| 77 #endif // NDEBUG | 77 #endif // NDEBUG |
| 78 | 78 |
| 79 #define EXTENSION_FUNCTION_ERROR(error) \ | 79 #define EXTENSION_FUNCTION_ERROR(error) \ |
| 80 do { \ | 80 do { \ |
| 81 error_ = error; \ | 81 error_ = error; \ |
| 82 this->bad_message_ = true; \ | 82 this->set_bad_message(true); \ |
| 83 return ValidationFailure(this); \ | 83 return ValidationFailure(this); \ |
| 84 } while (0) | 84 } while (0) |
| 85 | 85 |
| 86 // Declares a callable extension function with the given |name|. You must also | 86 // Declares a callable extension function with the given |name|. You must also |
| 87 // supply a unique |histogramvalue| used for histograms of extension function | 87 // supply a unique |histogramvalue| used for histograms of extension function |
| 88 // invocation (add new ones at the end of the enum in | 88 // invocation (add new ones at the end of the enum in |
| 89 // extension_function_histogram_value.h). | 89 // extension_function_histogram_value.h). |
| 90 #define DECLARE_EXTENSION_FUNCTION(name, histogramvalue) \ | 90 #define DECLARE_EXTENSION_FUNCTION(name, histogramvalue) \ |
| 91 public: static const char* function_name() { return name; } \ | 91 public: static const char* function_name() { return name; } \ |
| 92 public: static extensions::functions::HistogramValue histogram_value() \ | 92 public: static extensions::functions::HistogramValue histogram_value() \ |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // The result of a function call. | 138 // The result of a function call. |
| 139 // | 139 // |
| 140 // Use NoArguments(), OneArgument(), ArgumentList(), or Error() | 140 // Use NoArguments(), OneArgument(), ArgumentList(), or Error() |
| 141 // rather than this class directly. | 141 // rather than this class directly. |
| 142 class ResponseValueObject { | 142 class ResponseValueObject { |
| 143 public: | 143 public: |
| 144 virtual ~ResponseValueObject() {} | 144 virtual ~ResponseValueObject() {} |
| 145 | 145 |
| 146 // Returns true for success, false for failure. | 146 // Returns true for success, false for failure. |
| 147 virtual bool Apply() = 0; | 147 virtual bool Apply() = 0; |
| 148 |
| 149 protected: |
| 150 void SetFunctionResults(ExtensionFunction* function, |
| 151 std::unique_ptr<base::ListValue> results); |
| 152 void SetFunctionError(ExtensionFunction* function, |
| 153 const std::string& error); |
| 148 }; | 154 }; |
| 149 typedef std::unique_ptr<ResponseValueObject> ResponseValue; | 155 typedef std::unique_ptr<ResponseValueObject> ResponseValue; |
| 150 | 156 |
| 151 // The action to use when returning from RunAsync. | 157 // The action to use when returning from RunAsync. |
| 152 // | 158 // |
| 153 // Use RespondNow() or RespondLater() rather than this class directly. | 159 // Use RespondNow() or RespondLater() rather than this class directly. |
| 154 class ResponseActionObject { | 160 class ResponseActionObject { |
| 155 public: | 161 public: |
| 156 virtual ~ResponseActionObject() {} | 162 virtual ~ResponseActionObject() {} |
| 157 | 163 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 extensions::QuotaLimitHeuristics* heuristics) const {} | 230 extensions::QuotaLimitHeuristics* heuristics) const {} |
| 225 | 231 |
| 226 // Called when the quota limit has been exceeded. The default implementation | 232 // Called when the quota limit has been exceeded. The default implementation |
| 227 // returns an error. | 233 // returns an error. |
| 228 virtual void OnQuotaExceeded(const std::string& violation_error); | 234 virtual void OnQuotaExceeded(const std::string& violation_error); |
| 229 | 235 |
| 230 // Specifies the raw arguments to the function, as a JSON value. | 236 // Specifies the raw arguments to the function, as a JSON value. |
| 231 // TODO(dcheng): This should take a const ref. | 237 // TODO(dcheng): This should take a const ref. |
| 232 virtual void SetArgs(const base::ListValue* args); | 238 virtual void SetArgs(const base::ListValue* args); |
| 233 | 239 |
| 234 // Sets a single Value as the results of the function. | |
| 235 void SetResult(std::unique_ptr<base::Value> result); | |
| 236 | |
| 237 // Sets multiple Values as the results of the function. | |
| 238 void SetResultList(std::unique_ptr<base::ListValue> results); | |
| 239 | |
| 240 // Retrieves the results of the function as a ListValue. | 240 // Retrieves the results of the function as a ListValue. |
| 241 const base::ListValue* GetResultList() const; | 241 const base::ListValue* GetResultList() const; |
| 242 | 242 |
| 243 // Retrieves any error string from the function. | 243 // Retrieves any error string from the function. |
| 244 virtual std::string GetError() const; | 244 virtual const std::string& GetError() const; |
| 245 | 245 |
| 246 // Sets the function's error string. | 246 // Sets the function's error string. |
| 247 // TODO(devlin): This should be handled exclusively through the responses. |
| 247 virtual void SetError(const std::string& error); | 248 virtual void SetError(const std::string& error); |
| 248 | 249 |
| 249 // Sets the function's bad message state. | 250 bool bad_message() const { return bad_message_; } |
| 250 void set_bad_message(bool bad_message) { bad_message_ = bad_message; } | 251 void set_bad_message(bool bad_message) { bad_message_ = bad_message; } |
| 251 | 252 |
| 252 // Specifies the name of the function. A long-lived string (such as a string | 253 // Specifies the name of the function. A long-lived string (such as a string |
| 253 // literal) must be provided. | 254 // literal) must be provided. |
| 254 void set_name(const char* name) { name_ = name; } | 255 void set_name(const char* name) { name_ = name; } |
| 255 const char* name() const { return name_; } | 256 const char* name() const { return name_; } |
| 256 | 257 |
| 257 void set_profile_id(void* profile_id) { profile_id_ = profile_id; } | 258 void set_profile_id(void* profile_id) { profile_id_ = profile_id; } |
| 258 void* profile_id() const { return profile_id_; } | 259 void* profile_id() const { return profile_id_; } |
| 259 | 260 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 311 |
| 311 void set_source_process_id(int source_process_id) { | 312 void set_source_process_id(int source_process_id) { |
| 312 source_process_id_ = source_process_id; | 313 source_process_id_ = source_process_id; |
| 313 } | 314 } |
| 314 int source_process_id() const { | 315 int source_process_id() const { |
| 315 return source_process_id_; | 316 return source_process_id_; |
| 316 } | 317 } |
| 317 | 318 |
| 318 ResponseType* response_type() const { return response_type_.get(); } | 319 ResponseType* response_type() const { return response_type_.get(); } |
| 319 | 320 |
| 321 bool did_respond() const { return did_respond_; } |
| 322 |
| 320 // Sets did_respond_ to true so that the function won't DCHECK if it never | 323 // Sets did_respond_ to true so that the function won't DCHECK if it never |
| 321 // sends a response. Typically, this shouldn't be used, even in testing. It's | 324 // sends a response. Typically, this shouldn't be used, even in testing. It's |
| 322 // only for when you want to test functionality that doesn't exercise the | 325 // only for when you want to test functionality that doesn't exercise the |
| 323 // Run() aspect of an extension function. | 326 // Run() aspect of an extension function. |
| 324 void ignore_did_respond_for_testing() { did_respond_ = true; } | 327 void ignore_did_respond_for_testing() { did_respond_ = true; } |
| 325 // Same as above, but global. Yuck. Do not add any more uses of this. | 328 // Same as above, but global. Yuck. Do not add any more uses of this. |
| 326 static bool ignore_all_did_respond_for_testing_do_not_use; | 329 static bool ignore_all_did_respond_for_testing_do_not_use; |
| 327 | 330 |
| 328 protected: | 331 protected: |
| 329 friend struct ExtensionFunctionDeleteTraits; | 332 friend struct ExtensionFunctionDeleteTraits; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 virtual void Destruct() const = 0; | 401 virtual void Destruct() const = 0; |
| 399 | 402 |
| 400 // Called after the response is sent, allowing the function to perform any | 403 // Called after the response is sent, allowing the function to perform any |
| 401 // additional work or cleanup. | 404 // additional work or cleanup. |
| 402 virtual void OnResponded() {} | 405 virtual void OnResponded() {} |
| 403 | 406 |
| 404 // Return true if the argument to this function at |index| was provided and | 407 // Return true if the argument to this function at |index| was provided and |
| 405 // is non-null. | 408 // is non-null. |
| 406 bool HasOptionalArgument(size_t index); | 409 bool HasOptionalArgument(size_t index); |
| 407 | 410 |
| 411 // The extension that called this function. |
| 412 scoped_refptr<const extensions::Extension> extension_; |
| 413 |
| 414 // The arguments to the API. Only non-null if argument were specified. |
| 415 std::unique_ptr<base::ListValue> args_; |
| 416 |
| 417 private: |
| 418 friend class ResponseValueObject; |
| 419 |
| 420 // Call with true to indicate success, false to indicate failure. If this |
| 421 // failed, |error_| should be set. |
| 422 void SendResponseImpl(bool success); |
| 423 |
| 424 base::ElapsedTimer timer_; |
| 425 |
| 426 // The results of the API. This should be populated through the Respond()/ |
| 427 // RespondNow() methods. In legacy implementations, this is set directly, and |
| 428 // should be set before calling SendResponse(). |
| 429 std::unique_ptr<base::ListValue> results_; |
| 430 |
| 431 // Any detailed error from the API. This should be populated by the derived |
| 432 // class before Run() returns. |
| 433 std::string error_; |
| 434 |
| 435 // The callback to run once the function has done execution. |
| 436 ResponseCallback response_callback_; |
| 437 |
| 408 // Id of this request, used to map the response back to the caller. | 438 // Id of this request, used to map the response back to the caller. |
| 409 int request_id_; | 439 int request_id_; |
| 410 | 440 |
| 411 // The id of the profile of this function's extension. | 441 // The id of the profile of this function's extension. |
| 412 void* profile_id_; | 442 void* profile_id_; |
| 413 | 443 |
| 414 // The extension that called this function. | |
| 415 scoped_refptr<const extensions::Extension> extension_; | |
| 416 | |
| 417 // The name of this function. | 444 // The name of this function. |
| 418 const char* name_; | 445 const char* name_; |
| 419 | 446 |
| 420 // The URL of the frame which is making this request | 447 // The URL of the frame which is making this request |
| 421 GURL source_url_; | 448 GURL source_url_; |
| 422 | 449 |
| 423 // True if the js caller provides a callback function to receive the response | 450 // True if the js caller provides a callback function to receive the response |
| 424 // of this call. | 451 // of this call. |
| 425 bool has_callback_; | 452 bool has_callback_; |
| 426 | 453 |
| 427 // True if this callback should include information from incognito contexts | 454 // True if this callback should include information from incognito contexts |
| 428 // even if our profile_ is non-incognito. Note that in the case of a "split" | 455 // even if our profile_ is non-incognito. Note that in the case of a "split" |
| 429 // mode extension, this will always be false, and we will limit access to | 456 // mode extension, this will always be false, and we will limit access to |
| 430 // data from within the same profile_ (either incognito or not). | 457 // data from within the same profile_ (either incognito or not). |
| 431 bool include_incognito_; | 458 bool include_incognito_; |
| 432 | 459 |
| 433 // True if the call was made in response of user gesture. | 460 // True if the call was made in response of user gesture. |
| 434 bool user_gesture_; | 461 bool user_gesture_; |
| 435 | 462 |
| 436 // The arguments to the API. Only non-null if argument were specified. | |
| 437 std::unique_ptr<base::ListValue> args_; | |
| 438 | |
| 439 // The results of the API. This should be populated through the Respond()/ | |
| 440 // RespondNow() methods. In legacy implementations, this is set directly, and | |
| 441 // should be set before calling SendResponse(). | |
| 442 std::unique_ptr<base::ListValue> results_; | |
| 443 | |
| 444 // Any detailed error from the API. This should be populated by the derived | |
| 445 // class before Run() returns. | |
| 446 std::string error_; | |
| 447 | |
| 448 // Any class that gets a malformed message should set this to true before | 463 // Any class that gets a malformed message should set this to true before |
| 449 // returning. Usually we want to kill the message sending process. | 464 // returning. Usually we want to kill the message sending process. |
| 450 bool bad_message_; | 465 bool bad_message_; |
| 451 | 466 |
| 452 // The sample value to record with the histogram API when the function | 467 // The sample value to record with the histogram API when the function |
| 453 // is invoked. | 468 // is invoked. |
| 454 extensions::functions::HistogramValue histogram_value_; | 469 extensions::functions::HistogramValue histogram_value_; |
| 455 | 470 |
| 456 // The callback to run once the function has done execution. | |
| 457 ResponseCallback response_callback_; | |
| 458 | |
| 459 // The ID of the tab triggered this function call, or -1 if there is no tab. | 471 // The ID of the tab triggered this function call, or -1 if there is no tab. |
| 460 int source_tab_id_; | 472 int source_tab_id_; |
| 461 | 473 |
| 462 // The type of the JavaScript context where this call originated. | 474 // The type of the JavaScript context where this call originated. |
| 463 extensions::Feature::Context source_context_type_; | 475 extensions::Feature::Context source_context_type_; |
| 464 | 476 |
| 465 // The process ID of the page that triggered this function call, or -1 | 477 // The process ID of the page that triggered this function call, or -1 |
| 466 // if unknown. | 478 // if unknown. |
| 467 int source_process_id_; | 479 int source_process_id_; |
| 468 | 480 |
| 469 // Whether this function has responded. | |
| 470 bool did_respond_; | |
| 471 | |
| 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 | |
| 477 base::ElapsedTimer timer_; | |
| 478 | |
| 479 // The response type of the function, if the response has been sent. | 481 // The response type of the function, if the response has been sent. |
| 480 std::unique_ptr<ResponseType> response_type_; | 482 std::unique_ptr<ResponseType> response_type_; |
| 481 | 483 |
| 484 // Whether this function has responded. |
| 485 // TODO(devlin): Replace this with response_type_ != null. |
| 486 bool did_respond_; |
| 487 |
| 482 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 488 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
| 483 }; | 489 }; |
| 484 | 490 |
| 485 // Extension functions that run on the UI thread. Most functions fall into | 491 // Extension functions that run on the UI thread. Most functions fall into |
| 486 // this category. | 492 // this category. |
| 487 class UIThreadExtensionFunction : public ExtensionFunction { | 493 class UIThreadExtensionFunction : public ExtensionFunction { |
| 488 public: | 494 public: |
| 489 UIThreadExtensionFunction(); | 495 UIThreadExtensionFunction(); |
| 490 | 496 |
| 491 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; | 497 UIThreadExtensionFunction* AsUIThreadExtensionFunction() override; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 | 629 |
| 624 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); | 630 DISALLOW_COPY_AND_ASSIGN(IOThreadExtensionFunction); |
| 625 }; | 631 }; |
| 626 | 632 |
| 627 // Base class for an extension function that runs asynchronously *relative to | 633 // Base class for an extension function that runs asynchronously *relative to |
| 628 // the browser's UI thread*. | 634 // the browser's UI thread*. |
| 629 class AsyncExtensionFunction : public UIThreadExtensionFunction { | 635 class AsyncExtensionFunction : public UIThreadExtensionFunction { |
| 630 public: | 636 public: |
| 631 AsyncExtensionFunction(); | 637 AsyncExtensionFunction(); |
| 632 | 638 |
| 639 // ExtensionFunction: |
| 640 void SetError(const std::string& error) override; |
| 641 const std::string& GetError() const override; |
| 642 |
| 633 protected: | 643 protected: |
| 634 ~AsyncExtensionFunction() override; | 644 ~AsyncExtensionFunction() override; |
| 635 | 645 |
| 646 // Sets a single Value as the results of the function. |
| 647 void SetResult(std::unique_ptr<base::Value> result); |
| 648 |
| 649 // Sets multiple Values as the results of the function. |
| 650 void SetResultList(std::unique_ptr<base::ListValue> results); |
| 651 |
| 636 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. | 652 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead. |
| 637 // | 653 // |
| 638 // AsyncExtensionFunctions implement this method. Return true to indicate that | 654 // AsyncExtensionFunctions implement this method. Return true to indicate that |
| 639 // nothing has gone wrong yet; SendResponse must be called later. Return false | 655 // nothing has gone wrong yet; SendResponse must be called later. Return false |
| 640 // to respond immediately with an error. | 656 // to respond immediately with an error. |
| 641 virtual bool RunAsync() = 0; | 657 virtual bool RunAsync() = 0; |
| 642 | 658 |
| 643 // ValidationFailure override to match RunAsync(). | 659 // ValidationFailure override to match RunAsync(). |
| 644 static bool ValidationFailure(AsyncExtensionFunction* function); | 660 static bool ValidationFailure(AsyncExtensionFunction* function); |
| 645 | 661 |
| 646 // Responds with success/failure. |results_| or |error_| should be set | 662 // Responds with success/failure. |results_| or |error_| should be set |
| 647 // accordingly. | 663 // accordingly. |
| 648 void SendResponse(bool success); | 664 void SendResponse(bool success); |
| 649 | 665 |
| 666 // Exposed versions of |results_| and |error_| which are curried into the |
| 667 // ExtensionFunction response. |
| 668 std::unique_ptr<base::ListValue> results_; |
| 669 std::string error_; |
| 670 |
| 650 private: | 671 private: |
| 651 // If you're hitting a compile error here due to "final" - great! You're | 672 // If you're hitting a compile error here due to "final" - great! You're |
| 652 // doing the right thing, you just need to extend UIThreadExtensionFunction | 673 // doing the right thing, you just need to extend UIThreadExtensionFunction |
| 653 // instead of AsyncExtensionFunction. | 674 // instead of AsyncExtensionFunction. |
| 654 ResponseAction Run() final; | 675 ResponseAction Run() final; |
| 655 | 676 |
| 656 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); | 677 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); |
| 657 }; | 678 }; |
| 658 | 679 |
| 659 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 680 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
| OLD | NEW |