Skip to content

Commit

Permalink
[Android][Sync] Show error for non-HTTPS self-host URL input and Mino…
Browse files Browse the repository at this point in the history
…r fixups, formatting.
  • Loading branch information
jagadeshjai committed Dec 19, 2024
1 parent d3a09f5 commit fc1c9f8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@
import org.chromium.chrome.browser.sync.SyncServiceFactory;
import org.chromium.chrome.browser.sync.settings.BraveManageSyncSettings;
import org.chromium.components.browser_ui.settings.SettingsNavigation;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.sync.SyncService;
import org.chromium.ui.base.DeviceFormFactor;
import org.chromium.url.GURL;

import java.io.IOException;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -128,7 +130,7 @@ public class BraveSyncScreensPreference extends BravePreferenceFragment
private Button mDeleteAccountButton;
private Button mNewCodeWordsButton;
private Button mNewQrCodeButton;
private TextView mBraveSyncCustomSyncUrl;
private TextView mBraveReadOnlySyncUrlView;
private TextView mBraveSyncTextDevicesTitle;
private TextView mBraveSyncWordCountTitle;
private TextView mBraveSyncAddDeviceCodeWords;
Expand Down Expand Up @@ -338,8 +340,12 @@ private void fillDevices() {
}
}

// TODO: Check and hide if custom sync URL is empty
mBraveSyncCustomSyncUrl.setText(getBraveSyncWorker().getSyncServiceURL());
LinearLayout customSyncUrlReadOnlyLayout =
getView().findViewById(R.id.brave_read_only_sync_url_layout);
if (!sCustomSyncServiceUrl.isEmpty()) {
mBraveReadOnlySyncUrlView.setText(sCustomSyncServiceUrl);
customSyncUrlReadOnlyLayout.setVisibility(View.VISIBLE);
}

if (index > 0) {
mBraveSyncTextDevicesTitle.setText(
Expand Down Expand Up @@ -450,7 +456,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
mCopyButton.setOnClickListener(this);
}

mBraveSyncCustomSyncUrl = getView().findViewById(R.id.brave_sync_url_display);
mBraveReadOnlySyncUrlView = getView().findViewById(R.id.brave_read_only_sync_url_view);
mBraveSyncTextDevicesTitle = getView().findViewById(R.id.brave_sync_devices_title);
mBraveSyncWordCountTitle = getView().findViewById(R.id.brave_sync_text_word_count);
mBraveSyncWordCountTitle.setText(getString(R.string.brave_sync_word_count_text, 0));
Expand Down Expand Up @@ -1356,10 +1362,17 @@ private void saveCustomSyncUrlAndRelaunch() {
return;
}

// Check if the URL starts with a valid scheme (http:// or https://)
if (!url.matches("^[a-zA-Z][a-zA-Z0-9+.-]*://.*")) {
// Prepend 'http://' if it doesn't
url = "http://" + url;
GURL gurl = new GURL(url);
// Check if the URL starts with a valid scheme, if not, just prepend with https scheme.
if (!gurl.isValid() || gurl.getScheme().isEmpty()) {
url = "https://" + url;
gurl = new GURL(url);
}

// Check and show error when it is not an HTTPS Scheme.
if (!gurl.getScheme().equals(UrlConstants.HTTPS_SCHEME)) {
mCustomSyncUrlInput.setError(getString(R.string.brave_sync_url_https_error));
return;
}

// Validate the modified URL
Expand Down
13 changes: 5 additions & 8 deletions android/java/res/layout/brave_sync_done.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,26 @@
android:textColor="@color/modern_grey_500"
android:visibility="gone" />

<LinearLayout android:id="@+id/brave_sync_url_block"
<LinearLayout android:id="@+id/brave_read_only_sync_url_layout"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingVertical="12dp"
android:paddingHorizontal="16dp"
>
android:visibility="gone">
<TextView
android:layout_width="328dp"
android:layout_height="24dp"
android:text="@string/brave_sync_self_host_url_title"
android:textAppearance="@style/BraveSyncTitleTextHeadingH4"
android:gravity="top"
/>
android:gravity="top" />
<TextView
android:id="@+id/brave_sync_url_display"
android:id="@+id/brave_read_only_sync_url_view"
android:layout_width="328dp"
android:layout_height="22dp"
android:text="@string/brave_sync_advanced_options"
android:textAppearance="@style/BraveSyncUrlDisplay"
android:gravity="top"
/>

android:gravity="top" />
</LinearLayout>


Expand Down
3 changes: 3 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,9 @@ This file contains all "about" strings. It is set to NOT be translated, in tran
<message name="IDS_BRAVE_SYNC_URL_RESET_DEFAULT" desc="Text for Brave Sync self host url reset button">
Reset to default
</message>
<message name="IDS_BRAVE_SYNC_URL_HTTPS_ERROR" desc="Error message for Brave Sync self host url must be an HTTPS URL">
Brave Sync only supports HTTPS connections. Please enter a valid HTTPS sync URL.
</message>
<message name="IDS_BRAVE_SYNC_ADVANCED_OPTIONS_DIALOG_DESC" desc="Text for Brave Sync advanced options dialog description.">
Brave's sync protocol is open and you can provide your own server implementation. This requires that you host and configure the brave/go-sync implementation yourself. You can then provide the URL to this service and the browser will sync with your implementation instead. To confirm the change, you'll need to restart the browser and re-open sync options to continue.
</message>
Expand Down

0 comments on commit fc1c9f8

Please sign in to comment.