InspectorApplicationCacheAgent.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 Google 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
  7. * are met:
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #if ENABLE(INSPECTOR)
  27. #include "InspectorApplicationCacheAgent.h"
  28. #include "ApplicationCacheHost.h"
  29. #include "DocumentLoader.h"
  30. #include "Frame.h"
  31. #include "FrameLoader.h"
  32. #include "InspectorAgent.h"
  33. #include "InspectorFrontend.h"
  34. #include "InspectorPageAgent.h"
  35. #include "InspectorState.h"
  36. #include "InspectorValues.h"
  37. #include "InstrumentingAgents.h"
  38. #include "NetworkStateNotifier.h"
  39. #include "Page.h"
  40. #include "ResourceResponse.h"
  41. namespace WebCore {
  42. namespace ApplicationCacheAgentState {
  43. static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled";
  44. }
  45. InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorPageAgent* pageAgent)
  46. : InspectorBaseAgent<InspectorApplicationCacheAgent>("ApplicationCache", instrumentingAgents, state)
  47. , m_pageAgent(pageAgent)
  48. , m_frontend(0)
  49. {
  50. }
  51. void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend)
  52. {
  53. m_frontend = frontend->applicationcache();
  54. }
  55. void InspectorApplicationCacheAgent::clearFrontend()
  56. {
  57. m_instrumentingAgents->setInspectorApplicationCacheAgent(0);
  58. m_frontend = 0;
  59. }
  60. void InspectorApplicationCacheAgent::restore()
  61. {
  62. if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) {
  63. ErrorString error;
  64. enable(&error);
  65. }
  66. }
  67. void InspectorApplicationCacheAgent::enable(ErrorString*)
  68. {
  69. m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true);
  70. m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
  71. // We need to pass initial navigator.onOnline.
  72. networkStateChanged();
  73. }
  74. void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
  75. {
  76. DocumentLoader* documentLoader = frame->loader()->documentLoader();
  77. if (!documentLoader)
  78. return;
  79. ApplicationCacheHost* host = documentLoader->applicationCacheHost();
  80. ApplicationCacheHost::Status status = host->status();
  81. ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
  82. String manifestURL = info.m_manifest.string();
  83. m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
  84. }
  85. void InspectorApplicationCacheAgent::networkStateChanged()
  86. {
  87. bool isNowOnline = networkStateNotifier().onLine();
  88. m_frontend->networkStateUpdated(isNowOnline);
  89. }
  90. void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result)
  91. {
  92. result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create();
  93. Frame* mainFrame = m_pageAgent->mainFrame();
  94. for (Frame* frame = mainFrame; frame; frame = frame->tree()->traverseNext(mainFrame)) {
  95. DocumentLoader* documentLoader = frame->loader()->documentLoader();
  96. if (!documentLoader)
  97. continue;
  98. ApplicationCacheHost* host = documentLoader->applicationCacheHost();
  99. ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
  100. String manifestURL = info.m_manifest.string();
  101. if (!manifestURL.isEmpty()) {
  102. RefPtr<TypeBuilder::ApplicationCache::FrameWithManifest> value = TypeBuilder::ApplicationCache::FrameWithManifest::create()
  103. .setFrameId(m_pageAgent->frameId(frame))
  104. .setManifestURL(manifestURL)
  105. .setStatus(static_cast<int>(host->status()));
  106. result->addItem(value);
  107. }
  108. }
  109. }
  110. DocumentLoader* InspectorApplicationCacheAgent::assertFrameWithDocumentLoader(ErrorString* errorString, String frameId)
  111. {
  112. Frame* frame = m_pageAgent->assertFrame(errorString, frameId);
  113. if (!frame)
  114. return 0;
  115. return InspectorPageAgent::assertDocumentLoader(errorString, frame);
  116. }
  117. void InspectorApplicationCacheAgent::getManifestForFrame(ErrorString* errorString, const String& frameId, String* manifestURL)
  118. {
  119. DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
  120. if (!documentLoader)
  121. return;
  122. ApplicationCacheHost::CacheInfo info = documentLoader->applicationCacheHost()->applicationCacheInfo();
  123. *manifestURL = info.m_manifest.string();
  124. }
  125. void InspectorApplicationCacheAgent::getApplicationCacheForFrame(ErrorString* errorString, const String& frameId, RefPtr<TypeBuilder::ApplicationCache::ApplicationCache>& applicationCache)
  126. {
  127. DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
  128. if (!documentLoader)
  129. return;
  130. ApplicationCacheHost* host = documentLoader->applicationCacheHost();
  131. ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
  132. ApplicationCacheHost::ResourceInfoList resources;
  133. host->fillResourceList(&resources);
  134. applicationCache = buildObjectForApplicationCache(resources, info);
  135. }
  136. PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCache> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo)
  137. {
  138. return TypeBuilder::ApplicationCache::ApplicationCache::create()
  139. .setManifestURL(applicationCacheInfo.m_manifest.string())
  140. .setSize(applicationCacheInfo.m_size)
  141. .setCreationTime(applicationCacheInfo.m_creationTime)
  142. .setUpdateTime(applicationCacheInfo.m_updateTime)
  143. .setResources(buildArrayForApplicationCacheResources(applicationCacheResources))
  144. .release();
  145. }
  146. PassRefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources)
  147. {
  148. RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > resources = TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource>::create();
  149. ApplicationCacheHost::ResourceInfoList::const_iterator end = applicationCacheResources.end();
  150. ApplicationCacheHost::ResourceInfoList::const_iterator it = applicationCacheResources.begin();
  151. for (int i = 0; it != end; ++it, i++)
  152. resources->addItem(buildObjectForApplicationCacheResource(*it));
  153. return resources;
  154. }
  155. PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo)
  156. {
  157. String types;
  158. if (resourceInfo.m_isMaster)
  159. types.append("Master ");
  160. if (resourceInfo.m_isManifest)
  161. types.append("Manifest ");
  162. if (resourceInfo.m_isFallback)
  163. types.append("Fallback ");
  164. if (resourceInfo.m_isForeign)
  165. types.append("Foreign ");
  166. if (resourceInfo.m_isExplicit)
  167. types.append("Explicit ");
  168. RefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> value = TypeBuilder::ApplicationCache::ApplicationCacheResource::create()
  169. .setUrl(resourceInfo.m_resource.string())
  170. .setSize(static_cast<int>(resourceInfo.m_size))
  171. .setType(types);
  172. return value;
  173. }
  174. } // namespace WebCore
  175. #endif // ENABLE(INSPECTOR)