| OLD | NEW |
| 1 // Copyright (c) 2009, Google Inc. | 1 // Copyright (c) 2009, Google Inc. |
| 2 // All rights reserved. | 2 // 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 | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 DartController::~DartController() | 326 DartController::~DartController() |
| 327 { | 327 { |
| 328 clearWindowShell(); | 328 clearWindowShell(); |
| 329 } | 329 } |
| 330 | 330 |
| 331 void DartController::clearWindowShell() | 331 void DartController::clearWindowShell() |
| 332 { | 332 { |
| 333 DART_START_TIMER(); | 333 DART_START_TIMER(); |
| 334 initVMIfNeeded(); | 334 initVMIfNeeded(); |
| 335 DART_RECORD_TIMER("clearWindowShell::initVM took"); | 335 DART_RECORD_TIMER("clearWindowShell::initVM took"); |
| 336 m_documentsWithDart.clear(); | 336 m_loaders.clear(); |
| 337 if (m_loader) { | 337 m_usedNames.clear(); |
| 338 m_loader = nullptr; | 338 m_isolateNames.clear(); |
| 339 } | 339 m_mainLoader.clear(); |
| 340 | 340 |
| 341 // Due to synchronous dispatch, we may be in an isolate corresponding to ano
ther frame. | 341 // Due to synchronous dispatch, we may be in an isolate corresponding to ano
ther frame. |
| 342 // If so, exit here but re-enter before returning. | 342 // If so, exit here but re-enter before returning. |
| 343 Dart_Isolate currentIsolate = Dart_CurrentIsolate(); | 343 Dart_Isolate currentIsolate = Dart_CurrentIsolate(); |
| 344 if (currentIsolate) | 344 if (currentIsolate) |
| 345 Dart_ExitIsolate(); | 345 Dart_ExitIsolate(); |
| 346 | 346 |
| 347 Vector<Dart_Isolate>::iterator iterator; | 347 Vector<Dart_Isolate>::iterator iterator; |
| 348 for (iterator = m_isolates.begin(); iterator != m_isolates.end(); ++iterator
) { | 348 for (iterator = m_isolates.begin(); iterator != m_isolates.end(); ++iterator
) { |
| 349 Dart_Isolate isolate = *iterator; | 349 Dart_Isolate isolate = *iterator; |
| (...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 } | 742 } |
| 743 | 743 |
| 744 void DartController::loadAndRunScript(const String& url, Dart_Isolate isolate, P
assRefPtr<DartScriptInfo> info) | 744 void DartController::loadAndRunScript(const String& url, Dart_Isolate isolate, P
assRefPtr<DartScriptInfo> info) |
| 745 { | 745 { |
| 746 DART_START_TIMER(); | 746 DART_START_TIMER(); |
| 747 RefPtr<DartScriptInfo> scriptInfo = info; | 747 RefPtr<DartScriptInfo> scriptInfo = info; |
| 748 Document* ALLOW_UNUSED document = frame()->document(); | 748 Document* ALLOW_UNUSED document = frame()->document(); |
| 749 | 749 |
| 750 // Invoke only if this is the main document. | 750 // Invoke only if this is the main document. |
| 751 ASSERT(scriptInfo->ownerDocument() == document); | 751 ASSERT(scriptInfo->ownerDocument() == document); |
| 752 | 752 RefPtr<DartApplicationLoader> loader = m_loaders.get(isolate); |
| 753 ASSERT(m_loader); | 753 ASSERT(loader); |
| 754 // Due to deferred loading we might already be running Dart code on | 754 // Due to deferred loading we might already be running Dart code on |
| 755 // an isolate and the library tag handler callback could result in a call | 755 // an isolate and the library tag handler callback could result in a call |
| 756 // to this code, we skip the Enter/Exit Isolate calls in that case. | 756 // to this code, we skip the Enter/Exit Isolate calls in that case. |
| 757 if (isolate == Dart_CurrentIsolate()) { | 757 if (isolate == Dart_CurrentIsolate()) { |
| 758 m_loader->load(scriptInfo); | 758 loader->load(scriptInfo); |
| 759 } else { | 759 } else { |
| 760 Dart_EnterIsolate(isolate); | 760 DartIsolateScope scope(isolate); |
| 761 m_loader->load(scriptInfo); | 761 loader->load(scriptInfo); |
| 762 Dart_ExitIsolate(); | |
| 763 } | 762 } |
| 764 | 763 |
| 765 DART_RECORD_TIMER("DartController::loadAndRunScript took"); | 764 DART_RECORD_TIMER("DartController::loadAndRunScript took"); |
| 766 } | 765 } |
| 767 | 766 |
| 768 void DartController::evaluate(const ScriptSourceCode& sourceCode, ScriptLoader*
loader) | 767 void DartController::evaluate(const ScriptSourceCode& sourceCode, ScriptLoader*
loader) |
| 769 { | 768 { |
| 770 Document* document = frame()->document(); | 769 Document* document = frame()->document(); |
| 771 | 770 |
| 772 if (checkForExpiration()) { | 771 if (checkForExpiration()) { |
| 773 // Log to console. | 772 // Log to console. |
| 774 DartUtilities::reportProblem(document, ExpirationMessage); | 773 DartUtilities::reportProblem(document, ExpirationMessage); |
| 775 // Bring up alert message that Dartium has expired. | 774 // Bring up alert message that Dartium has expired. |
| 776 document->domWindow()->alert(ExpirationMessage); | 775 document->domWindow()->alert(ExpirationMessage); |
| 777 return; | 776 return; |
| 778 } | 777 } |
| 779 | 778 |
| 780 DART_START_TIMER(); | 779 DART_START_TIMER(); |
| 781 initVMIfNeeded(); | 780 initVMIfNeeded(); |
| 782 DART_RECORD_TIMER("evaluate::initVM took"); | 781 DART_RECORD_TIMER("evaluate::initVM took"); |
| 783 RefPtr<Element> element(loader->element()); | 782 RefPtr<Element> element(loader->element()); |
| 784 | 783 |
| 785 RefPtr<DartScriptInfo> scriptInfo = DartScriptInfo::create(element); | 784 RefPtr<DartScriptInfo> scriptInfo = DartScriptInfo::create(element); |
| 786 if (!scriptInfo) { | 785 if (!scriptInfo) { |
| 787 DartUtilities::reportProblem(document, "Dart script must be in HTML or S
VG document."); | 786 DartUtilities::reportProblem(document, "Dart script must be in HTML or S
VG document."); |
| 788 ASSERT_NOT_REACHED(); | 787 ASSERT_NOT_REACHED(); |
| 789 return; | 788 return; |
| 790 } | 789 } |
| 791 | 790 |
| 792 Document* owner = scriptInfo->ownerDocument(); | 791 RefPtr<DartApplicationLoader> currentLoader; |
| 793 if (m_documentsWithDart.contains(owner)) { | 792 |
| 793 // HTML Import case. |
| 794 if (m_mainLoader && document != scriptInfo->ownerDocument() && !m_mainLoader
->uninitialized()) { |
| 794 int line = scriptInfo->startLineNumber().zeroBasedInt(); | 795 int line = scriptInfo->startLineNumber().zeroBasedInt(); |
| 795 DartUtilities::reportProblem(owner, "Only one Dart script tag allowed pe
r document", line); | 796 DartUtilities::reportProblem(document, "Inline Darts scripts not support
ed after main script is invoked.", line); |
| 796 return; | 797 return; |
| 797 } | 798 } |
| 798 m_documentsWithDart.add(owner); | 799 if (!m_mainLoader || (!m_mainLoader->uninitialized() && document == scriptIn
fo->ownerDocument())) { |
| 799 | 800 // FIXME: perhaps if loader->validateUrlLoaded(scriptURL) has been |
| 800 if (m_loader) { | 801 // called we are actually good to use the existing loader? |
| 801 if (m_loader->running()) { | 802 currentLoader = DartApplicationLoader::create(document, true); |
| 802 // Main has already been invoked. | 803 if (!m_mainLoader) { |
| 803 String scriptURL = scriptInfo->sourceAttributeValue(); | 804 m_mainLoader = currentLoader; |
| 804 | |
| 805 // Enforce that no new code is loaded. We've already invoked main at
this point. | |
| 806 // Any referenced code must already be in the isolate. | |
| 807 if (scriptURL.isEmpty()) { | |
| 808 int line = scriptInfo->startLineNumber().zeroBasedInt(); | |
| 809 DartUtilities::reportProblem(document, "Inline Darts scripts not
supported after main script is invoked.", line); | |
| 810 } else { | |
| 811 m_loader->validateUrlLoaded(scriptURL); | |
| 812 } | |
| 813 return; | |
| 814 } | 805 } |
| 815 if (m_loader->error()) { | 806 } else { |
| 816 return; | 807 currentLoader = m_mainLoader; |
| 817 } | |
| 818 } | |
| 819 | |
| 820 if (!m_loader) { | |
| 821 m_loader = DartApplicationLoader::create(document, true); | |
| 822 } | 808 } |
| 823 | 809 |
| 824 DART_RECORD_TIMER("evaluate::prep for loading took"); | 810 DART_RECORD_TIMER("evaluate::prep for loading took"); |
| 825 if (document == scriptInfo->ownerDocument()) { | 811 if (document == scriptInfo->ownerDocument()) { |
| 826 String url = scriptInfo->url(); | 812 String url = scriptInfo->url(); |
| 827 // FIXME: This should be the first DOM enabled isolate. There is a race
condition | |
| 828 // however - m_loader above won't catch this if it hasn't been set yet. | |
| 829 // This problem goes away once we map each script to a separate isolate. | |
| 830 if (!m_isolates.isEmpty()) | |
| 831 return; | |
| 832 DART_RECORD_TIMER("evaluate before createIsolate"); | 813 DART_RECORD_TIMER("evaluate before createIsolate"); |
| 833 DEFINE_STATIC_LOCAL(const char*, packageRoot, (getenv("DART_PACKAGE_ROOT
"))); | 814 DEFINE_STATIC_LOCAL(const char*, packageRoot, (getenv("DART_PACKAGE_ROOT
"))); |
| 834 Dart_Isolate isolate = createDOMEnabledIsolate(url, "main", packageRoot,
document); | 815 Dart_Isolate isolate = createDOMEnabledIsolate(url, "main", packageRoot,
document); |
| 816 m_loaders.add(isolate, currentLoader); |
| 835 DART_RECORD_TIMER("evaluate after createIsolate"); | 817 DART_RECORD_TIMER("evaluate after createIsolate"); |
| 836 RefPtr<DartDomLoadCallback> callback = adoptRef(new DartDomLoadCallback(
this, url, isolate, document, scriptInfo)); | 818 RefPtr<DartDomLoadCallback> callback = adoptRef(new DartDomLoadCallback(
this, url, isolate, document, scriptInfo)); |
| 837 Dart_ExitIsolate(); | 819 Dart_ExitIsolate(); |
| 838 m_loader->processRequests(isolate, sourceCode, callback); | 820 currentLoader->processRequests(isolate, sourceCode, callback); |
| 839 DART_RECORD_TIMER("evaluate process request took"); | 821 DART_RECORD_TIMER("evaluate process request took"); |
| 840 } else { | 822 } else { |
| 841 m_loader->addRequest(scriptInfo); | 823 // Add all HTML import scripts into the first loader. |
| 824 m_mainLoader->addRequest(scriptInfo); |
| 842 } | 825 } |
| 843 DART_RECORD_TIMER("evaluate took"); | 826 DART_RECORD_TIMER("evaluate took"); |
| 844 } | 827 } |
| 845 | 828 |
| 846 void DartController::bindToWindowObject(LocalFrame* frame, const String& key, NP
Object* object) | 829 void DartController::bindToWindowObject(LocalFrame* frame, const String& key, NP
Object* object) |
| 847 { | 830 { |
| 848 // FIXME: proper management of lifetime. | 831 // FIXME: proper management of lifetime. |
| 849 m_npObjectMap.set(key, object); | 832 m_npObjectMap.set(key, object); |
| 850 } | 833 } |
| 851 | 834 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 // -1 cannot be used as a HashMap key however library ids are | 906 // -1 cannot be used as a HashMap key however library ids are |
| 924 // guaranteed to be non-negative so it is a non-issue. | 907 // guaranteed to be non-negative so it is a non-issue. |
| 925 ASSERT(libraryId >= 0); | 908 ASSERT(libraryId >= 0); |
| 926 // 0 cannot be used as a HashMap key so we add 1 to the library id to | 909 // 0 cannot be used as a HashMap key so we add 1 to the library id to |
| 927 // create a valid key. | 910 // create a valid key. |
| 928 intptr_t libraryIdKey = libraryId + 1; | 911 intptr_t libraryIdKey = libraryId + 1; |
| 929 LibraryIdMap::iterator libraryIter = libraryIdMap->find(libraryIdKey); | 912 LibraryIdMap::iterator libraryIter = libraryIdMap->find(libraryIdKey); |
| 930 DartScriptState* scriptState; | 913 DartScriptState* scriptState; |
| 931 if (libraryIter == libraryIdMap->end()) { | 914 if (libraryIter == libraryIdMap->end()) { |
| 932 V8ScriptState* v8ScriptState = V8ScriptState::from(v8Context); | 915 V8ScriptState* v8ScriptState = V8ScriptState::from(v8Context); |
| 933 RefPtr<DartScriptState> scriptStatePtr = DartScriptState::create(isolate
, libraryId, v8ScriptState); | 916 RefPtr<DartScriptState> scriptStatePtr = DartScriptState::create(isolate
, libraryId, v8ScriptState, isolateName(isolate)); |
| 934 libraryIdMap->set(libraryIdKey, scriptStatePtr); | 917 libraryIdMap->set(libraryIdKey, scriptStatePtr); |
| 935 scriptState = scriptStatePtr.get(); | 918 scriptState = scriptStatePtr.get(); |
| 936 } else { | 919 } else { |
| 937 scriptState = libraryIter->value.get(); | 920 scriptState = libraryIter->value.get(); |
| 938 ASSERT(scriptState); | 921 ASSERT(scriptState); |
| 939 } | 922 } |
| 940 return scriptState; | 923 return scriptState; |
| 941 } | 924 } |
| 942 | 925 |
| 926 String DartController::isolateName(Dart_Isolate isolate) |
| 927 { |
| 928 if (m_isolateNames.contains(isolate)) { |
| 929 return m_isolateNames.get(isolate); |
| 930 } |
| 931 String fileName(""); |
| 932 String name; |
| 933 if (m_loaders.contains(isolate)) { |
| 934 const KURL& scriptUrl = m_loaders.get(isolate)->scriptUrl(); |
| 935 if (scriptUrl.isValid()) { |
| 936 String lastPathComponent = scriptUrl.lastPathComponent(); |
| 937 if (!lastPathComponent.isEmpty()) { |
| 938 fileName = scriptUrl.lastPathComponent(); |
| 939 } |
| 940 } |
| 941 } |
| 942 name = fileName; |
| 943 int i = 2; |
| 944 while (m_usedNames.contains(name)) { |
| 945 name = String::format("%s[%d]", fileName.utf8().data(), i); |
| 946 i++; |
| 947 } |
| 948 m_isolateNames.add(isolate, name); |
| 949 m_usedNames.add(name); |
| 950 return name; |
| 951 } |
| 952 |
| 943 void DartController::collectScriptStatesForIsolate(Dart_Isolate isolate, v8::Han
dle<v8::Context> v8Context, Vector<DartScriptState*>& result) | 953 void DartController::collectScriptStatesForIsolate(Dart_Isolate isolate, v8::Han
dle<v8::Context> v8Context, Vector<DartScriptState*>& result) |
| 944 { | 954 { |
| 945 if (!isolate) | 955 if (!isolate) |
| 946 return; | 956 return; |
| 947 DartIsolateScope scope(isolate); | 957 DartIsolateScope scope(isolate); |
| 948 DartApiScope apiScope; | 958 DartApiScope apiScope; |
| 949 LibraryIdMap* libraryIdMap = libraryIdMapForIsolate(isolate); | 959 LibraryIdMap* libraryIdMap = libraryIdMapForIsolate(isolate); |
| 950 Dart_Handle libraryIdList = Dart_GetLibraryIds(); | 960 Dart_Handle libraryIdList = Dart_GetLibraryIds(); |
| 951 | 961 |
| 952 intptr_t length = 0; | 962 intptr_t length = 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 Dart_ExitIsolate(); | 997 Dart_ExitIsolate(); |
| 988 loader->processSingleRequest(isolate, url, callback); | 998 loader->processSingleRequest(isolate, url, callback); |
| 989 | 999 |
| 990 // Restore caller isolate. | 1000 // Restore caller isolate. |
| 991 Dart_EnterIsolate(caller); | 1001 Dart_EnterIsolate(caller); |
| 992 | 1002 |
| 993 // FIXME: We need some way to return a Dart_Handle to the isolate we just cr
eated. | 1003 // FIXME: We need some way to return a Dart_Handle to the isolate we just cr
eated. |
| 994 } | 1004 } |
| 995 | 1005 |
| 996 } | 1006 } |
| OLD | NEW |