github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/service/deferred_deep_links.go (about)

     1  package service
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  )
     8  
     9  type StringReceiver interface {
    10  	CallbackWithString(s string)
    11  }
    12  
    13  type InstallReferrerListener interface {
    14  	// StartInstallReferrerListener is used to get referrer information from the
    15  	// google play store on Android (to implement deferred deep links). This is
    16  	// asynchronous (due to the underlying play store api being so): pass it a
    17  	// callback function which will be called with the referrer string once it
    18  	// is available (or an empty string in case of errors).
    19  	StartInstallReferrerListener(callback StringReceiver)
    20  }
    21  
    22  type installReferrerHandler struct {
    23  	libkb.MetaContextified
    24  }
    25  
    26  // CallbackWithString is called from Java when the Android Play Store api
    27  // returns the requested install referrer information.
    28  func (c installReferrerHandler) CallbackWithString(s string) {
    29  	m := c.M().WithLogTag("IRL")
    30  	m.Debug("installReferrerHandler#CallbackWithString")
    31  	defer func() {
    32  		err := c.G().Env.GetConfigWriter().SetAndroidInstallReferrerChecked(true)
    33  		if err != nil {
    34  			m.Warning("Error in SetAndroidInstallReferrerChecked: %v", err)
    35  		}
    36  	}()
    37  
    38  	m.Debug("Waiting for the GUI to be ready to receive notifications")
    39  	if !c.G().UIRouter.WaitForUIType(libkb.HomeUIKind, 30*time.Second) {
    40  		c.M().Debug("Dropping notification of referrer information: GUI did not connect in time")
    41  		return
    42  	}
    43  	m.Debug("Notifying GUI of deferred deep invite link")
    44  	c.G().NotifyRouter.HandleHandleKeybaseLink(c.M().Ctx(), s, true)
    45  }
    46  
    47  var _ StringReceiver = installReferrerHandler{}
    48  
    49  func (d *Service) startInstallReferrerListener(m libkb.MetaContext) {
    50  	m = m.WithLogTag("IRL")
    51  	m.Debug("Service#startInstallReferrerListener called")
    52  
    53  	if !libkb.IsAndroid() {
    54  		m.Debug("InstallReferrerListener only runs on Android; short-circuiting startInstallReferrerListener")
    55  		return
    56  	}
    57  
    58  	if d.referrerListener == nil {
    59  		m.Debug("referrerListener is nil; short-circuiting startInstallReferrerListener")
    60  		return
    61  	}
    62  
    63  	if m.G().Env.GetConfig().GetAndroidInstallReferrerChecked() {
    64  		m.Debug("AndroidInstallReferrer already checked; short-circuiting startInstallReferrerListener")
    65  		return
    66  	}
    67  
    68  	d.referrerListener.StartInstallReferrerListener(installReferrerHandler{MetaContextified: libkb.NewMetaContextified(m)})
    69  }
    70  
    71  func (d *Service) SetInstallReferrerListener(i InstallReferrerListener) {
    72  	d.G().Log.Debug("InstallReferrerListener set")
    73  	d.referrerListener = i
    74  }