SAiOSKeychainPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // //////////////////////////////////////
  2. // Keychain PhoneGap Plugin
  3. // by Shazron Abdullah
  4. // Nov 5th 2010
  5. //
  6. // ///////////////////
  7. (function(){
  8. // ///////////////////
  9. // get local ref to global PhoneGap/Cordova/cordova object for exec function
  10. var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
  11. /**
  12. * Constructor
  13. */
  14. function SAiOSKeychainPlugin()
  15. {
  16. this._getCallbacks = {};
  17. this._setCallbacks = {};
  18. this._removeCallbacks = {};
  19. }
  20. //MARK: Get
  21. SAiOSKeychainPlugin.prototype._onGetCallbackSuccess = function(key, value)
  22. {
  23. if (this._getCallbacks[key] && this._getCallbacks[key].onSuccess) {
  24. this._getCallbacks[key].onSuccess(key, value);
  25. }
  26. delete this._getCallbacks[key];
  27. }
  28. SAiOSKeychainPlugin.prototype._onGetCallbackFail = function(key, error)
  29. {
  30. if (this._getCallbacks[key] && this._getCallbacks[key].onFail) {
  31. this._getCallbacks[key].onFail(key, error);
  32. }
  33. delete this._getCallbacks[key];
  34. }
  35. SAiOSKeychainPlugin.prototype.getForKey = function(key, servicename, onSuccess, onFail)
  36. {
  37. this._getCallbacks[key] = { onSuccess:onSuccess, onFail:onFail };
  38. cordovaRef.exec("SAiOSKeychainPlugin.getForKey", key, servicename);
  39. }
  40. //MARK: Set
  41. SAiOSKeychainPlugin.prototype._onSetCallbackSuccess = function(key)
  42. {
  43. if (this._setCallbacks[key] && this._setCallbacks[key].onSuccess) {
  44. this._setCallbacks[key].onSuccess(key);
  45. }
  46. delete this._setCallbacks[key];
  47. }
  48. SAiOSKeychainPlugin.prototype._onSetCallbackFail = function(key, error)
  49. {
  50. if (this._setCallbacks[key] && this._setCallbacks[key].onFail) {
  51. this._setCallbacks[key].onFail(key, error);
  52. }
  53. delete this._setCallbacks[key];
  54. }
  55. SAiOSKeychainPlugin.prototype.setForKey = function(key, value, servicename, onSuccess, onFail)
  56. {
  57. this._setCallbacks[key] = { onSuccess:onSuccess, onFail:onFail };
  58. cordovaRef.exec("SAiOSKeychainPlugin.setForKey", key, value, servicename);
  59. }
  60. //MARK: Remove
  61. SAiOSKeychainPlugin.prototype._onRemoveCallbackSuccess = function(key)
  62. {
  63. if (this._removeCallbacks[key] && this._removeCallbacks[key].onSuccess) {
  64. this._removeCallbacks[key].onSuccess(key);
  65. }
  66. delete this._removeCallbacks[key];
  67. }
  68. SAiOSKeychainPlugin.prototype._onRemoveCallbackFail = function(key, error)
  69. {
  70. if (this._removeCallbacks[key] && this._removeCallbacks[key].onFail) {
  71. this._removeCallbacks[key].onFail(key, error);
  72. }
  73. delete this._removeCallbacks[key];
  74. }
  75. SAiOSKeychainPlugin.prototype.removeForKey = function(key, servicename, onSuccess, onFail)
  76. {
  77. this._removeCallbacks[key] = { onSuccess:onSuccess, onFail:onFail };
  78. cordovaRef.exec("SAiOSKeychainPlugin.removeForKey", key, servicename);
  79. }
  80. //MARK: Install
  81. SAiOSKeychainPlugin.install = function()
  82. {
  83. if ( !window.plugins ) {
  84. window.plugins = {};
  85. }
  86. if ( !window.plugins.keychain ) {
  87. window.plugins.keychain = new SAiOSKeychainPlugin();
  88. }
  89. }
  90. /**
  91. * Add to Cordova constructor
  92. */
  93. if (cordovaRef && cordovaRef.addConstructor) {
  94. cordovaRef.addConstructor(SAiOSKeychainPlugin.install);
  95. } else {
  96. console.log("Keychain Cordova Plugin could not be installed.");
  97. return null;
  98. }
  99. // ///////////////////
  100. })();
  101. // ///////////////////