| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are met: | 5 * modification, are permitted provided that the following conditions are met: |
| 6 * | 6 * |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "core/frame/FrameView.h" | 43 #include "core/frame/FrameView.h" |
| 44 #include "core/platform/HistogramSupport.h" | 44 #include "core/platform/HistogramSupport.h" |
| 45 | 45 |
| 46 namespace WebCore { | 46 namespace WebCore { |
| 47 | 47 |
| 48 static const int defaultFontSize = 10; | 48 static const int defaultFontSize = 10; |
| 49 static const char* const defaultFontFamily = "sans-serif"; | 49 static const char* const defaultFontFamily = "sans-serif"; |
| 50 | 50 |
| 51 class LoadFontPromiseResolver : public CSSSegmentedFontFace::LoadFontCallback { | 51 class LoadFontPromiseResolver : public CSSSegmentedFontFace::LoadFontCallback { |
| 52 public: | 52 public: |
| 53 static PassRefPtr<LoadFontPromiseResolver> create(const FontFamily& family,
ExecutionContext* context) | 53 static PassRefPtr<LoadFontPromiseResolver> create(const FontFamily& family,
ScriptPromise promise, ExecutionContext* context) |
| 54 { | 54 { |
| 55 int numFamilies = 0; | 55 int numFamilies = 0; |
| 56 for (const FontFamily* f = &family; f; f = f->next()) | 56 for (const FontFamily* f = &family; f; f = f->next()) |
| 57 numFamilies++; | 57 numFamilies++; |
| 58 return adoptRef<LoadFontPromiseResolver>(new LoadFontPromiseResolver(num
Families, context)); | 58 return adoptRef<LoadFontPromiseResolver>(new LoadFontPromiseResolver(num
Families, promise, context)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 virtual void notifyLoaded(CSSSegmentedFontFace*) OVERRIDE; | 61 virtual void notifyLoaded(CSSSegmentedFontFace*) OVERRIDE; |
| 62 virtual void notifyError(CSSSegmentedFontFace*) OVERRIDE; | 62 virtual void notifyError(CSSSegmentedFontFace*) OVERRIDE; |
| 63 void loaded(Document*); | 63 void loaded(Document*); |
| 64 void error(Document*); | 64 void error(Document*); |
| 65 void resolve(); | 65 void resolve(); |
| 66 | 66 |
| 67 ScriptPromise promise() | |
| 68 { | |
| 69 ScriptPromise promise = m_resolver->promise(); | |
| 70 m_resolver->detachPromise(); | |
| 71 return promise; | |
| 72 } | |
| 73 | |
| 74 private: | 67 private: |
| 75 LoadFontPromiseResolver(int numLoading, ExecutionContext* context) | 68 LoadFontPromiseResolver(int numLoading, ScriptPromise promise, ExecutionCont
ext* context) |
| 76 : m_numLoading(numLoading) | 69 : m_numLoading(numLoading) |
| 77 , m_errorOccured(false) | 70 , m_errorOccured(false) |
| 78 , m_scriptState(ScriptState::current()) | 71 , m_scriptState(ScriptState::current()) |
| 79 , m_resolver(ScriptPromiseResolver::create(context)) | 72 , m_resolver(ScriptPromiseResolver::create(promise, context)) |
| 80 { } | 73 { } |
| 81 | 74 |
| 82 int m_numLoading; | 75 int m_numLoading; |
| 83 bool m_errorOccured; | 76 bool m_errorOccured; |
| 84 ScriptState* m_scriptState; | 77 ScriptState* m_scriptState; |
| 85 RefPtr<ScriptPromiseResolver> m_resolver; | 78 RefPtr<ScriptPromiseResolver> m_resolver; |
| 86 }; | 79 }; |
| 87 | 80 |
| 88 void LoadFontPromiseResolver::loaded(Document* document) | 81 void LoadFontPromiseResolver::loaded(Document* document) |
| 89 { | 82 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 109 { | 102 { |
| 110 error(face->fontSelector()->document()); | 103 error(face->fontSelector()->document()); |
| 111 } | 104 } |
| 112 | 105 |
| 113 void LoadFontPromiseResolver::resolve() | 106 void LoadFontPromiseResolver::resolve() |
| 114 { | 107 { |
| 115 ScriptScope scope(m_scriptState); | 108 ScriptScope scope(m_scriptState); |
| 116 if (m_errorOccured) | 109 if (m_errorOccured) |
| 117 m_resolver->reject(ScriptValue::createNull()); | 110 m_resolver->reject(ScriptValue::createNull()); |
| 118 else | 111 else |
| 119 m_resolver->fulfill(ScriptValue::createNull()); | 112 m_resolver->resolve(ScriptValue::createNull()); |
| 120 } | 113 } |
| 121 | 114 |
| 122 class FontsReadyPromiseResolver { | 115 class FontsReadyPromiseResolver { |
| 123 public: | 116 public: |
| 124 static PassOwnPtr<FontsReadyPromiseResolver> create(ExecutionContext* contex
t) | 117 static PassOwnPtr<FontsReadyPromiseResolver> create(ScriptPromise promise, E
xecutionContext* context) |
| 125 { | 118 { |
| 126 return adoptPtr(new FontsReadyPromiseResolver(context)); | 119 return adoptPtr(new FontsReadyPromiseResolver(promise, context)); |
| 127 } | 120 } |
| 128 | 121 |
| 129 void call(PassRefPtr<FontFaceSet> fontFaceSet) | 122 void call(PassRefPtr<FontFaceSet> fontFaceSet) |
| 130 { | 123 { |
| 131 ScriptScope scope(m_scriptState); | 124 ScriptScope scope(m_scriptState); |
| 132 m_resolver->fulfill(fontFaceSet); | 125 m_resolver->resolve(fontFaceSet); |
| 133 } | |
| 134 | |
| 135 ScriptPromise promise() | |
| 136 { | |
| 137 ScriptPromise promise = m_resolver->promise(); | |
| 138 m_resolver->detachPromise(); | |
| 139 return promise; | |
| 140 } | 126 } |
| 141 | 127 |
| 142 private: | 128 private: |
| 143 FontsReadyPromiseResolver(ExecutionContext* context) | 129 FontsReadyPromiseResolver(ScriptPromise promise, ExecutionContext* context) |
| 144 : m_scriptState(ScriptState::current()) | 130 : m_scriptState(ScriptState::current()) |
| 145 , m_resolver(ScriptPromiseResolver::create(context)) | 131 , m_resolver(ScriptPromiseResolver::create(promise, context)) |
| 146 { } | 132 { } |
| 147 ScriptState* m_scriptState; | 133 ScriptState* m_scriptState; |
| 148 RefPtr<ScriptPromiseResolver> m_resolver; | 134 RefPtr<ScriptPromiseResolver> m_resolver; |
| 149 }; | 135 }; |
| 150 | 136 |
| 151 FontFaceSet::FontFaceSet(Document* document) | 137 FontFaceSet::FontFaceSet(Document* document) |
| 152 : ActiveDOMObject(document) | 138 : ActiveDOMObject(document) |
| 153 , m_loadingCount(0) | 139 , m_loadingCount(0) |
| 154 , m_shouldFireDoneEvent(false) | 140 , m_shouldFireDoneEvent(false) |
| 155 , m_timer(this, &FontFaceSet::timerFired) | 141 , m_timer(this, &FontFaceSet::timerFired) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 if (!m_loadingCount) { | 260 if (!m_loadingCount) { |
| 275 ASSERT(!m_shouldFireDoneEvent); | 261 ASSERT(!m_shouldFireDoneEvent); |
| 276 m_shouldFireDoneEvent = true; | 262 m_shouldFireDoneEvent = true; |
| 277 if (!m_timer.isActive()) | 263 if (!m_timer.isActive()) |
| 278 m_timer.startOneShot(0); | 264 m_timer.startOneShot(0); |
| 279 } | 265 } |
| 280 } | 266 } |
| 281 | 267 |
| 282 ScriptPromise FontFaceSet::ready() | 268 ScriptPromise FontFaceSet::ready() |
| 283 { | 269 { |
| 284 OwnPtr<FontsReadyPromiseResolver> resolver = FontsReadyPromiseResolver::crea
te(executionContext()); | 270 ScriptPromise promise = ScriptPromise::createPending(executionContext()); |
| 285 ScriptPromise promise = resolver->promise(); | 271 OwnPtr<FontsReadyPromiseResolver> resolver = FontsReadyPromiseResolver::crea
te(promise, executionContext()); |
| 286 m_readyResolvers.append(resolver.release()); | 272 m_readyResolvers.append(resolver.release()); |
| 287 if (!m_timer.isActive()) | 273 if (!m_timer.isActive()) |
| 288 m_timer.startOneShot(0); | 274 m_timer.startOneShot(0); |
| 289 return promise; | 275 return promise; |
| 290 } | 276 } |
| 291 | 277 |
| 292 void FontFaceSet::fireDoneEventIfPossible() | 278 void FontFaceSet::fireDoneEventIfPossible() |
| 293 { | 279 { |
| 294 if (!m_pendingEvents.isEmpty() || !m_pendingLoadResolvers.isEmpty()) | 280 if (!m_pendingEvents.isEmpty() || !m_pendingLoadResolvers.isEmpty()) |
| 295 return; | 281 return; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 | 338 |
| 353 ScriptPromise FontFaceSet::load(const String& fontString, const String& text, Ex
ceptionState& es) | 339 ScriptPromise FontFaceSet::load(const String& fontString, const String& text, Ex
ceptionState& es) |
| 354 { | 340 { |
| 355 Font font; | 341 Font font; |
| 356 if (!resolveFontStyle(fontString, font)) { | 342 if (!resolveFontStyle(fontString, font)) { |
| 357 es.throwUninformativeAndGenericDOMException(SyntaxError); | 343 es.throwUninformativeAndGenericDOMException(SyntaxError); |
| 358 return ScriptPromise(); | 344 return ScriptPromise(); |
| 359 } | 345 } |
| 360 | 346 |
| 361 Document* d = document(); | 347 Document* d = document(); |
| 362 RefPtr<LoadFontPromiseResolver> resolver = LoadFontPromiseResolver::create(f
ont.family(), executionContext()); | 348 ScriptPromise promise = ScriptPromise::createPending(executionContext()); |
| 349 RefPtr<LoadFontPromiseResolver> resolver = LoadFontPromiseResolver::create(f
ont.family(), promise, executionContext()); |
| 363 for (const FontFamily* f = &font.family(); f; f = f->next()) { | 350 for (const FontFamily* f = &font.family(); f; f = f->next()) { |
| 364 CSSSegmentedFontFace* face = d->styleResolver()->fontSelector()->getFont
Face(font.fontDescription(), f->family()); | 351 CSSSegmentedFontFace* face = d->styleResolver()->fontSelector()->getFont
Face(font.fontDescription(), f->family()); |
| 365 if (!face) { | 352 if (!face) { |
| 366 resolver->error(d); | 353 resolver->error(d); |
| 367 continue; | 354 continue; |
| 368 } | 355 } |
| 369 face->loadFont(font.fontDescription(), nullToSpace(text), resolver); | 356 face->loadFont(font.fontDescription(), nullToSpace(text), resolver); |
| 370 } | 357 } |
| 371 return resolver->promise(); | 358 return promise; |
| 372 } | 359 } |
| 373 | 360 |
| 374 bool FontFaceSet::check(const String& fontString, const String& text, ExceptionS
tate& es) | 361 bool FontFaceSet::check(const String& fontString, const String& text, ExceptionS
tate& es) |
| 375 { | 362 { |
| 376 Font font; | 363 Font font; |
| 377 if (!resolveFontStyle(fontString, font)) { | 364 if (!resolveFontStyle(fontString, font)) { |
| 378 es.throwUninformativeAndGenericDOMException(SyntaxError); | 365 es.throwUninformativeAndGenericDOMException(SyntaxError); |
| 379 return false; | 366 return false; |
| 380 } | 367 } |
| 381 | 368 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 422 |
| 436 void FontFaceSet::FontLoadHistogram::record() | 423 void FontFaceSet::FontLoadHistogram::record() |
| 437 { | 424 { |
| 438 if (m_recorded) | 425 if (m_recorded) |
| 439 return; | 426 return; |
| 440 m_recorded = true; | 427 m_recorded = true; |
| 441 HistogramSupport::histogramCustomCounts("WebFont.WebFontsInPage", m_count, 1
, 100, 50); | 428 HistogramSupport::histogramCustomCounts("WebFont.WebFontsInPage", m_count, 1
, 100, 50); |
| 442 } | 429 } |
| 443 | 430 |
| 444 } // namespace WebCore | 431 } // namespace WebCore |
| OLD | NEW |