// Token: 0x06000E47 RID: 3655 RVA: 0x00055940 File Offset: 0x00053B40
 private bool IsMailboxNeedingPolicyUpdate(MailboxSession mailboxSession, SharingPolicyCache sharingPolicyCache)
 {
     byte[] valueOrDefault = mailboxSession.Mailbox.GetValueOrDefault <byte[]>(MailboxSchema.LastSharingPolicyAppliedId, null);
     if (valueOrDefault == null)
     {
         SharingPolicyAssistant.Tracer.TraceDebug <Guid>((long)this.GetHashCode(), "{0}: mailbox does not have LastSharingPolicyAppliedId present and is ready to have policy applied.", mailboxSession.MailboxGuid);
         return(true);
     }
     if (valueOrDefault.Length != SharingPolicyAssistant.GuidBytesLength)
     {
         SharingPolicyAssistant.Tracer.TraceDebug <Guid>((long)this.GetHashCode(), "{0}: mailbox does have LastSharingPolicyAppliedId set, but its value is corrupted. Mailbox ready to have policy applied.", mailboxSession.MailboxGuid);
         return(true);
     }
     byte[] valueOrDefault2 = mailboxSession.Mailbox.GetValueOrDefault <byte[]>(MailboxSchema.LastSharingPolicyAppliedHash, null);
     if (valueOrDefault2 == null)
     {
         SharingPolicyAssistant.Tracer.TraceDebug <Guid>((long)this.GetHashCode(), "{0}: mailbox does not have LastSharingPolicyAppliedHash present yet. Mailbox ready to have policy applied.", mailboxSession.MailboxGuid);
         return(true);
     }
     if (!ArrayComparer <byte> .Comparer.Equals(sharingPolicyCache.Hash, valueOrDefault2))
     {
         SharingPolicyAssistant.Tracer.TraceDebug <Guid>((long)this.GetHashCode(), "{0}: mailbox has different sharing policy applied than the one in the AD. Mailbox ready to have policy applied.", mailboxSession.MailboxGuid);
         return(true);
     }
     SharingPolicyAssistant.Tracer.TraceDebug <Guid>((long)this.GetHashCode(), "{0}: mailbox has same sharing policy applied as the one in the AD. No need to apply policy to this mailbox.", mailboxSession.MailboxGuid);
     return(false);
 }
        // Token: 0x06000E48 RID: 3656 RVA: 0x00055A2C File Offset: 0x00053C2C
        protected override void InvokeInternal(InvokeArgs invokeArgs, List <KeyValuePair <string, object> > customDataToLog)
        {
            MailboxSession mailboxSession = invokeArgs.StoreSession as MailboxSession;

            if (mailboxSession == null)
            {
                return;
            }
            ADUser aduser = SharingPolicyAssistant.GetADUser(mailboxSession.MailboxOwner, mailboxSession.GetADRecipientSession(true, ConsistencyMode.IgnoreInvalid));

            if (aduser == null)
            {
                return;
            }
            if (ADRecipient.IsSystemMailbox(aduser.RecipientTypeDetails))
            {
                SharingPolicyAssistant.Tracer.TraceDebug <IExchangePrincipal, RecipientTypeDetails>((long)this.GetHashCode(), "{0}: Skipping the mailbox processing as it is a system mailbox. RecipientTypeDetails {1}.", mailboxSession.MailboxOwner, aduser.RecipientTypeDetails);
                return;
            }
            SharingPolicyAssistant.Tracer.TraceDebug <IExchangePrincipal>((long)this.GetHashCode(), "{0}: Begin process mailbox", mailboxSession.MailboxOwner);
            SharingPolicyCache sharingPolicyCache = SharingPolicyCache.Get(aduser);
            bool flag = false;

            if (sharingPolicyCache != null)
            {
                if (!this.IsMailboxNeedingPolicyUpdate(mailboxSession, sharingPolicyCache))
                {
                    return;
                }
                flag = this.ApplyPolicy(mailboxSession, sharingPolicyCache.Policy);
            }
            if (flag)
            {
                SharingPolicyAssistant.Tracer.TraceDebug <IExchangePrincipal>((long)this.GetHashCode(), "{0}: Storing applied policy to mailbox table.", mailboxSession.MailboxOwner);
                SharingPolicyAssistant.UpdateMailboxData(mailboxSession.Mailbox, (aduser.SharingPolicy == null && !sharingPolicyCache.BelongsToDehydratedContainer) ? SharingPolicyCache.DynamicDefaultPolicy.ObjectGuid.ToByteArray() : sharingPolicyCache.Policy.Id.ObjectGuid.ToByteArray(), sharingPolicyCache.Hash);
            }
            else
            {
                SharingPolicyAssistant.Tracer.TraceWarning <IExchangePrincipal>(0L, "{0}: Unable to find sharing policy for this mailbox.", mailboxSession.MailboxOwner);
                ExDateTime?exDateTime = mailboxSession.Mailbox.TryGetProperty(MailboxSchema.LastSharingPolicyAppliedTime) as ExDateTime?;
                byte[]     array      = mailboxSession.Mailbox.TryGetProperty(MailboxSchema.LastSharingPolicyAppliedHash) as byte[];
                if (exDateTime == null || array != null)
                {
                    SharingPolicyAssistant.UpdateMailboxData(mailboxSession.Mailbox, null, null);
                }
                else
                {
                    TimeSpan retryTimeSpan = ExDateTime.UtcNow.Subtract(exDateTime.Value);
                    if (ExDateTime.UtcNow.Subtract(exDateTime.Value) > SharingPolicyAssistant.RetryThresholdLimit)
                    {
                        SharingPolicyAssistant.SubmitInformationalWatson(mailboxSession.MailboxOwner, retryTimeSpan);
                        SharingPolicyAssistant.UpdateMailboxData(mailboxSession.Mailbox, null, null);
                    }
                }
            }
            SharingPolicyAssistant.Tracer.TraceDebug <IExchangePrincipal>((long)this.GetHashCode(), "{0}: End process mailbox", mailboxSession.MailboxOwner);
        }
        // Token: 0x06000E60 RID: 3680 RVA: 0x00056750 File Offset: 0x00054950
        private static SharingPolicyCache GetHydratedPolicyFromCacheOrAD(OrganizationId orgId, ADObjectId policyId)
        {
            SharingPolicyCache sharingPolicyCache;

            lock (SharingPolicyCache.cacheSyncLock)
            {
                if (policyId == SharingPolicyCache.DynamicDefaultPolicy)
                {
                    if (SharingPolicyCache.hydratedDefaultCache.TryGetValue(orgId, out sharingPolicyCache))
                    {
                        SharingPolicyCache.Tracer.TraceDebug <ADObjectId, OrganizationId>(0L, "Found Default Sharing Policy {0} in hydratedDefaultCache for Org {1}.", policyId, orgId);
                        return(sharingPolicyCache);
                    }
                }
                else if (SharingPolicyCache.hydratedCache.TryGetValue(policyId.ObjectGuid, out sharingPolicyCache))
                {
                    SharingPolicyCache.Tracer.TraceDebug <ADObjectId, OrganizationId>(0L, "Found Sharing Policy {0} in hydratedCache for Org {1}.", policyId, orgId);
                    return(sharingPolicyCache);
                }
            }
            IConfigurationSession tenantOrTopologyConfigurationSession = DirectorySessionFactory.Default.GetTenantOrTopologyConfigurationSession(ConsistencyMode.IgnoreInvalid, ADSessionSettings.FromOrganizationIdWithoutRbacScopesServiceOnly(orgId), 211, "GetHydratedPolicyFromCacheOrAD", "f:\\15.00.1497\\sources\\dev\\MailboxAssistants\\src\\assistants\\SharingPolicy\\SharingPolicyCache.cs");

            tenantOrTopologyConfigurationSession.SessionSettings.IsSharedConfigChecked = true;
            bool flag2 = policyId == SharingPolicyCache.DynamicDefaultPolicy;

            if (flag2)
            {
                policyId = SharingPolicyCache.GetDefaultSharingPolicyId(tenantOrTopologyConfigurationSession);
                if (policyId == null)
                {
                    return(null);
                }
            }
            SharingPolicy sharingPolicy = tenantOrTopologyConfigurationSession.Read <SharingPolicy>(policyId);

            if (sharingPolicy == null)
            {
                SharingPolicyCache.Tracer.TraceWarning <ADObjectId, bool>(0L, "Unable to find SharingPolicy {0} in the AD. PolicyId is default:{1}", policyId, flag2);
                sharingPolicyCache = null;
            }
            else
            {
                sharingPolicyCache = new SharingPolicyCache(sharingPolicy, false);
            }
            lock (SharingPolicyCache.cacheSyncLock)
            {
                SharingPolicyCache.Tracer.TraceDebug <ADObjectId>(0L, "Add the Sharing Policy {0} to cache.", policyId);
                SharingPolicyCache.hydratedCache[policyId.ObjectGuid] = sharingPolicyCache;
                if (sharingPolicyCache != null && flag2)
                {
                    SharingPolicyCache.Tracer.TraceDebug <ADObjectId, OrganizationId>(0L, "Add Default Sharing Policy {0} of Org {1} to hydrated cache ", policyId, orgId);
                    SharingPolicyCache.hydratedDefaultCache[orgId] = sharingPolicyCache;
                }
            }
            return(sharingPolicyCache);
        }
        // Token: 0x06000E61 RID: 3681 RVA: 0x000568D8 File Offset: 0x00054AD8
        private static SharingPolicyCache GetDehydratedPolicyFromCacheOrAD(OrganizationId orgId, ADObjectId policyId)
        {
            SharingPolicyCache sharingPolicyCache;

            if (policyId != SharingPolicyCache.DynamicDefaultPolicy)
            {
                lock (SharingPolicyCache.cacheSyncLock)
                {
                    if (SharingPolicyCache.dehydratedDefaultCache.TryGetValue(policyId.ObjectGuid, out sharingPolicyCache))
                    {
                        SharingPolicyCache.Tracer.TraceDebug <ADObjectId>(0L, "Found Sharing Policy {0} in dehydrated cache.", policyId);
                        return(sharingPolicyCache);
                    }
                }
            }
            SharedConfiguration   sharedConfiguration = SharedConfiguration.GetSharedConfiguration(orgId);
            IConfigurationSession tenantOrTopologyConfigurationSession = DirectorySessionFactory.Default.GetTenantOrTopologyConfigurationSession(ConsistencyMode.IgnoreInvalid, sharedConfiguration.GetSharedConfigurationSessionSettings(), 282, "GetDehydratedPolicyFromCacheOrAD", "f:\\15.00.1497\\sources\\dev\\MailboxAssistants\\src\\assistants\\SharingPolicy\\SharingPolicyCache.cs");

            tenantOrTopologyConfigurationSession.SessionSettings.IsSharedConfigChecked = true;
            bool flag2 = policyId == SharingPolicyCache.DynamicDefaultPolicy;

            if (flag2)
            {
                SharingPolicyCache.Tracer.TraceDebug <OrganizationId>(0L, "Find Default Policy Id for Org {0} ", orgId);
                policyId = SharingPolicyCache.GetDefaultSharingPolicyId(tenantOrTopologyConfigurationSession);
                if (policyId == null)
                {
                    return(null);
                }
                lock (SharingPolicyCache.cacheSyncLock)
                {
                    if (SharingPolicyCache.dehydratedDefaultCache.TryGetValue(policyId.ObjectGuid, out sharingPolicyCache))
                    {
                        SharingPolicyCache.Tracer.TraceDebug <ADObjectId>(0L, "Found Sharing Policy {0} in dehydrated cache.", policyId);
                        return(sharingPolicyCache);
                    }
                }
            }
            SharingPolicy sharingPolicy = tenantOrTopologyConfigurationSession.Read <SharingPolicy>(policyId);

            if (sharingPolicy == null)
            {
                SharingPolicyCache.Tracer.TraceError <ADObjectId, bool>(0L, "Unable to find SharingPolicy {0} in the AD. PolicyId is default:{1}", policyId, flag2);
                return(null);
            }
            sharingPolicyCache = new SharingPolicyCache(sharingPolicy, true);
            lock (SharingPolicyCache.cacheSyncLock)
            {
                SharingPolicyCache.dehydratedDefaultCache[policyId.ObjectGuid] = sharingPolicyCache;
            }
            return(sharingPolicyCache);
        }
        // Token: 0x06000E5F RID: 3679 RVA: 0x000566F0 File Offset: 0x000548F0
        private static SharingPolicyCache InternalGet(OrganizationId orgId, ADObjectId policyId)
        {
            orgId    = (orgId ?? OrganizationId.ForestWideOrgId);
            policyId = (policyId ?? SharingPolicyCache.DynamicDefaultPolicy);
            bool flag = SharedConfiguration.IsDehydratedConfiguration(orgId);

            SharingPolicyCache.Tracer.TraceDebug <ADObjectId, string, OrganizationId>(0L, "Find Sharing policy {0} in {1} Org {2} ", policyId, flag ? "dehydrated" : "hydrated", orgId);
            if (!flag)
            {
                return(SharingPolicyCache.GetHydratedPolicyFromCacheOrAD(orgId, policyId));
            }
            return(SharingPolicyCache.GetDehydratedPolicyFromCacheOrAD(orgId, policyId));
        }
 // Token: 0x06000E5E RID: 3678 RVA: 0x000566C5 File Offset: 0x000548C5
 internal static SharingPolicyCache Get(ADObjectId policyId, TenantPartitionHint tenantPartitionHint)
 {
     ArgumentValidator.ThrowIfNull("policyId", policyId);
     ArgumentValidator.ThrowIfNull("tenantPartitionHint", tenantPartitionHint);
     return(SharingPolicyCache.InternalGet(ADSessionSettings.FromTenantPartitionHint(tenantPartitionHint).CurrentOrganizationId, policyId));
 }
 // Token: 0x06000E5D RID: 3677 RVA: 0x000566A7 File Offset: 0x000548A7
 internal static SharingPolicyCache Get(ADUser adUser)
 {
     ArgumentValidator.ThrowIfNull("adUser", adUser);
     return(SharingPolicyCache.InternalGet(adUser.OrganizationId, adUser.SharingPolicy));
 }
 // Token: 0x06000E40 RID: 3648 RVA: 0x000558E2 File Offset: 0x00053AE2
 public void OnWorkCycleCheckpoint()
 {
     SharingPolicyCache.PurgeCache();
 }
 // Token: 0x06000E45 RID: 3653 RVA: 0x00055935 File Offset: 0x00053B35
 public void OnBeforeDemandJob(Guid mailboxGuid, Guid databaseGuid)
 {
     SharingPolicyCache.PurgeCache();
 }