ContentsScaleFactor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2011 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 "PluginTest.h"
  26. using namespace std;
  27. class ContentsScaleFactor : public PluginTest {
  28. public:
  29. ContentsScaleFactor(NPP npp, const string& identifier)
  30. : PluginTest(npp, identifier)
  31. , m_cachedContentsScaleFactor(1.0)
  32. {
  33. }
  34. private:
  35. double contentsScaleFactor()
  36. {
  37. double contentsScaleFactor = 1.0;
  38. NPN_GetValue(NPNVcontentsScaleFactor, &contentsScaleFactor);
  39. return contentsScaleFactor;
  40. }
  41. double cachedContentsScaleFactor()
  42. {
  43. return m_cachedContentsScaleFactor;
  44. }
  45. class ScriptableObject : public Object<ScriptableObject> {
  46. public:
  47. bool hasProperty(NPIdentifier propertyName)
  48. {
  49. return identifierIs(propertyName, "contentsScaleFactor")
  50. || identifierIs(propertyName, "cachedContentsScaleFactor");
  51. }
  52. bool getProperty(NPIdentifier propertyName, NPVariant* result)
  53. {
  54. if (identifierIs(propertyName, "contentsScaleFactor")) {
  55. DOUBLE_TO_NPVARIANT(pluginTest()->contentsScaleFactor(), *result);
  56. return true;
  57. }
  58. if (identifierIs(propertyName, "cachedContentsScaleFactor")) {
  59. DOUBLE_TO_NPVARIANT(pluginTest()->cachedContentsScaleFactor(), *result);
  60. return true;
  61. }
  62. return false;
  63. }
  64. private:
  65. ContentsScaleFactor* pluginTest() const { return static_cast<ContentsScaleFactor*>(Object<ScriptableObject>::pluginTest()); }
  66. };
  67. virtual NPError NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData *saved)
  68. {
  69. m_cachedContentsScaleFactor = contentsScaleFactor();
  70. return NPERR_NO_ERROR;
  71. }
  72. virtual NPError NPP_GetValue(NPPVariable variable, void* value)
  73. {
  74. if (variable != NPPVpluginScriptableNPObject)
  75. return NPERR_GENERIC_ERROR;
  76. *(NPObject**)value = ScriptableObject::create(this);
  77. return NPERR_NO_ERROR;
  78. }
  79. virtual NPError NPP_SetValue(NPNVariable variable, void* value)
  80. {
  81. switch (variable) {
  82. case NPNVcontentsScaleFactor:
  83. m_cachedContentsScaleFactor = *(double*)value;
  84. return NPERR_NO_ERROR;
  85. default:
  86. return NPERR_GENERIC_ERROR;
  87. }
  88. }
  89. double m_cachedContentsScaleFactor;
  90. };
  91. static PluginTest::Register<ContentsScaleFactor> contentsScaleFactor("contents-scale-factor");