1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.omnibox;
6 
7 import org.chromium.base.LifetimeAssert;
8 import org.chromium.base.annotations.NativeMethods;
9 import org.chromium.chrome.browser.profiles.Profile;
10 import org.chromium.components.omnibox.AutocompleteSchemeClassifier;
11 
12 /**
13  * Creates the c++ class that provides scheme classification logic for Chrome.
14  * Must call destroy() after using this object to delete the native object.
15  */
16 public class ChromeAutocompleteSchemeClassifier extends AutocompleteSchemeClassifier {
17     private final LifetimeAssert mLifetimeAssert = LifetimeAssert.create(this);
18 
ChromeAutocompleteSchemeClassifier(Profile profile)19     public ChromeAutocompleteSchemeClassifier(Profile profile) {
20         super(ChromeAutocompleteSchemeClassifierJni.get().createAutocompleteClassifier(profile));
21     }
22 
23     @Override
destroy()24     public void destroy() {
25         ChromeAutocompleteSchemeClassifierJni.get().deleteAutocompleteClassifier(
26                 super.getNativePtr());
27 
28         // If mLifetimeAssert is GC'ed before this is called, it will throw an exception
29         // with a stack trace showing the stack during LifetimeAssert.create().
30         LifetimeAssert.setSafeToGc(mLifetimeAssert, true);
31     }
32 
33     @NativeMethods
34     interface Natives {
createAutocompleteClassifier(Profile profile)35         long createAutocompleteClassifier(Profile profile);
deleteAutocompleteClassifier(long chromeAutocompleteSchemeClassifier)36         void deleteAutocompleteClassifier(long chromeAutocompleteSchemeClassifier);
37     }
38 }
39