Title: [123461] trunk
Revision
123461
Author
h...@chromium.org
Date
2012-07-24 05:25:13 -0700 (Tue, 24 Jul 2012)

Log Message

Speech _javascript_ API: Add the SpeechRecognitionResult.emma attribute
https://bugs.webkit.org/show_bug.cgi?id=91743

Reviewed by Adam Barth.

Source/WebCore:

Implement the 'emma' attribute as described in the spec draft at
http://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html

Test: fast/speech/scripted/emma.html

* Modules/speech/SpeechRecognitionResult.cpp:
(WebCore::SpeechRecognitionResult::~SpeechRecognitionResult):
(WebCore):
(WebCore::SpeechRecognitionResult::emma):
* Modules/speech/SpeechRecognitionResult.h:
(WebCore):
(SpeechRecognitionResult):
* Modules/speech/SpeechRecognitionResult.idl:
* WebCore.gypi:
* bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp:
(WebCore):
(WebCore::V8SpeechRecognitionResult::visitDOMWrapper):
  We need to add an implicit reference from the result object to the
  emma object to keep the latters DOM tree alive across
  garbage-collection.

LayoutTests:

Add layout test for the SpeechRecognitionResult.emma attribute.

* fast/speech/scripted/emma.html: Added.
* fast/speech/scripted/emma-expected.txt: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (123460 => 123461)


--- trunk/LayoutTests/ChangeLog	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/LayoutTests/ChangeLog	2012-07-24 12:25:13 UTC (rev 123461)
@@ -1,3 +1,15 @@
+2012-07-23  Hans Wennborg  <h...@chromium.org>
+
+        Speech _javascript_ API: Add the SpeechRecognitionResult.emma attribute
+        https://bugs.webkit.org/show_bug.cgi?id=91743
+
+        Reviewed by Adam Barth.
+
+        Add layout test for the SpeechRecognitionResult.emma attribute.
+
+        * fast/speech/scripted/emma.html: Added.
+        * fast/speech/scripted/emma-expected.txt: Added.
+
 2012-07-24  Zan Dobersek  <zandober...@gmail.com>
 
         Unreviewed GTK gardening.

Added: trunk/LayoutTests/fast/speech/scripted/emma-expected.txt (0 => 123461)


--- trunk/LayoutTests/fast/speech/scripted/emma-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/speech/scripted/emma-expected.txt	2012-07-24 12:25:13 UTC (rev 123461)
@@ -0,0 +1,51 @@
+Test the SpeechRecognitionResult.emma attribute.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS 'webkitSpeechRecognition' in self is true
+PASS webkitSpeechRecognition == null is false
+onstart
+onaudiostart
+onsoundstart
+onspeechstart
+onresult
+PASS event.result.emma.childNodes.length is 1
+PASS event.result.emma.firstChild.nodeName is "emma:emma"
+window.emma = event.result.emma.firstChild
+PASS emma.getAttribute("version") is "1.0"
+PASS emma.childNodes.length is 1
+PASS emma.firstChild.nodeName is "emma:one-of"
+window._oneof_ = emma.firstChild
+window.emmaNs = 'http://www.w3.org/2003/04/emma'
+PASS oneof.getAttribute("emma:medium") is "acoustic"
+PASS oneof.getAttributeNS(emmaNs, "medium") is "acoustic"
+PASS oneof.getAttribute("emma:mode") is "voice"
+PASS oneof.getAttributeNS(emmaNs, "mode") is "voice"
+PASS oneof.childNodes.length is 1
+PASS oneof.firstChild.nodeName is "emma:interpretation"
+window.interpretation = oneof.firstChild
+PASS interpretation.getAttribute("id") is "1"
+PASS interpretation.getAttribute("emma:confidence") is "0.42"
+PASS interpretation.getAttributeNS(emmaNs, "confidence") is "0.42"
+PASS interpretation.getElementsByTagName("literal").length is 1
+window.literal = interpretation.getElementsByTagName("literal")[0]
+PASS literal.childNodes.length is 1
+PASS literal.firstChild.data is "hello, world"
+
+Test that emma's DOM tree stays alive:
+window.emma = null
+window._oneof_ = null
+window.interpretation = null
+window.literal = null
+event.result.emma.customProperty = 42
+gc()
+PASS event.result.emma.customProperty is 42
+onspeechend
+onsoundend
+onaudioend
+onend
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
Property changes on: trunk/LayoutTests/fast/speech/scripted/emma-expected.txt
___________________________________________________________________

Added: svn:eol-style

Added: trunk/LayoutTests/fast/speech/scripted/emma.html (0 => 123461)


--- trunk/LayoutTests/fast/speech/scripted/emma.html	                        (rev 0)
+++ trunk/LayoutTests/fast/speech/scripted/emma.html	2012-07-24 12:25:13 UTC (rev 123461)
@@ -0,0 +1,82 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script type="text/_javascript_">
+description('Test the SpeechRecognitionResult.emma attribute.');
+
+function run() {
+    // Check availability of constructors.
+    shouldBeTrue("'webkitSpeechRecognition' in self");
+    shouldBeFalse("webkitSpeechRecognition == null");
+
+    emmaTest();
+}
+
+function emmaTest() {
+    var r = new webkitSpeechRecognition();
+
+    // Set default handlers.
+    for (var prop in r) {
+        if (prop.match('^on')) {
+            r[prop] = function() { debug('on' + event.type); }
+        }
+    }
+
+    r._onresult_ = function() {
+        debug('onresult');
+        shouldBe('event.result.emma.childNodes.length', '1');
+        shouldBeEqualToString('event.result.emma.firstChild.nodeName', 'emma:emma');
+        evalAndLog('window.emma = event.result.emma.firstChild');
+        shouldBeEqualToString('emma.getAttribute("version")', '1.0');
+
+        shouldBe('emma.childNodes.length', '1');
+        shouldBeEqualToString('emma.firstChild.nodeName', 'emma:one-of');
+        evalAndLog('window._oneof_ = emma.firstChild');
+        evalAndLog("window.emmaNs = 'http://www.w3.org/2003/04/emma'");
+        shouldBeEqualToString('oneof.getAttribute("emma:medium")', 'acoustic');
+        shouldBeEqualToString('oneof.getAttributeNS(emmaNs, "medium")', 'acoustic');
+        shouldBeEqualToString('oneof.getAttribute("emma:mode")', 'voice');
+        shouldBeEqualToString('oneof.getAttributeNS(emmaNs, "mode")', 'voice');
+
+        shouldBe('oneof.childNodes.length', '1');
+        shouldBeEqualToString('oneof.firstChild.nodeName', 'emma:interpretation');
+        evalAndLog('window.interpretation = oneof.firstChild');
+        shouldBeEqualToString('interpretation.getAttribute("id")', '1');
+        shouldBeEqualToString('interpretation.getAttribute("emma:confidence")', '0.42');
+        shouldBeEqualToString('interpretation.getAttributeNS(emmaNs, "confidence")', '0.42');
+        shouldBe('interpretation.getElementsByTagName("literal").length', '1');
+        evalAndLog('window.literal = interpretation.getElementsByTagName("literal")[0]');
+        shouldBe('literal.childNodes.length', '1');
+        shouldBeEqualToString('literal.firstChild.data', 'hello, world');
+
+        // Test that SpeechRecognitionResult keeps emma's DOM tree alive.
+        debug("\nTest that emma's DOM tree stays alive:");
+        evalAndLog('window.emma = null');
+        evalAndLog('window._oneof_ = null');
+        evalAndLog('window.interpretation = null');
+        evalAndLog('window.literal = null');
+        evalAndLog('event.result.emma.customProperty = 42');
+        evalAndLog('gc()');
+        shouldBe('event.result.emma.customProperty', '42');
+    }
+
+    r._onend_ = function() {
+        debug('onend');
+        finishJSTest();
+    }
+
+    if (window.testRunner) {
+        testRunner.addMockSpeechRecognitionResult('hello, world', 0.42);
+    }
+    r.start();
+}
+
+window._onload_ = run;
+window.jsTestIsAsync = true;
+</script>
+<script src=""
+</body>
+</html>
Property changes on: trunk/LayoutTests/fast/speech/scripted/emma.html
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/Source/WebCore/ChangeLog (123460 => 123461)


--- trunk/Source/WebCore/ChangeLog	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/Source/WebCore/ChangeLog	2012-07-24 12:25:13 UTC (rev 123461)
@@ -1,3 +1,31 @@
+2012-07-23  Hans Wennborg  <h...@chromium.org>
+
+        Speech _javascript_ API: Add the SpeechRecognitionResult.emma attribute
+        https://bugs.webkit.org/show_bug.cgi?id=91743
+
+        Reviewed by Adam Barth.
+
+        Implement the 'emma' attribute as described in the spec draft at
+        http://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html
+
+        Test: fast/speech/scripted/emma.html
+
+        * Modules/speech/SpeechRecognitionResult.cpp:
+        (WebCore::SpeechRecognitionResult::~SpeechRecognitionResult):
+        (WebCore):
+        (WebCore::SpeechRecognitionResult::emma):
+        * Modules/speech/SpeechRecognitionResult.h:
+        (WebCore):
+        (SpeechRecognitionResult):
+        * Modules/speech/SpeechRecognitionResult.idl:
+        * WebCore.gypi:
+        * bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp:
+        (WebCore):
+        (WebCore::V8SpeechRecognitionResult::visitDOMWrapper):
+          We need to add an implicit reference from the result object to the
+          emma object to keep the latters DOM tree alive across
+          garbage-collection.
+
 2012-07-24  Andrei Poenaru  <poen...@adobe.com>
 
         Web Inspector: Protocol Extension: add getFlowByName command

Modified: trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.cpp (123460 => 123461)


--- trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.cpp	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.cpp	2012-07-24 12:25:13 UTC (rev 123461)
@@ -29,8 +29,16 @@
 
 #include "SpeechRecognitionResult.h"
 
+#include "Document.h"
+#include "Element.h"
+#include "Text.h"
+
 namespace WebCore {
 
+SpeechRecognitionResult::~SpeechRecognitionResult()
+{
+}
+
 PassRefPtr<SpeechRecognitionResult> SpeechRecognitionResult::create(const Vector<RefPtr<SpeechRecognitionAlternative> >& alternatives, bool final)
 {
     return adoptRef(new SpeechRecognitionResult(alternatives, final));
@@ -44,6 +52,46 @@
     return m_alternatives[index].get();
 }
 
+Document* SpeechRecognitionResult::emma()
+{
+    if (m_emma)
+        return m_emma.get();
+
+    RefPtr<Document> document = Document::create(0, KURL());
+
+    const char emmaNamespaceUrl[] = "http://www.w3.org/2003/04/emma";
+    RefPtr<Element> emmaElement = document->createElement(QualifiedName("emma", "emma", emmaNamespaceUrl), false);
+    ExceptionCode ec = 0;
+    emmaElement->setAttribute("version", "1.0", ec);
+    ASSERT(!ec);
+    if (ec)
+        return 0;
+
+    RefPtr<Element> _oneOf_ = document->createElement(QualifiedName("emma", "one-of", emmaNamespaceUrl), false);
+    oneOf->setAttribute(QualifiedName("emma", "medium", emmaNamespaceUrl), "acoustic");
+    oneOf->setAttribute(QualifiedName("emma", "mode", emmaNamespaceUrl), "voice");
+    oneOf->setIdAttribute("one-of");
+
+    for (size_t i = 0; i < m_alternatives.size(); ++i) {
+        const RefPtr<SpeechRecognitionAlternative>& alternative = m_alternatives[i];
+
+        RefPtr<Element> interpretation = document->createElement(QualifiedName("emma", "interpretation", emmaNamespaceUrl), false);
+        interpretation->setIdAttribute(String::number(i + 1));
+        interpretation->setAttribute(QualifiedName("emma", "confidence", emmaNamespaceUrl), String::number(alternative->confidence()));
+
+        RefPtr<Element> literal = document->createElement(QualifiedName("emma", "literal", emmaNamespaceUrl), false);
+        literal->appendChild(document->createTextNode(alternative->transcript()));
+        interpretation->appendChild(literal.release());
+        oneOf->appendChild(interpretation.release());
+    }
+
+    emmaElement->appendChild(oneOf.release());
+    document->appendChild(emmaElement.release());
+
+    m_emma = document;
+    return m_emma.get();
+}
+
 SpeechRecognitionResult::SpeechRecognitionResult(const Vector<RefPtr<SpeechRecognitionAlternative> >& alternatives, bool final)
     : m_alternatives(alternatives)
     , m_final(final)

Modified: trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.h (123460 => 123461)


--- trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.h	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.h	2012-07-24 12:25:13 UTC (rev 123461)
@@ -34,19 +34,24 @@
 
 namespace WebCore {
 
+class Document;
+
 class SpeechRecognitionResult : public RefCounted<SpeechRecognitionResult> {
 public:
+    ~SpeechRecognitionResult();
     static PassRefPtr<SpeechRecognitionResult> create(const Vector<RefPtr<SpeechRecognitionAlternative> >&, bool final);
 
     unsigned long length() { return m_alternatives.size(); }
     SpeechRecognitionAlternative* item(unsigned long index);
     bool final() { return m_final; }
+    Document* emma();
 
 private:
     SpeechRecognitionResult(const Vector<RefPtr<SpeechRecognitionAlternative> >&, bool final);
 
     Vector<RefPtr<SpeechRecognitionAlternative> > m_alternatives;
     bool m_final;
+    RefPtr<Document> m_emma;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.idl (123460 => 123461)


--- trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.idl	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.idl	2012-07-24 12:25:13 UTC (rev 123461)
@@ -26,11 +26,14 @@
 module core {
     interface [
         Conditional=SCRIPTED_SPEECH,
-        IndexedGetter
+        CustomIsReachable,
+        IndexedGetter,
+        V8DependentLifetime
     ] SpeechRecognitionResult {
         readonly attribute unsigned long length;
         SpeechRecognitionAlternative item(in [IsIndex] unsigned long index);
         readonly attribute boolean final;
+        readonly attribute Document emma;
     };
 }
 

Modified: trunk/Source/WebCore/WebCore.gypi (123460 => 123461)


--- trunk/Source/WebCore/WebCore.gypi	2012-07-24 12:09:49 UTC (rev 123460)
+++ trunk/Source/WebCore/WebCore.gypi	2012-07-24 12:25:13 UTC (rev 123461)
@@ -2375,6 +2375,7 @@
             'bindings/v8/custom/V8SVGPathSegCustom.cpp',
             'bindings/v8/custom/V8ScriptProfileCustom.cpp',
             'bindings/v8/custom/V8ScriptProfileNodeCustom.cpp',
+            'bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp',
             'bindings/v8/custom/V8StorageCustom.cpp',
             'bindings/v8/custom/V8StyleSheetCustom.cpp',
             'bindings/v8/custom/V8StyleSheetListCustom.cpp',

Copied: trunk/Source/WebCore/bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp (from rev 123460, trunk/Source/WebCore/Modules/speech/SpeechRecognitionResult.cpp) (0 => 123461)


--- trunk/Source/WebCore/bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp	                        (rev 0)
+++ trunk/Source/WebCore/bindings/v8/custom/V8SpeechRecognitionResultCustom.cpp	2012-07-24 12:25:13 UTC (rev 123461)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if ENABLE(SCRIPTED_SPEECH)
+
+#include "V8SpeechRecognitionResult.h"
+
+#include "SpeechRecognitionResult.h"
+#include "V8Binding.h"
+
+namespace WebCore {
+
+void V8SpeechRecognitionResult::visitDOMWrapper(DOMDataStore* store, void* object, v8::Persistent<v8::Object> wrapper)
+{
+    SpeechRecognitionResult* impl = static_cast<SpeechRecognitionResult*>(object);
+    Document* emma = impl->emma();
+    v8::Persistent<v8::Value> emmaWrapper = store->domNodeMap().get(emma);
+    if (!emmaWrapper.IsEmpty())
+        v8::V8::AddImplicitReferences(wrapper, &emmaWrapper, 1);
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(SCRIPTED_SPEECH)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to