Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4acac231 authored by Svetoslav Ganov's avatar Svetoslav Ganov
Browse files

Leaking connection to the Text-To-Speech service.

bug:3417215

1. Now the Text-To-Speech instance is exposed to JavaScript
   and removed from JavaScript in the attach and detach from
   window callbacks which is consistent with what WebView
   does with its tree observer callbacks.

Change-Id: I3e084ba938ba6f13c578317129264dbcbdcf6450
parent 6f6ef48d
Loading
Loading
Loading
Loading
+27 −20
Original line number Diff line number Diff line
@@ -791,6 +791,9 @@ public class WebView extends AbsoluteLayout
    // The value of 1 means the accessibility script is already injected
    private static final String PATTERN_MATCH_AXS_URL_PARAMETER = "(\\?axs=(0|1))|(&axs=(0|1))";

    // TextToSpeech instance exposed to JavaScript to the injected screenreader.
    private TextToSpeech mTextToSpeech;

    // variable to cache the above pattern in case accessibility is enabled.
    private Pattern mMatchAxsUrlParameterPattern;

@@ -981,13 +984,6 @@ public class WebView extends AbsoluteLayout
        // Used by the chrome stack to find application paths
        JniUtil.setContext(context);

        if (AccessibilityManager.getInstance(context).isEnabled()) {
            if (javaScriptInterfaces == null) {
                javaScriptInterfaces = new HashMap<String, Object>();
            }
            exposeAccessibilityJavaScriptApi(javaScriptInterfaces);
        }

        mCallbackProxy = new CallbackProxy(context, this);
        mViewManager = new ViewManager(this);
        L10nUtils.loadStrings(context);
@@ -1183,23 +1179,30 @@ public class WebView extends AbsoluteLayout
    }

    /**
     * Exposes accessibility APIs to JavaScript by appending them to the JavaScript
     * interfaces map provided by the WebView client. In case of conflicting
     * alias with the one of the accessibility API the user specified one wins.
     * Adds accessibility APIs to JavaScript.
     *
     * @param javaScriptInterfaces A map with interfaces to be exposed to JavaScript.
     * Note: This method is responsible to performing the necessary
     *       check if the accessibility APIs should be exposed.
     */
    private void exposeAccessibilityJavaScriptApi(Map<String, Object> javaScriptInterfaces) {
        if (javaScriptInterfaces.containsKey(ALIAS_ACCESSIBILITY_JS_INTERFACE)) {
            Log.w(LOGTAG, "JavaScript interface mapped to \"" + ALIAS_ACCESSIBILITY_JS_INTERFACE
                    + "\" overrides the accessibility API JavaScript interface. No accessibility"
                    + "API will be exposed to JavaScript!");
            return;
    private void addAccessibilityApisToJavaScript() {
        if (AccessibilityManager.getInstance(mContext).isEnabled()
                && getSettings().getJavaScriptEnabled()) {
            // exposing the TTS for now ...
            mTextToSpeech = new TextToSpeech(getContext(), null); 
            addJavascriptInterface(mTextToSpeech, ALIAS_ACCESSIBILITY_JS_INTERFACE);
        }
    }

        // expose the TTS for now ...
        javaScriptInterfaces.put(ALIAS_ACCESSIBILITY_JS_INTERFACE,
                new TextToSpeech(getContext(), null));
    /**
     * Removes accessibility APIs from JavaScript.
     */
    private void removeAccessibilityApisFromJavaScript() {
        // exposing the TTS for now ...
        if (mTextToSpeech != null) {
            removeJavascriptInterface(ALIAS_ACCESSIBILITY_JS_INTERFACE);
            mTextToSpeech.shutdown();
            mTextToSpeech = null;
        }
    }

    @Override
@@ -4996,6 +4999,8 @@ public class WebView extends AbsoluteLayout
                treeObserver.addOnScrollChangedListener(mScrollChangedListener);
            }
        }

        addAccessibilityApisToJavaScript();
    }

    @Override
@@ -5016,6 +5021,8 @@ public class WebView extends AbsoluteLayout
            }
        }

        removeAccessibilityApisFromJavaScript();

        super.onDetachedFromWindow();
    }