InspectorState.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #if ENABLE(INSPECTOR)
  30. #include "InspectorState.h"
  31. #include "InspectorStateClient.h"
  32. #include <wtf/PassOwnPtr.h>
  33. namespace WebCore {
  34. InspectorState::InspectorState(InspectorStateUpdateListener* listener, PassRefPtr<InspectorObject> properties)
  35. : m_listener(listener)
  36. , m_properties(properties)
  37. {
  38. }
  39. void InspectorState::updateCookie()
  40. {
  41. if (m_listener)
  42. m_listener->inspectorStateUpdated();
  43. }
  44. void InspectorState::setFromCookie(PassRefPtr<InspectorObject> properties)
  45. {
  46. m_properties = properties;
  47. }
  48. void InspectorState::setValue(const String& propertyName, PassRefPtr<InspectorValue> value)
  49. {
  50. m_properties->setValue(propertyName, value);
  51. updateCookie();
  52. }
  53. void InspectorState::remove(const String& propertyName)
  54. {
  55. m_properties->remove(propertyName);
  56. updateCookie();
  57. }
  58. bool InspectorState::getBoolean(const String& propertyName)
  59. {
  60. InspectorObject::iterator it = m_properties->find(propertyName);
  61. bool value = false;
  62. if (it != m_properties->end())
  63. it->value->asBoolean(&value);
  64. return value;
  65. }
  66. String InspectorState::getString(const String& propertyName)
  67. {
  68. InspectorObject::iterator it = m_properties->find(propertyName);
  69. String value;
  70. if (it != m_properties->end())
  71. it->value->asString(&value);
  72. return value;
  73. }
  74. long InspectorState::getLong(const String& propertyName)
  75. {
  76. InspectorObject::iterator it = m_properties->find(propertyName);
  77. long value = 0;
  78. if (it != m_properties->end())
  79. it->value->asNumber(&value);
  80. return value;
  81. }
  82. double InspectorState::getDouble(const String& propertyName)
  83. {
  84. InspectorObject::iterator it = m_properties->find(propertyName);
  85. double value = 0;
  86. if (it != m_properties->end())
  87. it->value->asNumber(&value);
  88. return value;
  89. }
  90. PassRefPtr<InspectorObject> InspectorState::getObject(const String& propertyName)
  91. {
  92. InspectorObject::iterator it = m_properties->find(propertyName);
  93. if (it == m_properties->end()) {
  94. m_properties->setObject(propertyName, InspectorObject::create());
  95. it = m_properties->find(propertyName);
  96. }
  97. return it->value->asObject();
  98. }
  99. InspectorState* InspectorCompositeState::createAgentState(const String& agentName)
  100. {
  101. ASSERT(m_stateObject->find(agentName) == m_stateObject->end());
  102. ASSERT(m_inspectorStateMap.find(agentName) == m_inspectorStateMap.end());
  103. RefPtr<InspectorObject> stateProperties = InspectorObject::create();
  104. m_stateObject->setObject(agentName, stateProperties);
  105. OwnPtr<InspectorState> statePtr = adoptPtr(new InspectorState(this, stateProperties));
  106. InspectorState* state = statePtr.get();
  107. m_inspectorStateMap.add(agentName, statePtr.release());
  108. return state;
  109. }
  110. void InspectorCompositeState::loadFromCookie(const String& inspectorCompositeStateCookie)
  111. {
  112. RefPtr<InspectorValue> cookie = InspectorValue::parseJSON(inspectorCompositeStateCookie);
  113. if (cookie)
  114. m_stateObject = cookie->asObject();
  115. if (!m_stateObject)
  116. m_stateObject = InspectorObject::create();
  117. InspectorStateMap::iterator end = m_inspectorStateMap.end();
  118. for (InspectorStateMap::iterator it = m_inspectorStateMap.begin(); it != end; ++it) {
  119. RefPtr<InspectorObject> agentStateObject = m_stateObject->getObject(it->key);
  120. if (!agentStateObject) {
  121. agentStateObject = InspectorObject::create();
  122. m_stateObject->setObject(it->key, agentStateObject);
  123. }
  124. it->value->setFromCookie(agentStateObject);
  125. }
  126. }
  127. void InspectorCompositeState::mute()
  128. {
  129. m_isMuted = true;
  130. }
  131. void InspectorCompositeState::unmute()
  132. {
  133. m_isMuted = false;
  134. }
  135. void InspectorCompositeState::inspectorStateUpdated()
  136. {
  137. if (m_client && !m_isMuted && m_client->supportsInspectorStateUpdates())
  138. m_client->updateInspectorStateCookie(m_stateObject->toJSONString());
  139. }
  140. } // namespace WebCore
  141. #endif // ENABLE(INSPECTOR)