Changeset 140589 in webkit
- Timestamp:
- Jan 23, 2013, 2:15:54 PM (12 years ago)
- Location:
- trunk/Source
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r140579 r140589 1 2013-01-23 Adam Barth <abarth@webkit.org> 2 3 BackgroundHTMLParser::sendTokensToMainThread should use bind 4 https://bugs.webkit.org/show_bug.cgi?id=107637 5 6 Reviewed by Eric Seidel. 7 8 * wtf/Functional.h: 9 - I had to re-work the approach to validating WeakPtr |this| 10 arguments a bit. Previously you couldn't pass a WeakPtr as a 11 non-|this| argument to a function because we would try to unwrap 12 it into a raw pointer. 13 * wtf/WeakPtr.h: 14 (WTF::WeakPtrFactory::revokeAll): 15 (WeakPtrFactory): 16 - Let clients revoke all outstanding WeakPtrs without needing to 17 destroy the WeakPtrFactory. 18 * wtf/chromium/MainThreadChromium.cpp: 19 (WTF::callFunctionObject): 20 (WTF::callOnMainThread): 21 - Implement callOnMainThread for Function objects. The non-Chromium 22 implementation of MainThread.cpp already implements this feature. 23 1 24 2013-01-23 Justin Schuh <jschuh@chromium.org> 2 25 -
trunk/Source/WTF/wtf/Functional.h
r140040 r140589 84 84 typedef R ResultType; 85 85 static const bool shouldRefFirstParameter = false; 86 static const bool shouldValidateFirstParameter = false;87 86 88 87 explicit FunctionWrapper(R (*function)()) … … 105 104 typedef R ResultType; 106 105 static const bool shouldRefFirstParameter = false; 107 static const bool shouldValidateFirstParameter = false;108 106 109 107 explicit FunctionWrapper(R (*function)(P1)) … … 126 124 typedef R ResultType; 127 125 static const bool shouldRefFirstParameter = false; 128 static const bool shouldValidateFirstParameter = false;129 126 130 127 explicit FunctionWrapper(R (*function)(P1, P2)) … … 147 144 typedef R ResultType; 148 145 static const bool shouldRefFirstParameter = false; 149 static const bool shouldValidateFirstParameter = false;150 146 151 147 explicit FunctionWrapper(R (*function)(P1, P2, P3)) … … 168 164 typedef R ResultType; 169 165 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 170 static const bool shouldValidateFirstParameter = true;171 166 172 167 explicit FunctionWrapper(R (C::*function)()) … … 178 173 { 179 174 return (c->*m_function)(); 175 } 176 177 R operator()(const WeakPtr<C>& c) 178 { 179 C* obj = c.get(); 180 if (!obj) 181 return R(); 182 return (obj->*m_function)(); 180 183 } 181 184 … … 189 192 typedef R ResultType; 190 193 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 191 static const bool shouldValidateFirstParameter = true;192 194 193 195 explicit FunctionWrapper(R (C::*function)(P1)) … … 199 201 { 200 202 return (c->*m_function)(p1); 203 } 204 205 R operator()(const WeakPtr<C>& c, P1 p1) 206 { 207 C* obj = c.get(); 208 if (!obj) 209 return R(); 210 return (obj->*m_function)(p1); 201 211 } 202 212 … … 210 220 typedef R ResultType; 211 221 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 212 static const bool shouldValidateFirstParameter = true;213 222 214 223 explicit FunctionWrapper(R (C::*function)(P1, P2)) … … 220 229 { 221 230 return (c->*m_function)(p1, p2); 231 } 232 233 R operator()(const WeakPtr<C>& c, P1 p1, P2 p2) 234 { 235 C* obj = c.get(); 236 if (!obj) 237 return R(); 238 return (obj->*m_function)(p1, p2); 222 239 } 223 240 … … 231 248 typedef R ResultType; 232 249 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 233 static const bool shouldValidateFirstParameter = true;234 250 235 251 explicit FunctionWrapper(R (C::*function)(P1, P2, P3)) … … 241 257 { 242 258 return (c->*m_function)(p1, p2, p3); 259 } 260 261 R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3) 262 { 263 C* obj = c.get(); 264 if (!obj) 265 return R(); 266 return (obj->*m_function)(p1, p2, p3); 243 267 } 244 268 … … 252 276 typedef R ResultType; 253 277 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 254 static const bool shouldValidateFirstParameter = true;255 278 256 279 explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4)) … … 262 285 { 263 286 return (c->*m_function)(p1, p2, p3, p4); 287 } 288 289 R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3, P4 p4) 290 { 291 C* obj = c.get(); 292 if (!obj) 293 return R(); 294 return (obj->*m_function)(p1, p2, p3, p4); 264 295 } 265 296 … … 273 304 typedef R ResultType; 274 305 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 275 static const bool shouldValidateFirstParameter = true;276 306 277 307 explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4, P5)) … … 283 313 { 284 314 return (c->*m_function)(p1, p2, p3, p4, p5); 315 } 316 317 R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) 318 { 319 C* obj = c.get(); 320 if (!obj) 321 return R(); 322 return (obj->*m_function)(p1, p2, p3, p4, p5); 285 323 } 286 324 … … 295 333 typedef R ResultType; 296 334 static const bool shouldRefFirstParameter = false; 297 static const bool shouldValidateFirstParameter = false;298 335 299 336 explicit FunctionWrapper(R (^block)()) … … 336 373 337 374 static StorageType wrap(const T& value) { return value; } 338 static bool validate(const StorageType&) { return true; }339 375 static const T& unwrap(const StorageType& value) { return value; } 340 376 }; … … 344 380 345 381 static StorageType wrap(PassRefPtr<T> value) { return value; } 346 static bool validate(const StorageType&) { return true; }347 382 static T* unwrap(const StorageType& value) { return value.get(); } 348 383 }; … … 352 387 353 388 static StorageType wrap(RefPtr<T> value) { return value.release(); } 354 static bool validate(const StorageType&) { return true; }355 389 static T* unwrap(const StorageType& value) { return value.get(); } 356 390 }; 357 358 template<typename T> struct ParamStorageTraits<WeakPtr<T> > {359 typedef WeakPtr<T> StorageType;360 361 static StorageType wrap(WeakPtr<T> value) { return value; }362 static bool validate(const StorageType& value) { return value.get(); }363 static T* unwrap(const StorageType& value) { return value.get(); }364 };365 366 391 367 392 template<typename> class RetainPtr; … … 371 396 372 397 static StorageType wrap(const RetainPtr<T>& value) { return value; } 373 static bool validate(const StorageType&) { return true; }374 398 static typename RetainPtr<T>::PtrType unwrap(const StorageType& value) { return value.get(); } 375 399 }; … … 426 450 virtual typename FunctionWrapper::ResultType operator()() 427 451 { 428 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))429 return typename FunctionWrapper::ResultType();430 452 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1)); 431 453 } … … 454 476 virtual typename FunctionWrapper::ResultType operator()() 455 477 { 456 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))457 return typename FunctionWrapper::ResultType();458 478 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2)); 459 479 } … … 484 504 virtual typename FunctionWrapper::ResultType operator()() 485 505 { 486 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))487 return typename FunctionWrapper::ResultType();488 506 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3)); 489 507 } … … 516 534 virtual typename FunctionWrapper::ResultType operator()() 517 535 { 518 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))519 return typename FunctionWrapper::ResultType();520 536 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4)); 521 537 } … … 550 566 virtual typename FunctionWrapper::ResultType operator()() 551 567 { 552 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))553 return typename FunctionWrapper::ResultType();554 568 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5)); 555 569 } … … 586 600 virtual typename FunctionWrapper::ResultType operator()() 587 601 { 588 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))589 return typename FunctionWrapper::ResultType();590 602 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5), ParamStorageTraits<P6>::unwrap(m_p6)); 591 603 } -
trunk/Source/WTF/wtf/WeakPtr.h
r139780 r140589 97 97 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } 98 98 99 void revokeAll() 100 { 101 T* ptr = m_ref->get(); 102 m_ref->clear(); 103 // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs. 104 m_ref = Internal::WeakReference<T>::create(ptr); 105 } 106 99 107 private: 100 108 RefPtr<Internal::WeakReference<T> > m_ref; -
trunk/Source/WTF/wtf/chromium/MainThreadChromium.cpp
r111778 r140589 35 35 #include "ChromiumThreading.h" 36 36 #include "Threading.h" 37 #include <wtf/Functional.h> 37 38 38 39 namespace WTF { … … 55 56 } 56 57 58 static void callFunctionObject(void* context) 59 { 60 Function<void()>* function = static_cast<Function<void()>*>(context); 61 (*function)(); 62 delete function; 63 } 64 65 void callOnMainThread(const Function<void()>& function) 66 { 67 callOnMainThread(callFunctionObject, new Function<void()>(function)); 68 } 69 57 70 void callOnMainThreadAndWait(MainThreadFunction*, void*) 58 71 { -
trunk/Source/WebCore/ChangeLog
r140588 r140589 1 2013-01-23 Adam Barth <abarth@webkit.org> 2 3 BackgroundHTMLParser::sendTokensToMainThread should use bind 4 https://bugs.webkit.org/show_bug.cgi?id=107637 5 6 Reviewed by Eric Seidel. 7 8 This patch replaces our hand-written implementation of bind for 9 didReceiveTokensFromBackgroundParser with bind from Functional.h. To 10 use the generic version of bind, we need to switch to using WeakPtr to 11 hold a reference to the main thread parser in the BackgroundHTMLParser. 12 13 * html/parser/BackgroundHTMLParser.cpp: 14 (WebCore::BackgroundHTMLParser::BackgroundHTMLParser): 15 (WebCore::BackgroundHTMLParser::sendTokensToMainThread): 16 (WebCore::BackgroundHTMLParser::createPartial): 17 * html/parser/BackgroundHTMLParser.h: 18 (WebCore::BackgroundHTMLParser::create): 19 (BackgroundHTMLParser): 20 (ParserMap): 21 * html/parser/HTMLDocumentParser.cpp: 22 (WebCore::HTMLDocumentParser::HTMLDocumentParser): 23 (WebCore::HTMLDocumentParser::startBackgroundParser): 24 (WebCore::HTMLDocumentParser::stopBackgroundParser): 25 * html/parser/HTMLDocumentParser.h: 26 (HTMLDocumentParser): 27 1 28 2013-01-23 Roger Fong <roger_fong@apple.com> 2 29 -
trunk/Source/WebCore/html/parser/BackgroundHTMLParser.cpp
r140478 r140589 69 69 } 70 70 71 typedef const void* ParserIdentifier;72 class HTMLDocumentParser;73 74 71 ParserMap& parserMap() 75 72 { … … 86 83 } 87 84 88 ParserMap::MainThreadParserMap& ParserMap::mainThreadParsers() 89 { 90 ASSERT(isMainThread()); 91 return m_mainThreadParsers; 92 } 93 94 BackgroundHTMLParser::BackgroundHTMLParser(const HTMLParserOptions& options, ParserIdentifier identifier) 85 BackgroundHTMLParser::BackgroundHTMLParser(const HTMLParserOptions& options, WeakPtr<HTMLDocumentParser> parser) 95 86 : m_inForeignContent(false) 96 87 , m_tokenizer(HTMLTokenizer::create(options)) 97 88 , m_options(options) 98 , m_parser Identifer(identifier)89 , m_parser(parser) 99 90 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) 100 91 { … … 174 165 } 175 166 176 class TokenDelivery {177 WTF_MAKE_NONCOPYABLE(TokenDelivery);178 public:179 TokenDelivery()180 : identifier(0)181 {182 }183 184 ParserIdentifier identifier;185 OwnPtr<CompactHTMLTokenStream> tokens;186 static void execute(void* context)187 {188 TokenDelivery* delivery = static_cast<TokenDelivery*>(context);189 HTMLDocumentParser* parser = parserMap().mainThreadParsers().get(delivery->identifier);190 if (parser)191 parser->didReceiveTokensFromBackgroundParser(delivery->tokens.release());192 // FIXME: Ideally we wouldn't need to call delete manually. Instead193 // we would like an API where the message queue owns the tasks and194 // takes care of deleting them.195 delete delivery;196 }197 };198 199 167 void BackgroundHTMLParser::sendTokensToMainThread() 200 168 { … … 206 174 #endif 207 175 208 TokenDelivery* delivery = new TokenDelivery; 209 delivery->identifier = m_parserIdentifer; 210 delivery->tokens = m_pendingTokens.release(); 211 callOnMainThread(TokenDelivery::execute, delivery); 176 callOnMainThread(bind(&HTMLDocumentParser::didReceiveTokensFromBackgroundParser, m_parser, m_pendingTokens.release())); 212 177 213 178 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); 214 179 } 215 180 216 void BackgroundHTMLParser::createPartial(ParserIdentifier identifier, HTMLParserOptions options )181 void BackgroundHTMLParser::createPartial(ParserIdentifier identifier, HTMLParserOptions options, WeakPtr<HTMLDocumentParser> parser) 217 182 { 218 183 ASSERT(!parserMap().backgroundParsers().get(identifier)); 219 parserMap().backgroundParsers().set(identifier, BackgroundHTMLParser::create(options, identifier));184 parserMap().backgroundParsers().set(identifier, BackgroundHTMLParser::create(options, parser)); 220 185 } 221 186 -
trunk/Source/WebCore/html/parser/BackgroundHTMLParser.h
r140478 r140589 33 33 #include "HTMLToken.h" 34 34 #include "HTMLTokenizer.h" 35 #include <wtf/WeakPtr.h> 35 36 36 37 namespace WebCore { … … 45 46 void finish(); 46 47 47 static PassOwnPtr<BackgroundHTMLParser> create(const HTMLParserOptions& options, ParserIdentifier identifier)48 static PassOwnPtr<BackgroundHTMLParser> create(const HTMLParserOptions& options, WeakPtr<HTMLDocumentParser> parser) 48 49 { 49 return adoptPtr(new BackgroundHTMLParser(options, identifier));50 return adoptPtr(new BackgroundHTMLParser(options, parser)); 50 51 } 51 52 52 static void createPartial(ParserIdentifier, HTMLParserOptions );53 static void createPartial(ParserIdentifier, HTMLParserOptions, WeakPtr<HTMLDocumentParser>); 53 54 static void stopPartial(ParserIdentifier); 54 55 static void appendPartial(ParserIdentifier, const String& input); … … 56 57 57 58 private: 58 explicit BackgroundHTMLParser(const HTMLParserOptions&, ParserIdentifier);59 BackgroundHTMLParser(const HTMLParserOptions&, WeakPtr<HTMLDocumentParser>); 59 60 60 61 void markEndOfFile(); … … 69 70 OwnPtr<HTMLTokenizer> m_tokenizer; 70 71 HTMLParserOptions m_options; 71 ParserIdentifier m_parserIdentifer;72 WeakPtr<HTMLDocumentParser> m_parser; 72 73 OwnPtr<CompactHTMLTokenStream> m_pendingTokens; 73 74 }; … … 81 82 82 83 typedef HashMap<ParserIdentifier, OwnPtr<BackgroundHTMLParser> > BackgroundParserMap; 83 typedef HashMap<ParserIdentifier, HTMLDocumentParser*> MainThreadParserMap;84 84 85 85 BackgroundParserMap& backgroundParsers(); 86 MainThreadParserMap& mainThreadParsers();87 86 88 87 private: 89 88 BackgroundParserMap m_backgroundParsers; 90 MainThreadParserMap m_mainThreadParsers;91 89 }; 92 90 -
trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp
r140559 r140589 83 83 , m_parserScheduler(HTMLParserScheduler::create(this)) 84 84 , m_xssAuditor(this) 85 #if ENABLE(THREADED_HTML_PARSER) 86 , m_weakFactory(this) 87 #endif 85 88 , m_endWasDelayed(false) 86 89 , m_haveBackgroundParser(false) … … 97 100 , m_treeBuilder(HTMLTreeBuilder::create(this, fragment, contextElement, scriptingPermission, m_options)) 98 101 , m_xssAuditor(this) 102 #if ENABLE(THREADED_HTML_PARSER) 103 , m_weakFactory(this) 104 #endif 99 105 , m_endWasDelayed(false) 100 106 , m_haveBackgroundParser(false) … … 457 463 458 464 ParserIdentifier identifier = ParserMap::identifierForParser(this); 459 parserMap().mainThreadParsers().set(identifier, this); 460 461 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::createPartial, identifier, m_options)); 465 WeakPtr<HTMLDocumentParser> parser = m_weakFactory.createWeakPtr(); 466 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::createPartial, identifier, m_options, parser)); 462 467 } 463 468 … … 470 475 ParserIdentifier identifier = ParserMap::identifierForParser(this); 471 476 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::stopPartial, identifier)); 472 473 parserMap().mainThreadParsers().set(identifier, 0); 474 // We will not recieve any messages from the parser after this point. 477 m_weakFactory.revokeAll(); 475 478 } 476 479 -
trunk/Source/WebCore/html/parser/HTMLDocumentParser.h
r140478 r140589 40 40 #include <wtf/Deque.h> 41 41 #include <wtf/OwnPtr.h> 42 #include <wtf/WeakPtr.h> 42 43 #include <wtf/text/TextPosition.h> 43 44 … … 172 173 #if ENABLE(THREADED_HTML_PARSER) 173 174 Deque<OwnPtr<CompactHTMLTokenStream> > m_pendingTokens; 175 WeakPtrFactory<HTMLDocumentParser> m_weakFactory; 174 176 #endif 175 177
Note:
See TracChangeset
for help on using the changeset viewer.