@@ -32,6 +32,7 @@ import androidx.lifecycle.flowWithLifecycle
32
32
import androidx.lifecycle.lifecycleScope
33
33
import androidx.webkit.WebViewCompat
34
34
import com.duckduckgo.anvil.annotations.InjectWith
35
+ import com.duckduckgo.autofill.api.AutofillFeature
35
36
import com.duckduckgo.autofill.api.AutofillFragmentResultsPlugin
36
37
import com.duckduckgo.autofill.api.BrowserAutofill
37
38
import com.duckduckgo.autofill.api.CredentialAutofillDialogFactory
@@ -47,7 +48,9 @@ import com.duckduckgo.autofill.impl.importing.gpm.webflow.autofill.NoOpAutofillE
47
48
import com.duckduckgo.autofill.impl.importing.gpm.webflow.autofill.NoOpEmailProtectionInContextSignupFlowListener
48
49
import com.duckduckgo.autofill.impl.importing.gpm.webflow.autofill.NoOpEmailProtectionUserPromptListener
49
50
import com.duckduckgo.autofill.impl.importing.takeout.store.BookmarkImportConfigStore
51
+ import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarkResult.Error
50
52
import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarkResult.Success
53
+ import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarkResult.UserCancelled
51
54
import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarksWebFlowViewModel.Command.ExitFlowAsFailure
52
55
import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarksWebFlowViewModel.Command.ExitFlowWithSuccess
53
56
import com.duckduckgo.autofill.impl.importing.takeout.webflow.ImportGoogleBookmarksWebFlowViewModel.Command.InjectCredentialsFromReauth
@@ -121,6 +124,9 @@ class ImportGoogleBookmarksWebFlowFragment :
121
124
@Inject
122
125
lateinit var browserAutofillConfigurator: InternalBrowserAutofillConfigurator
123
126
127
+ @Inject
128
+ lateinit var autofillFeature: AutofillFeature
129
+
124
130
private var binding: FragmentImportGoogleBookmarksWebflowBinding ? = null
125
131
private var cancellationDialog: DaxAlertDialog ? = null
126
132
@@ -193,16 +199,12 @@ class ImportGoogleBookmarksWebFlowFragment :
193
199
// Inject null to indicate no credentials available
194
200
browserAutofill.injectCredentials(null )
195
201
}
196
- is PromptUserToSelectFromStoredCredentials ->
197
- showCredentialChooserDialog(
198
- command.originalUrl,
199
- command.credentials,
200
- command.triggerType,
201
- )
202
- is ExitFlowWithSuccess -> {
203
- logcat { " Bookmark-import: ExitFlowWithSuccess received with count: ${command.importedCount} " }
204
- exitFlowAsSuccess(command.importedCount)
205
- }
202
+ is PromptUserToSelectFromStoredCredentials -> showCredentialChooserDialog(
203
+ command.originalUrl,
204
+ command.credentials,
205
+ command.triggerType,
206
+ )
207
+ is ExitFlowWithSuccess -> exitFlowAsSuccess(command.importedCount)
206
208
is ExitFlowAsFailure -> exitFlowAsError(command.reason)
207
209
is PromptUserToConfirmFlowCancellation -> askUserToConfirmCancellation()
208
210
}
@@ -221,12 +223,11 @@ class ImportGoogleBookmarksWebFlowFragment :
221
223
return @withContext
222
224
}
223
225
224
- val credentials =
225
- LoginCredentials (
226
- domain = url,
227
- username = username,
228
- password = password,
229
- )
226
+ val credentials = LoginCredentials (
227
+ domain = url,
228
+ username = username,
229
+ password = password,
230
+ )
230
231
231
232
logcat { " Injecting re-authentication credentials" }
232
233
browserAutofill.injectCredentials(credentials)
@@ -317,6 +318,19 @@ class ImportGoogleBookmarksWebFlowFragment :
317
318
} else {
318
319
logcat(WARN ) { " Bookmark-import: Not able to inject bookmark import JavaScript" }
319
320
}
321
+
322
+ val canAddMessageListener = withContext(dispatchers.io()) {
323
+ autofillFeature.canUseWebMessageListenerDuringBookmarkImport().isEnabled()
324
+ }
325
+
326
+ if (canAddMessageListener) {
327
+ WebViewCompat .addWebMessageListener(webView, " ddgBookmarkImport" , setOf (" *" )) { _, message, sourceOrigin, _, _ ->
328
+ val data = message.data ? : return @addWebMessageListener
329
+ viewModel.onWebMessageReceived(data)
330
+ }
331
+ } else {
332
+ logcat(WARN ) { " Bookmark-import: Not able to add WebMessage listener for bookmark import" }
333
+ }
320
334
}
321
335
322
336
private fun initialiseToolbar () {
@@ -375,35 +389,32 @@ class ImportGoogleBookmarksWebFlowFragment :
375
389
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, onBackPressedCallback)
376
390
}
377
391
378
- private fun exitFlowAsSuccess (bookmarkCount : Int ) {
379
- logcat { " Bookmark-import: Reporting import success with bookmarkCount: $bookmarkCount " }
392
+ private fun exitFlowAsSuccess (importedCount : Int = 0 ) {
393
+ logcat { " Bookmark-import: Reporting import success with bookmarkCount: $importedCount " }
380
394
dismissCancellationDialog()
381
- val result =
382
- Bundle ().apply {
383
- putParcelable(ImportGoogleBookmarkResult .RESULT_KEY_DETAILS , Success (bookmarkCount))
384
- }
395
+ val result = Bundle ().apply {
396
+ putParcelable(ImportGoogleBookmarkResult .Companion .RESULT_KEY_DETAILS , Success (importedCount))
397
+ }
385
398
parentFragmentManager.setFragmentResult(ImportGoogleBookmarkResult .RESULT_KEY , result)
386
399
}
387
400
388
401
private fun exitFlowAsCancellation (stage : String ) {
389
402
logcat { " Bookmark-import: Flow cancelled at stage: $stage " }
390
403
dismissCancellationDialog()
391
404
392
- val result =
393
- Bundle ().apply {
394
- putParcelable(ImportGoogleBookmarkResult .Companion .RESULT_KEY_DETAILS , ImportGoogleBookmarkResult .UserCancelled (stage))
395
- }
405
+ val result = Bundle ().apply {
406
+ putParcelable(ImportGoogleBookmarkResult .Companion .RESULT_KEY_DETAILS , UserCancelled (stage))
407
+ }
396
408
setFragmentResult(ImportGoogleBookmarkResult .Companion .RESULT_KEY , result)
397
409
}
398
410
399
411
private fun exitFlowAsError (reason : UserCannotImportReason ) {
400
412
logcat { " Bookmark-import: Flow error at stage: ${reason.mapToStage()} " }
401
413
dismissCancellationDialog()
402
414
403
- val result =
404
- Bundle ().apply {
405
- putParcelable(ImportGoogleBookmarkResult .Companion .RESULT_KEY_DETAILS , ImportGoogleBookmarkResult .Error (reason))
406
- }
415
+ val result = Bundle ().apply {
416
+ putParcelable(ImportGoogleBookmarkResult .Companion .RESULT_KEY_DETAILS , Error (reason))
417
+ }
407
418
setFragmentResult(ImportGoogleBookmarkResult .Companion .RESULT_KEY , result)
408
419
}
409
420
@@ -419,13 +430,12 @@ class ImportGoogleBookmarksWebFlowFragment :
419
430
return @withContext
420
431
}
421
432
422
- val dialog =
423
- credentialAutofillDialogFactory.autofillSelectCredentialsDialog(
424
- url,
425
- credentials,
426
- triggerType,
427
- CUSTOM_FLOW_TAB_ID ,
428
- )
433
+ val dialog = credentialAutofillDialogFactory.autofillSelectCredentialsDialog(
434
+ url,
435
+ credentials,
436
+ triggerType,
437
+ CUSTOM_FLOW_TAB_ID ,
438
+ )
429
439
dialog.show(childFragmentManager, SELECT_CREDENTIALS_FRAGMENT_TAG )
430
440
}
431
441
}
0 commit comments