retrieveLicense method

  1. @override
Future<Try<LcpLicense, LcpException>?> retrieveLicense(
  1. FileSystemEntity file,
  2. LcpAuthenticating? authentication,
  3. bool allowUserInteraction,
  4. dynamic sender,
)
override

Opens the LCP license of a protected publication, to access its DRM metadata and decipher its content.

@param authentication Used to retrieve the user passphrase if it is not already known. The request will be cancelled if no passphrase is found in the LCP passphrase storage and the provided authentication. @param allowUserInteraction Indicates whether the user can be prompted for their passphrase. @param sender Free object that can be used by reading apps to give some UX context when presenting dialogs with LcpAuthenticating.

Implementation

@override
Future<Try<LcpLicense, LcpException>?> retrieveLicense(
    FileSystemEntity file,
    LcpAuthenticating? authentication,
    bool allowUserInteraction,
    dynamic sender) async {
  try {
    LicenseContainer container =
        await LicenseContainer.createLicenseContainer(file.path);
    // WARNING: Using the Default dispatcher in the state machine code is critical. If we were using the Main Dispatcher,
    // calling runBlocking in LicenseValidation.handle would block the main thread and cause a severe issue
    // with LcpAuthenticating.retrievePassphrase. Specifically, the interaction of runBlocking and suspendCoroutine
    // blocks the current thread before the passphrase popup has been showed until some button not yet showed is clicked.
    LcpException? lcpException;
    LcpLicense? license = await _retrieveLicense(
            container, authentication, allowUserInteraction, sender)
        .onError((error, stackTrace) {
      lcpException = LcpException.wrap(error);
      return null;
    });

    if (license != null) {
      return Try.success(license);
    }
    if (lcpException != null) {
      return Try.failure(lcpException);
    }
    return null;
  } on Exception catch (e, stacktrace) {
    Fimber.d("retrieveLicense ERROR", ex: e, stacktrace: stacktrace);
    return Try.failure(LcpException.wrap(e));
  }
}