Browse Source

Workaround for #270

In my opinion, it is neccessary for a browser to open local files.
Because local files might be a security risk,
ask the user before opening a local file.
master
ByteHamster 9 years ago
parent
commit
8169294c80
  1. 12
      app/src/main/AndroidManifest.xml
  2. 40
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 11
      app/src/main/java/acr/browser/lightning/utils/UrlUtils.java
  4. 3
      app/src/main/java/acr/browser/lightning/view/LightningView.java

12
app/src/main/AndroidManifest.xml

@ -47,6 +47,18 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file"/>
<data android:mimeType="text/html"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="application/xhtml+xml"/>
<data android:mimeType="application/vnd.wap.xhtml+xml"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" /> <data android:scheme="http" />
<data android:scheme="https" /> <data android:scheme="https" />
<data android:scheme="about" /> <data android:scheme="about" />

40
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -403,7 +403,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
updateUrl(currentView.getUrl(), true); updateUrl(currentView.getUrl(), true);
} else if (hasFocus && currentView != null) { } else if (hasFocus && currentView != null) {
String url = currentView.getUrl(); String url = currentView.getUrl();
if (url.startsWith(Constants.FILE)) { if (UrlUtils.isSpecialUrl(url)) {
mSearch.setText(""); mSearch.setText("");
} else { } else {
mSearch.setText(url); mSearch.setText(url);
@ -691,7 +691,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale); overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale);
return true; return true;
case R.id.action_share: case R.id.action_share:
if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !UrlUtils.isSpecialUrl(currentView.getUrl())) {
Intent shareIntent = new Intent(Intent.ACTION_SEND); Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, currentView.getTitle()); shareIntent.putExtra(Intent.EXTRA_SUBJECT, currentView.getTitle());
@ -703,7 +703,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
openBookmarks(); openBookmarks();
return true; return true;
case R.id.action_copy: case R.id.action_copy:
if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !UrlUtils.isSpecialUrl(currentView.getUrl())) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", currentView.getUrl()); ClipData clip = ClipData.newPlainText("label", currentView.getUrl());
clipboard.setPrimaryClip(clip); clipboard.setPrimaryClip(clip);
@ -717,7 +717,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
openHistory(); openHistory();
return true; return true;
case R.id.action_add_bookmark: case R.id.action_add_bookmark:
if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !UrlUtils.isSpecialUrl(currentView.getUrl())) {
mEventBus.post(new BrowserEvents.AddBookmark(currentView.getTitle(), mEventBus.post(new BrowserEvents.AddBookmark(currentView.getTitle(),
currentView.getUrl())); currentView.getUrl()));
} }
@ -893,24 +893,38 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
void handleNewIntent(Intent intent) { void handleNewIntent(Intent intent) {
String url = null; final String url;
if (intent != null) { if (intent != null) {
url = intent.getDataString(); url = intent.getDataString();
} else {
url = null;
} }
int num = 0; int num = 0;
String source = null; final String source;
if (intent != null && intent.getExtras() != null) { if (intent != null && intent.getExtras() != null) {
num = intent.getExtras().getInt(getPackageName() + ".Origin"); num = intent.getExtras().getInt(getPackageName() + ".Origin");
source = intent.getExtras().getString("SOURCE"); source = intent.getExtras().getString("SOURCE");
} else {
source = null;
} }
if (num == 1) { if (num == 1) {
loadUrlInCurrentView(url); loadUrlInCurrentView(url);
} else if (url != null) { } else if (url != null) {
if (url.startsWith(Constants.FILE)) { if (url.startsWith(Constants.FILE)) {
Utils.showSnackbar(this, R.string.message_blocked_local); AlertDialog.Builder builder = new AlertDialog.Builder(this);
url = null; builder.setCancelable(true)
.setMessage(R.string.message_blocked_local)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.action_open, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
newTab(url, true);
} }
})
.show();
} else {
newTab(url, true); newTab(url, true);
}
mIsNewIntent = (source == null); mIsNewIntent = (source == null);
} }
} }
@ -986,7 +1000,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
int current = tabsManager.positionOf(currentTab); int current = tabsManager.positionOf(currentTab);
if (!tabToDelete.getUrl().startsWith(Constants.FILE) && !isIncognito()) { if (!UrlUtils.isSpecialUrl(tabToDelete.getUrl()) && !isIncognito()) {
mPreferences.setSavedUrl(tabToDelete.getUrl()); mPreferences.setSavedUrl(tabToDelete.getUrl());
} }
final boolean isShown = tabToDelete.isShown(); final boolean isShown = tabToDelete.isShown();
@ -1014,7 +1028,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
tabsManager.deleteTab(position); tabsManager.deleteTab(position);
} }
} else { } else {
if (currentTab != null && (currentTab.getUrl().startsWith(Constants.FILE) if (currentTab != null && (UrlUtils.isSpecialUrl(currentTab.getUrl())
|| currentTab.getUrl().equals(mHomepage))) { || currentTab.getUrl().equals(mHomepage))) {
closeActivity(); closeActivity();
} else { } else {
@ -1273,7 +1287,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
final LightningView currentTab = tabsManager.getCurrentTab(); final LightningView currentTab = tabsManager.getCurrentTab();
mEventBus.post(new BrowserEvents.CurrentPageUrl(url)); mEventBus.post(new BrowserEvents.CurrentPageUrl(url));
if (shortUrl && !url.startsWith(Constants.FILE)) { if (shortUrl && !UrlUtils.isSpecialUrl(url)) {
switch (mPreferences.getUrlBoxContentChoice()) { switch (mPreferences.getUrlBoxContentChoice()) {
case 0: // Default, show only the domain case 0: // Default, show only the domain
url = url.replaceFirst(Constants.HTTP, ""); url = url.replaceFirst(Constants.HTTP, "");
@ -1292,7 +1306,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
break; break;
} }
} else { } else {
if (url.startsWith(Constants.FILE)) { if (UrlUtils.isSpecialUrl(url)) {
url = ""; url = "";
} }
mSearch.setText(url); mSearch.setText(url);
@ -1320,7 +1334,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
} }
}; };
if (!url.startsWith(Constants.FILE)) { if (!UrlUtils.isSpecialUrl(url)) {
new Thread(update).start(); new Thread(update).start();
} }
} }

11
app/src/main/java/acr/browser/lightning/utils/UrlUtils.java

@ -21,6 +21,9 @@ import android.webkit.URLUtil;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
/** /**
* Utility methods for Url manipulation * Utility methods for Url manipulation
*/ */
@ -145,4 +148,12 @@ public class UrlUtils {
} }
return inUrl; return inUrl;
} }
/**
* Returns whether the given url is the bookmarks/history page or a normal website
*/
public static boolean isSpecialUrl(String url) {
return url != null && url.startsWith(Constants.FILE)
&& (url.endsWith(Constants.BOOKMARKS_FILENAME) || url.endsWith(HistoryPage.FILENAME));
}
} }

3
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -52,6 +52,7 @@ import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
public class LightningView { public class LightningView {
@ -612,7 +613,7 @@ public class LightningView {
private void longClickPage(final String url) { private void longClickPage(final String url) {
final WebView.HitTestResult result = mWebView.getHitTestResult(); final WebView.HitTestResult result = mWebView.getHitTestResult();
String currentUrl = mWebView.getUrl(); String currentUrl = mWebView.getUrl();
if (currentUrl != null && currentUrl.startsWith(Constants.FILE)) { if (currentUrl != null && UrlUtils.isSpecialUrl(currentUrl)) {
if (currentUrl.endsWith(HistoryPage.FILENAME)) { if (currentUrl.endsWith(HistoryPage.FILENAME)) {
if (url != null) { if (url != null) {
mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, url); mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, url);

Loading…
Cancel
Save