OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.bookmarkimport; |
| 6 |
| 7 import android.content.ContentResolver; |
| 8 import android.content.Context; |
| 9 import android.content.pm.PackageInfo; |
| 10 import android.content.pm.PackageManager; |
| 11 import android.content.pm.ProviderInfo; |
| 12 |
| 13 import org.chromium.base.VisibleForTesting; |
| 14 |
| 15 /** |
| 16 * Imports bookmarks from Android Browser into Chrome. |
| 17 */ |
| 18 public class AndroidBrowserImporter extends BookmarkImporter { |
| 19 private static final String BROWSER_PROVIDER_PROXY_PACKAGE = "com.android.br
owser.provider"; |
| 20 private static final String ANDROID_BROWSER_AUTHORITIES = "com.android.brows
er;browser"; |
| 21 |
| 22 private ContentResolver mInputResolver; |
| 23 private boolean mIgnoreAvailableProvidersForTestPurposes; |
| 24 |
| 25 public AndroidBrowserImporter(Context context) { |
| 26 super(context); |
| 27 mInputResolver = context.getContentResolver(); |
| 28 } |
| 29 |
| 30 /** |
| 31 * @return Flag indicating if Android Browser bookmarks data is accesible. |
| 32 * Note that this doesn't ensure the existence of any new or valid b
ookmarks. |
| 33 */ |
| 34 public boolean areBookmarksAccessible() { |
| 35 if (!areProvidersValid()) return false; |
| 36 return AndroidBrowserProviderIterator.isProviderAvailable(mInputResolver
); |
| 37 } |
| 38 |
| 39 // Used by tests through reflection. |
| 40 @VisibleForTesting |
| 41 void setInputResolver(ContentResolver inputResolver) { |
| 42 mInputResolver = inputResolver; |
| 43 } |
| 44 |
| 45 @VisibleForTesting |
| 46 void setIgnoreAvailableProvidersForTestPurposes(boolean ignoreProviders) { |
| 47 mIgnoreAvailableProvidersForTestPurposes = ignoreProviders; |
| 48 } |
| 49 |
| 50 private boolean areProvidersValid() { |
| 51 // Unless a test tests if a provider is valid, this function shall alway
s return true. |
| 52 if (mIgnoreAvailableProvidersForTestPurposes) return true; |
| 53 |
| 54 // If the proxy is present then we are in a post-OTA scenario where we h
ave completely |
| 55 // replaced Android Browser. In that case the providers point to ourselv
es. |
| 56 try { |
| 57 PackageInfo packageInfo = mContext.getPackageManager().getPackageInf
o( |
| 58 BROWSER_PROVIDER_PROXY_PACKAGE, PackageManager.GET_PROVIDERS
); |
| 59 // There is no provider proxy package, we will not query ourselves,
let's continue |
| 60 if (packageInfo == null) return true; |
| 61 |
| 62 ProviderInfo[] providers = packageInfo.providers; |
| 63 // This should be present, but if it's not assume the package was pl
aced by ourselves. |
| 64 if (providers == null) return false; |
| 65 |
| 66 // Verify the authority of the package in case OEMs create one with
the same name |
| 67 // that doesn't replace Android Browser's authority. |
| 68 for (ProviderInfo provider : providers) { |
| 69 if (provider != null && provider.authority != null |
| 70 && provider.authority.equals(ANDROID_BROWSER_AUTHORITIES
)) { |
| 71 return false; |
| 72 } |
| 73 } |
| 74 return true; |
| 75 } catch (PackageManager.NameNotFoundException e) { |
| 76 return true; |
| 77 } |
| 78 } |
| 79 |
| 80 @Override |
| 81 protected BookmarkImporter.BookmarkIterator[] availableBookmarks() { |
| 82 // Make sure we don't query ourselves. |
| 83 if (!areProvidersValid()) return null; |
| 84 |
| 85 return new BookmarkImporter.BookmarkIterator[] { |
| 86 AndroidBrowserProviderIterator.createIfAvailable(mInputResolver) |
| 87 }; |
| 88 } |
| 89 } |
OLD | NEW |