WebDatabaseManagerClient.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2007,2012 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. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "WebDatabaseManagerClient.h"
  26. #if ENABLE(SQL_DATABASE)
  27. #import "WebDatabaseManagerPrivate.h"
  28. #import "WebSecurityOriginInternal.h"
  29. #import <wtf/MainThread.h>
  30. #import <wtf/RetainPtr.h>
  31. #import <WebCore/SecurityOrigin.h>
  32. using namespace WebCore;
  33. WebDatabaseManagerClient* WebDatabaseManagerClient::sharedWebDatabaseManagerClient()
  34. {
  35. static WebDatabaseManagerClient* sharedClient = new WebDatabaseManagerClient();
  36. return sharedClient;
  37. }
  38. WebDatabaseManagerClient::WebDatabaseManagerClient()
  39. {
  40. }
  41. WebDatabaseManagerClient::~WebDatabaseManagerClient()
  42. {
  43. }
  44. class DidModifyOriginData {
  45. WTF_MAKE_NONCOPYABLE(DidModifyOriginData);
  46. public:
  47. static void dispatchToMainThread(WebDatabaseManagerClient* client, SecurityOrigin* origin)
  48. {
  49. DidModifyOriginData* context = new DidModifyOriginData(client, origin->isolatedCopy());
  50. callOnMainThread(&DidModifyOriginData::dispatchDidModifyOriginOnMainThread, context);
  51. }
  52. private:
  53. DidModifyOriginData(WebDatabaseManagerClient* client, PassRefPtr<SecurityOrigin> origin)
  54. : client(client)
  55. , origin(origin)
  56. {
  57. }
  58. static void dispatchDidModifyOriginOnMainThread(void* context)
  59. {
  60. ASSERT(isMainThread());
  61. DidModifyOriginData* info = static_cast<DidModifyOriginData*>(context);
  62. info->client->dispatchDidModifyOrigin(info->origin.get());
  63. delete info;
  64. }
  65. WebDatabaseManagerClient* client;
  66. RefPtr<SecurityOrigin> origin;
  67. };
  68. void WebDatabaseManagerClient::dispatchDidModifyOrigin(SecurityOrigin* origin)
  69. {
  70. if (!isMainThread()) {
  71. DidModifyOriginData::dispatchToMainThread(this, origin);
  72. return;
  73. }
  74. RetainPtr<WebSecurityOrigin> webSecurityOrigin = adoptNS([[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:origin]);
  75. [[NSNotificationCenter defaultCenter] postNotificationName:WebDatabaseDidModifyOriginNotification
  76. object:webSecurityOrigin.get()];
  77. }
  78. void WebDatabaseManagerClient::dispatchDidModifyDatabase(SecurityOrigin* origin, const String& databaseIdentifier)
  79. {
  80. if (!isMainThread()) {
  81. DidModifyOriginData::dispatchToMainThread(this, origin);
  82. return;
  83. }
  84. RetainPtr<WebSecurityOrigin> webSecurityOrigin = adoptNS([[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:origin]);
  85. RetainPtr<NSDictionary> userInfo = adoptNS([[NSDictionary alloc]
  86. initWithObjectsAndKeys:(NSString *)databaseIdentifier, WebDatabaseIdentifierKey, nil]);
  87. [[NSNotificationCenter defaultCenter] postNotificationName:WebDatabaseDidModifyDatabaseNotification
  88. object:webSecurityOrigin.get()
  89. userInfo:userInfo.get()];
  90. }
  91. #endif