Setting.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2009 Google Inc. All rights reserved.
  3. * Copyright (C) 2013 Apple Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. WebInspector.Setting = function(name, defaultValue)
  32. {
  33. this._name = name;
  34. this._localStorageKey = WebInspector.Setting.LocalStorageKeyPrefix + name;
  35. this._defaultValue = defaultValue;
  36. }
  37. WebInspector.Object.addConstructorFunctions(WebInspector.Setting);
  38. WebInspector.Setting.Event = {
  39. Changed: "setting-changed"
  40. };
  41. WebInspector.Setting.LocalStorageKeyPrefix = "com.apple.WebInspector.";
  42. WebInspector.Setting.prototype = {
  43. constructor: WebInspector.Setting,
  44. // Public
  45. get name()
  46. {
  47. return this._name;
  48. },
  49. get value()
  50. {
  51. if ("_value" in this)
  52. return this._value;
  53. // Make a copy of the default value so changes to object values don't modify the default value.
  54. this._value = JSON.parse(JSON.stringify(this._defaultValue));
  55. if (window.localStorage && this._localStorageKey in window.localStorage) {
  56. try {
  57. this._value = JSON.parse(window.localStorage[this._localStorageKey]);
  58. } catch(e) {
  59. delete window.localStorage[this._localStorageKey];
  60. }
  61. }
  62. return this._value;
  63. },
  64. set value(value)
  65. {
  66. this._value = value;
  67. if (window.localStorage) {
  68. try {
  69. // Use Object.shallowEqual to properly compare objects.
  70. if (Object.shallowEqual(this._value, this._defaultValue))
  71. delete window.localStorage[this._localStorageKey];
  72. else
  73. window.localStorage[this._localStorageKey] = JSON.stringify(this._value);
  74. } catch(e) {
  75. console.error("Error saving setting with name: " + this._name);
  76. }
  77. }
  78. this.dispatchEventToListeners(WebInspector.Setting.Event.Changed, this._value, {name: this._name});
  79. }
  80. }
  81. WebInspector.Setting.prototype.__proto__ = WebInspector.Object.prototype;