WebProcessQt.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2010 Apple 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebProcess.h"
  27. #include "InjectedBundle.h"
  28. #include "QtBuiltinBundle.h"
  29. #include "QtNetworkAccessManager.h"
  30. #include "SeccompFiltersWebProcessQt.h"
  31. #include "WKBundleAPICast.h"
  32. #include "WebProcessCreationParameters.h"
  33. #include <QCoreApplication>
  34. #include <QNetworkAccessManager>
  35. #include <QNetworkCookieJar>
  36. #include <QNetworkDiskCache>
  37. #include <WebCore/CookieJarQt.h>
  38. #include <WebCore/FileSystem.h>
  39. #include <WebCore/MemoryCache.h>
  40. #include <WebCore/PageCache.h>
  41. #include <WebCore/RuntimeEnabledFeatures.h>
  42. #if defined(Q_OS_MACX)
  43. #include <dispatch/dispatch.h>
  44. #include <mach/host_info.h>
  45. #include <mach/mach.h>
  46. #include <mach/mach_error.h>
  47. #elif !defined(Q_OS_WIN)
  48. #include <unistd.h>
  49. #endif
  50. using namespace WebCore;
  51. namespace WebKit {
  52. static uint64_t physicalMemorySizeInBytes()
  53. {
  54. static uint64_t physicalMemorySize = 0;
  55. if (!physicalMemorySize) {
  56. #if defined(Q_OS_MACX)
  57. host_basic_info_data_t hostInfo;
  58. mach_port_t host = mach_host_self();
  59. mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
  60. kern_return_t r = host_info(host, HOST_BASIC_INFO, (host_info_t)&hostInfo, &count);
  61. mach_port_deallocate(mach_task_self(), host);
  62. if (r == KERN_SUCCESS)
  63. physicalMemorySize = hostInfo.max_mem;
  64. #elif defined(Q_OS_WIN)
  65. MEMORYSTATUSEX statex;
  66. statex.dwLength = sizeof(statex);
  67. GlobalMemoryStatusEx(&statex);
  68. physicalMemorySize = static_cast<uint64_t>(statex.ullTotalPhys);
  69. #else
  70. long pageSize = sysconf(_SC_PAGESIZE);
  71. long numberOfPages = sysconf(_SC_PHYS_PAGES);
  72. if (pageSize > 0 && numberOfPages > 0)
  73. physicalMemorySize = static_cast<uint64_t>(pageSize) * static_cast<uint64_t>(numberOfPages);
  74. #endif
  75. }
  76. return physicalMemorySize;
  77. }
  78. void WebProcess::platformSetCacheModel(CacheModel cacheModel)
  79. {
  80. uint64_t physicalMemorySizeInMegabytes = physicalMemorySizeInBytes() / 1024 / 1024;
  81. // The Mac port of WebKit2 uses a fudge factor of 1000 here to account for misalignment, however,
  82. // that tends to overestimate the memory quite a bit (1 byte misalignment ~ 48 MiB misestimation).
  83. // We use 1024 * 1023 for now to keep the estimation error down to +/- ~1 MiB.
  84. QNetworkDiskCache* diskCache = qobject_cast<QNetworkDiskCache*>(m_networkAccessManager->cache());
  85. uint64_t freeVolumeSpace = !diskCache ? 0 : WebCore::getVolumeFreeSizeForPath(diskCache->cacheDirectory().toLocal8Bit().constData()) / 1024 / 1023;
  86. // The following variables are initialised to 0 because WebProcess::calculateCacheSizes might not
  87. // set them in some rare cases.
  88. unsigned cacheTotalCapacity = 0;
  89. unsigned cacheMinDeadCapacity = 0;
  90. unsigned cacheMaxDeadCapacity = 0;
  91. double deadDecodedDataDeletionInterval = 0;
  92. unsigned pageCacheCapacity = 0;
  93. unsigned long urlCacheMemoryCapacity = 0;
  94. unsigned long urlCacheDiskCapacity = 0;
  95. calculateCacheSizes(cacheModel, physicalMemorySizeInMegabytes, freeVolumeSpace,
  96. cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
  97. pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);
  98. if (diskCache)
  99. diskCache->setMaximumCacheSize(urlCacheDiskCapacity);
  100. memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
  101. memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
  102. pageCache()->setCapacity(pageCacheCapacity);
  103. // FIXME: Implement hybrid in-memory- and disk-caching as e.g. the Mac port does.
  104. }
  105. void WebProcess::platformClearResourceCaches(ResourceCachesToClear)
  106. {
  107. }
  108. #if defined(Q_OS_MACX)
  109. static void parentProcessDiedCallback(void*)
  110. {
  111. QCoreApplication::quit();
  112. }
  113. #endif
  114. void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, CoreIPC::MessageDecoder&)
  115. {
  116. #if ENABLE(SECCOMP_FILTERS)
  117. {
  118. WebKit::SeccompFiltersWebProcessQt seccompFilters(parameters);
  119. seccompFilters.initialize();
  120. }
  121. #endif
  122. m_networkAccessManager = new QtNetworkAccessManager(this);
  123. if (!parameters.cookieStorageDirectory.isEmpty()) {
  124. WebCore::SharedCookieJarQt* jar = WebCore::SharedCookieJarQt::create(parameters.cookieStorageDirectory);
  125. m_networkAccessManager->setCookieJar(jar);
  126. // Do not let QNetworkAccessManager delete the jar.
  127. jar->setParent(0);
  128. }
  129. if (!parameters.diskCacheDirectory.isEmpty()) {
  130. QNetworkDiskCache* diskCache = new QNetworkDiskCache();
  131. diskCache->setCacheDirectory(parameters.diskCacheDirectory);
  132. // The m_networkAccessManager takes ownership of the diskCache object upon the following call.
  133. m_networkAccessManager->setCache(diskCache);
  134. }
  135. #if defined(Q_OS_MACX)
  136. pid_t ppid = getppid();
  137. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  138. dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, ppid, DISPATCH_PROC_EXIT, queue);
  139. if (source) {
  140. dispatch_source_set_event_handler_f(source, parentProcessDiedCallback);
  141. dispatch_resume(source);
  142. }
  143. #endif
  144. WebCore::RuntimeEnabledFeatures::setSpeechInputEnabled(false);
  145. // We'll only install the Qt builtin bundle if we don't have one given by the UI process.
  146. // Currently only WTR provides its own bundle.
  147. if (parameters.injectedBundlePath.isEmpty()) {
  148. m_injectedBundle = InjectedBundle::create(String());
  149. m_injectedBundle->setSandboxExtension(SandboxExtension::create(parameters.injectedBundlePathExtensionHandle));
  150. QtBuiltinBundle::shared().initialize(toAPI(m_injectedBundle.get()));
  151. }
  152. }
  153. void WebProcess::platformTerminate()
  154. {
  155. delete m_networkAccessManager;
  156. m_networkAccessManager = 0;
  157. WebCore::SharedCookieJarQt::shared()->destroy();
  158. }
  159. } // namespace WebKit