InternalSettings.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (C) 2012 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
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "InternalSettings.h"
  28. #include "CaptionUserPreferences.h"
  29. #include "Document.h"
  30. #include "ExceptionCode.h"
  31. #include "Frame.h"
  32. #include "FrameView.h"
  33. #include "Language.h"
  34. #include "LocaleToScriptMapping.h"
  35. #include "Page.h"
  36. #include "PageGroup.h"
  37. #include "RuntimeEnabledFeatures.h"
  38. #include "Settings.h"
  39. #include "Supplementable.h"
  40. #include "TextRun.h"
  41. #if ENABLE(INPUT_TYPE_COLOR)
  42. #include "ColorChooser.h"
  43. #endif
  44. #define InternalSettingsGuardForSettingsReturn(returnValue) \
  45. if (!settings()) { \
  46. ec = INVALID_ACCESS_ERR; \
  47. return returnValue; \
  48. }
  49. #define InternalSettingsGuardForSettings() \
  50. if (!settings()) { \
  51. ec = INVALID_ACCESS_ERR; \
  52. return; \
  53. }
  54. #define InternalSettingsGuardForPage() \
  55. if (!page()) { \
  56. ec = INVALID_ACCESS_ERR; \
  57. return; \
  58. }
  59. namespace WebCore {
  60. InternalSettings::Backup::Backup(Settings* settings)
  61. : m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::cssExclusionsEnabled())
  62. , m_originalCSSShapesEnabled(RuntimeEnabledFeatures::cssShapesEnabled())
  63. , m_originalCSSVariablesEnabled(settings->cssVariablesEnabled())
  64. #if ENABLE(SHADOW_DOM)
  65. , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::shadowDOMEnabled())
  66. , m_originalAuthorShadowDOMForAnyElementEnabled(RuntimeEnabledFeatures::authorShadowDOMForAnyElementEnabled())
  67. #endif
  68. #if ENABLE(STYLE_SCOPED)
  69. , m_originalStyleScoped(RuntimeEnabledFeatures::styleScopedEnabled())
  70. #endif
  71. , m_originalEditingBehavior(settings->editingBehaviorType())
  72. #if ENABLE(TEXT_AUTOSIZING)
  73. , m_originalTextAutosizingEnabled(settings->textAutosizingEnabled())
  74. , m_originalTextAutosizingWindowSizeOverride(settings->textAutosizingWindowSizeOverride())
  75. , m_originalTextAutosizingFontScaleFactor(settings->textAutosizingFontScaleFactor())
  76. #endif
  77. , m_originalMediaTypeOverride(settings->mediaTypeOverride())
  78. #if ENABLE(DIALOG_ELEMENT)
  79. , m_originalDialogElementEnabled(RuntimeEnabledFeatures::dialogElementEnabled())
  80. #endif
  81. , m_originalCanvasUsesAcceleratedDrawing(settings->canvasUsesAcceleratedDrawing())
  82. , m_originalMockScrollbarsEnabled(settings->mockScrollbarsEnabled())
  83. , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::langAttributeAwareFormControlUIEnabled())
  84. , m_imagesEnabled(settings->areImagesEnabled())
  85. , m_minimumTimerInterval(settings->minDOMTimerInterval())
  86. #if ENABLE(VIDEO_TRACK)
  87. , m_shouldDisplaySubtitles(settings->shouldDisplaySubtitles())
  88. , m_shouldDisplayCaptions(settings->shouldDisplayCaptions())
  89. , m_shouldDisplayTextDescriptions(settings->shouldDisplayTextDescriptions())
  90. #endif
  91. , m_defaultVideoPosterURL(settings->defaultVideoPosterURL())
  92. , m_originalTimeWithoutMouseMovementBeforeHidingControls(settings->timeWithoutMouseMovementBeforeHidingControls())
  93. , m_useLegacyBackgroundSizeShorthandBehavior(settings->useLegacyBackgroundSizeShorthandBehavior())
  94. {
  95. }
  96. void InternalSettings::Backup::restoreTo(Settings* settings)
  97. {
  98. RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
  99. RuntimeEnabledFeatures::setCSSShapesEnabled(m_originalCSSShapesEnabled);
  100. settings->setCSSVariablesEnabled(m_originalCSSVariablesEnabled);
  101. #if ENABLE(SHADOW_DOM)
  102. RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
  103. RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(m_originalAuthorShadowDOMForAnyElementEnabled);
  104. #endif
  105. #if ENABLE(STYLE_SCOPED)
  106. RuntimeEnabledFeatures::setStyleScopedEnabled(m_originalStyleScoped);
  107. #endif
  108. settings->setEditingBehaviorType(m_originalEditingBehavior);
  109. #if ENABLE(TEXT_AUTOSIZING)
  110. settings->setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
  111. settings->setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
  112. settings->setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
  113. #endif
  114. settings->setMediaTypeOverride(m_originalMediaTypeOverride);
  115. #if ENABLE(DIALOG_ELEMENT)
  116. RuntimeEnabledFeatures::setDialogElementEnabled(m_originalDialogElementEnabled);
  117. #endif
  118. settings->setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
  119. settings->setMockScrollbarsEnabled(m_originalMockScrollbarsEnabled);
  120. RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
  121. settings->setImagesEnabled(m_imagesEnabled);
  122. settings->setMinDOMTimerInterval(m_minimumTimerInterval);
  123. #if ENABLE(VIDEO_TRACK)
  124. settings->setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
  125. settings->setShouldDisplayCaptions(m_shouldDisplayCaptions);
  126. settings->setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
  127. #endif
  128. settings->setDefaultVideoPosterURL(m_defaultVideoPosterURL);
  129. settings->setTimeWithoutMouseMovementBeforeHidingControls(m_originalTimeWithoutMouseMovementBeforeHidingControls);
  130. settings->setUseLegacyBackgroundSizeShorthandBehavior(m_useLegacyBackgroundSizeShorthandBehavior);
  131. }
  132. // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
  133. // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
  134. // Instead, we manually make InternalSettings supplement Page.
  135. class InternalSettingsWrapper : public Supplement<Page> {
  136. public:
  137. explicit InternalSettingsWrapper(Page* page)
  138. : m_internalSettings(InternalSettings::create(page)) { }
  139. virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
  140. #if !ASSERT_DISABLED
  141. virtual bool isRefCountedWrapper() const OVERRIDE { return true; }
  142. #endif
  143. InternalSettings* internalSettings() const { return m_internalSettings.get(); }
  144. private:
  145. RefPtr<InternalSettings> m_internalSettings;
  146. };
  147. const char* InternalSettings::supplementName()
  148. {
  149. return "InternalSettings";
  150. }
  151. InternalSettings* InternalSettings::from(Page* page)
  152. {
  153. if (!Supplement<Page>::from(page, supplementName()))
  154. Supplement<Page>::provideTo(page, supplementName(), adoptPtr(new InternalSettingsWrapper(page)));
  155. return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
  156. }
  157. InternalSettings::~InternalSettings()
  158. {
  159. }
  160. InternalSettings::InternalSettings(Page* page)
  161. : InternalSettingsGenerated(page)
  162. , m_page(page)
  163. , m_backup(page->settings())
  164. {
  165. }
  166. void InternalSettings::resetToConsistentState()
  167. {
  168. page()->setPageScaleFactor(1, IntPoint(0, 0));
  169. page()->setCanStartMedia(true);
  170. m_backup.restoreTo(settings());
  171. m_backup = Backup(settings());
  172. InternalSettingsGenerated::resetToConsistentState();
  173. }
  174. Settings* InternalSettings::settings() const
  175. {
  176. if (!page())
  177. return 0;
  178. return page()->settings();
  179. }
  180. void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
  181. {
  182. InternalSettingsGuardForSettings();
  183. settings()->setMockScrollbarsEnabled(enabled);
  184. }
  185. static bool urlIsWhitelistedForSetShadowDOMEnabled(const String& url)
  186. {
  187. // This check is just for preventing fuzzers from crashing because of unintended API calls.
  188. // You can list your test if needed.
  189. return notFound != url.find("fast/dom/shadow/content-shadow-unknown.html")
  190. || notFound != url.find("fast/dom/shadow/insertion-points-with-shadow-disabled.html");
  191. }
  192. void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
  193. {
  194. if (!urlIsWhitelistedForSetShadowDOMEnabled(page()->mainFrame()->document()->url().string())) {
  195. ec = INVALID_ACCESS_ERR;
  196. return;
  197. }
  198. #if ENABLE(SHADOW_DOM)
  199. RuntimeEnabledFeatures::setShadowDOMEnabled(enabled);
  200. #else
  201. // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
  202. // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
  203. if (enabled)
  204. ec = INVALID_ACCESS_ERR;
  205. #endif
  206. }
  207. void InternalSettings::setAuthorShadowDOMForAnyElementEnabled(bool isEnabled)
  208. {
  209. #if ENABLE(SHADOW_DOM)
  210. RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(isEnabled);
  211. #else
  212. UNUSED_PARAM(isEnabled);
  213. #endif
  214. }
  215. void InternalSettings::setStyleScopedEnabled(bool enabled)
  216. {
  217. #if ENABLE(STYLE_SCOPED)
  218. RuntimeEnabledFeatures::setStyleScopedEnabled(enabled);
  219. #else
  220. UNUSED_PARAM(enabled);
  221. #endif
  222. }
  223. void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
  224. {
  225. #if ENABLE(TOUCH_EVENTS)
  226. InternalSettingsGuardForSettings();
  227. settings()->setTouchEventEmulationEnabled(enabled);
  228. #else
  229. UNUSED_PARAM(enabled);
  230. UNUSED_PARAM(ec);
  231. #endif
  232. }
  233. typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
  234. static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
  235. {
  236. UScriptCode code = scriptNameToCode(script);
  237. if (code != USCRIPT_INVALID_CODE)
  238. (settings->*setter)(family, code);
  239. }
  240. void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
  241. {
  242. InternalSettingsGuardForSettings();
  243. setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
  244. }
  245. void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
  246. {
  247. InternalSettingsGuardForSettings();
  248. setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
  249. }
  250. void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
  251. {
  252. InternalSettingsGuardForSettings();
  253. setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
  254. }
  255. void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
  256. {
  257. InternalSettingsGuardForSettings();
  258. setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
  259. }
  260. void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
  261. {
  262. InternalSettingsGuardForSettings();
  263. setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
  264. }
  265. void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
  266. {
  267. InternalSettingsGuardForSettings();
  268. setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
  269. }
  270. void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
  271. {
  272. InternalSettingsGuardForSettings();
  273. setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
  274. }
  275. void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
  276. {
  277. #if ENABLE(TEXT_AUTOSIZING)
  278. InternalSettingsGuardForSettings();
  279. settings()->setTextAutosizingEnabled(enabled);
  280. #else
  281. UNUSED_PARAM(enabled);
  282. UNUSED_PARAM(ec);
  283. #endif
  284. }
  285. void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
  286. {
  287. #if ENABLE(TEXT_AUTOSIZING)
  288. InternalSettingsGuardForSettings();
  289. settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
  290. #else
  291. UNUSED_PARAM(width);
  292. UNUSED_PARAM(height);
  293. UNUSED_PARAM(ec);
  294. #endif
  295. }
  296. void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
  297. {
  298. InternalSettingsGuardForSettings();
  299. settings()->setMediaTypeOverride(mediaType);
  300. }
  301. void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
  302. {
  303. #if ENABLE(TEXT_AUTOSIZING)
  304. InternalSettingsGuardForSettings();
  305. settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
  306. #else
  307. UNUSED_PARAM(fontScaleFactor);
  308. UNUSED_PARAM(ec);
  309. #endif
  310. }
  311. void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
  312. {
  313. UNUSED_PARAM(ec);
  314. RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
  315. }
  316. void InternalSettings::setCSSShapesEnabled(bool enabled, ExceptionCode& ec)
  317. {
  318. UNUSED_PARAM(ec);
  319. RuntimeEnabledFeatures::setCSSShapesEnabled(enabled);
  320. }
  321. void InternalSettings::setCSSVariablesEnabled(bool enabled, ExceptionCode& ec)
  322. {
  323. InternalSettingsGuardForSettings();
  324. settings()->setCSSVariablesEnabled(enabled);
  325. }
  326. bool InternalSettings::cssVariablesEnabled(ExceptionCode& ec)
  327. {
  328. InternalSettingsGuardForSettingsReturn(false);
  329. return settings()->cssVariablesEnabled();
  330. }
  331. void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
  332. {
  333. InternalSettingsGuardForSettings();
  334. m_page->setCanStartMedia(enabled);
  335. }
  336. void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
  337. {
  338. InternalSettingsGuardForSettings();
  339. if (equalIgnoringCase(editingBehavior, "win"))
  340. settings()->setEditingBehaviorType(EditingWindowsBehavior);
  341. else if (equalIgnoringCase(editingBehavior, "mac"))
  342. settings()->setEditingBehaviorType(EditingMacBehavior);
  343. else if (equalIgnoringCase(editingBehavior, "unix"))
  344. settings()->setEditingBehaviorType(EditingUnixBehavior);
  345. else
  346. ec = SYNTAX_ERR;
  347. }
  348. void InternalSettings::setDialogElementEnabled(bool enabled, ExceptionCode& ec)
  349. {
  350. UNUSED_PARAM(ec);
  351. #if ENABLE(DIALOG_ELEMENT)
  352. RuntimeEnabledFeatures::setDialogElementEnabled(enabled);
  353. #else
  354. UNUSED_PARAM(enabled);
  355. #endif
  356. }
  357. void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
  358. {
  359. InternalSettingsGuardForSettings();
  360. #if ENABLE(VIDEO_TRACK)
  361. if (!page())
  362. return;
  363. CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
  364. if (equalIgnoringCase(kind, "Subtitles"))
  365. captionPreferences->setUserPrefersSubtitles(enabled);
  366. else if (equalIgnoringCase(kind, "Captions"))
  367. captionPreferences->setUserPrefersCaptions(enabled);
  368. else if (equalIgnoringCase(kind, "TextDescriptions"))
  369. captionPreferences->setUserPrefersTextDescriptions(enabled);
  370. else
  371. ec = SYNTAX_ERR;
  372. #else
  373. UNUSED_PARAM(kind);
  374. UNUSED_PARAM(enabled);
  375. #endif
  376. }
  377. bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
  378. {
  379. InternalSettingsGuardForSettingsReturn(false);
  380. #if ENABLE(VIDEO_TRACK)
  381. if (!page())
  382. return false;
  383. CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
  384. if (equalIgnoringCase(kind, "Subtitles"))
  385. return captionPreferences->userPrefersSubtitles();
  386. if (equalIgnoringCase(kind, "Captions"))
  387. return captionPreferences->userPrefersCaptions();
  388. if (equalIgnoringCase(kind, "TextDescriptions"))
  389. return captionPreferences->userPrefersTextDescriptions();
  390. ec = SYNTAX_ERR;
  391. return false;
  392. #else
  393. UNUSED_PARAM(kind);
  394. return false;
  395. #endif
  396. }
  397. void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
  398. {
  399. InternalSettingsGuardForSettings();
  400. if (mode == "AllowAll")
  401. settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
  402. else if (mode == "BlockThirdParty")
  403. settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
  404. else if (mode == "BlockAll")
  405. settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
  406. else
  407. ec = SYNTAX_ERR;
  408. }
  409. void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
  410. {
  411. RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(enabled);
  412. }
  413. void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
  414. {
  415. InternalSettingsGuardForSettings();
  416. settings()->setImagesEnabled(enabled);
  417. }
  418. void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
  419. {
  420. InternalSettingsGuardForSettings();
  421. settings()->setMinDOMTimerInterval(intervalInSeconds);
  422. }
  423. void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
  424. {
  425. InternalSettingsGuardForSettings();
  426. settings()->setDefaultVideoPosterURL(url);
  427. }
  428. void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
  429. {
  430. InternalSettingsGuardForSettings();
  431. settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
  432. }
  433. void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
  434. {
  435. InternalSettingsGuardForSettings();
  436. settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
  437. }
  438. }