WorkerContext.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2008, 2009 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 COMPUTER, 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 COMPUTER, 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. */
  26. #ifndef WorkerContext_h
  27. #define WorkerContext_h
  28. #if ENABLE(WORKERS)
  29. #include "ContentSecurityPolicy.h"
  30. #include "EventListener.h"
  31. #include "EventNames.h"
  32. #include "EventTarget.h"
  33. #include "GroupSettings.h"
  34. #include "ScriptExecutionContext.h"
  35. #include "WorkerEventQueue.h"
  36. #include "WorkerScriptController.h"
  37. #include <wtf/Assertions.h>
  38. #include <wtf/HashMap.h>
  39. #include <wtf/OwnPtr.h>
  40. #include <wtf/PassRefPtr.h>
  41. #include <wtf/RefCounted.h>
  42. #include <wtf/RefPtr.h>
  43. #include <wtf/text/AtomicStringHash.h>
  44. namespace WebCore {
  45. class Blob;
  46. class DOMURL;
  47. class ScheduledAction;
  48. class WorkerInspectorController;
  49. class WorkerLocation;
  50. class WorkerNavigator;
  51. class WorkerThread;
  52. class WorkerContext : public RefCounted<WorkerContext>, public ScriptExecutionContext, public EventTarget {
  53. public:
  54. virtual ~WorkerContext();
  55. virtual bool isWorkerContext() const OVERRIDE { return true; }
  56. virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
  57. virtual bool isSharedWorkerContext() const { return false; }
  58. virtual bool isDedicatedWorkerContext() const { return false; }
  59. const KURL& url() const { return m_url; }
  60. KURL completeURL(const String&) const;
  61. const GroupSettings* groupSettings() { return m_groupSettings.get(); }
  62. virtual String userAgent(const KURL&) const;
  63. virtual void disableEval(const String& errorMessage) OVERRIDE;
  64. WorkerScriptController* script() { return m_script.get(); }
  65. void clearScript() { m_script.clear(); }
  66. #if ENABLE(INSPECTOR)
  67. void clearInspector();
  68. #endif
  69. WorkerThread* thread() const { return m_thread; }
  70. bool hasPendingActivity() const;
  71. virtual void postTask(PassOwnPtr<Task>) OVERRIDE; // Executes the task on context's thread asynchronously.
  72. // WorkerGlobalScope
  73. WorkerContext* self() { return this; }
  74. WorkerLocation* location() const;
  75. void close();
  76. DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
  77. // WorkerUtils
  78. virtual void importScripts(const Vector<String>& urls, ExceptionCode&);
  79. WorkerNavigator* navigator() const;
  80. // Timers
  81. int setTimeout(PassOwnPtr<ScheduledAction>, int timeout);
  82. void clearTimeout(int timeoutId);
  83. int setInterval(PassOwnPtr<ScheduledAction>, int timeout);
  84. void clearInterval(int timeoutId);
  85. // ScriptExecutionContext
  86. virtual WorkerEventQueue* eventQueue() const OVERRIDE;
  87. virtual bool isContextThread() const OVERRIDE;
  88. virtual bool isJSExecutionForbidden() const OVERRIDE;
  89. #if ENABLE(INSPECTOR)
  90. WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
  91. #endif
  92. // These methods are used for GC marking. See JSWorkerContext::visitChildrenVirtual(SlotVisitor&) in
  93. // JSWorkerContextCustom.cpp.
  94. WorkerNavigator* optionalNavigator() const { return m_navigator.get(); }
  95. WorkerLocation* optionalLocation() const { return m_location.get(); }
  96. using RefCounted<WorkerContext>::ref;
  97. using RefCounted<WorkerContext>::deref;
  98. bool isClosing() { return m_closing; }
  99. // An observer interface to be notified when the worker thread is getting stopped.
  100. class Observer {
  101. WTF_MAKE_NONCOPYABLE(Observer);
  102. public:
  103. Observer(WorkerContext*);
  104. virtual ~Observer();
  105. virtual void notifyStop() = 0;
  106. void stopObserving();
  107. private:
  108. WorkerContext* m_context;
  109. };
  110. friend class Observer;
  111. void registerObserver(Observer*);
  112. void unregisterObserver(Observer*);
  113. void notifyObserversOfStop();
  114. virtual SecurityOrigin* topOrigin() const OVERRIDE { return m_topOrigin.get(); }
  115. protected:
  116. WorkerContext(const KURL&, const String& userAgent, PassOwnPtr<GroupSettings>, WorkerThread*, PassRefPtr<SecurityOrigin> topOrigin);
  117. void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType);
  118. virtual void logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>) OVERRIDE;
  119. void addMessageToWorkerConsole(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack>, ScriptState* = 0, unsigned long requestIdentifier = 0);
  120. private:
  121. virtual void refScriptExecutionContext() OVERRIDE { ref(); }
  122. virtual void derefScriptExecutionContext() OVERRIDE { deref(); }
  123. virtual void refEventTarget() OVERRIDE { ref(); }
  124. virtual void derefEventTarget() OVERRIDE { deref(); }
  125. virtual EventTargetData* eventTargetData() OVERRIDE;
  126. virtual EventTargetData* ensureEventTargetData() OVERRIDE;
  127. virtual const KURL& virtualURL() const OVERRIDE;
  128. virtual KURL virtualCompleteURL(const String&) const;
  129. virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack>, ScriptState* = 0, unsigned long requestIdentifier = 0) OVERRIDE;
  130. virtual void addConsoleMessage(MessageSource, MessageLevel, const String& message, unsigned long requestIdentifier = 0) OVERRIDE;
  131. virtual EventTarget* errorEventTarget() OVERRIDE;
  132. KURL m_url;
  133. String m_userAgent;
  134. OwnPtr<GroupSettings> m_groupSettings;
  135. mutable RefPtr<WorkerLocation> m_location;
  136. mutable RefPtr<WorkerNavigator> m_navigator;
  137. OwnPtr<WorkerScriptController> m_script;
  138. WorkerThread* m_thread;
  139. #if ENABLE(BLOB)
  140. mutable RefPtr<DOMURL> m_domURL;
  141. #endif
  142. #if ENABLE(INSPECTOR)
  143. OwnPtr<WorkerInspectorController> m_workerInspectorController;
  144. #endif
  145. bool m_closing;
  146. EventTargetData m_eventTargetData;
  147. HashSet<Observer*> m_workerObservers;
  148. OwnPtr<WorkerEventQueue> m_eventQueue;
  149. RefPtr<SecurityOrigin> m_topOrigin;
  150. };
  151. } // namespace WebCore
  152. #endif // ENABLE(WORKERS)
  153. #endif // WorkerContext_h