コード例 #1
0
        /// <summary>
        /// Gets all registered instances of the given service.
        /// </summary>
        /// <typeparam name="T">The type of service to resolve.</typeparam>
        /// <returns>An enumerable with all service instances.</returns>
        public IEnumerable <T> GetServices <T>() where T : class
        {
            var count = _services.Count;

            for (var i = 0; i < count; i++)
            {
                var service  = _services[i];
                var instance = service as T;

                if (instance == null)
                {
                    var creator = service as Func <IBrowsingContext, T>;

                    if (creator == null)
                    {
                        continue;
                    }

                    instance     = creator.Invoke(this);
                    _services[i] = instance;
                }

                yield return(instance);
            }

            if (_parent != null)
            {
                foreach (var service in _parent.GetServices <T>())
                {
                    yield return(service);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads the given URI by using an asynchronous request.
        /// </summary>
        /// <param name="request">The data of the request to send.</param>
        /// <param name="cancel">The cancellation token to use..</param>
        /// <returns>
        /// The task which will eventually return the response.
        /// </returns>
        protected async Task <IResponse> LoadAsync(Request request, CancellationToken cancel)
        {
            var requesters    = _context.GetServices <IRequester>();
            var response      = default(IResponse);
            var redirectCount = 0;

            AppendCookieTo(request);

            do
            {
                if (response != null)
                {
                    redirectCount++;
                    ExtractCookieFrom(response);
                    request = CreateNewRequest(request, response);
                    AppendCookieTo(request);
                }

                foreach (var requester in requesters)
                {
                    if (requester.SupportsProtocol(request.Address.Scheme))
                    {
                        _context.Fire(new RequestEvent(request, null));
                        response = await requester.RequestAsync(request, cancel).ConfigureAwait(false);

                        _context.Fire(new RequestEvent(request, response));
                        break;
                    }
                }
            }while (response != null && response.StatusCode.IsRedirected() && redirectCount < MaxRedirects);

            return(response);
        }
コード例 #3
0
        /// <summary>
        /// Gets the spell check service for the given language, if any.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <param name="language">The language of the spellchecker.</param>
        /// <returns>The spell check service, if any.</returns>
        public static ISpellCheckService GetSpellCheck(this IBrowsingContext context, String language)
        {
            var substitute = default(ISpellCheckService);
            var services   = context.GetServices <ISpellCheckService>();
            var culture    = context.GetCultureFrom(language);
            var twoLetters = culture.TwoLetterISOLanguageName;

            foreach (var service in services)
            {
                var otherCulture = service.Culture;

                if (otherCulture != null)
                {
                    var otherTwoLetters = otherCulture.TwoLetterISOLanguageName;

                    if (otherCulture.Equals(culture))
                    {
                        return(service);
                    }
                    else if (substitute == null && otherTwoLetters.Is(twoLetters))
                    {
                        substitute = service;
                    }
                }
            }

            return(substitute);
        }
コード例 #4
0
        /// <summary>
        /// Gets a resource service. Multiple resource services may be registered, so
        /// the one that matches the given mime-type will be returned, if any.
        /// </summary>
        /// <typeparam name="TResource">The type of the resource service.</typeparam>
        /// <param name="context">The current context.</param>
        /// <param name="type">The mime-type of the resource.</param>
        /// <returns>The service instance or null.</returns>
        public static IResourceService <TResource> GetResourceService <TResource>(this IBrowsingContext context, String type)
            where TResource : IResourceInfo
        {
            var services = context.GetServices <IResourceService <TResource> >();

            foreach (var service in services)
            {
                if (service.SupportsType(type))
                {
                    return(service);
                }
            }

            return(default);
コード例 #5
0
        /// <summary>
        /// Tries to get the scripting service for the given mime-type.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <param name="type">The type of the scripting language.</param>
        /// <returns>The scripting service, if any.</returns>
        public static IScriptingService GetScripting(this IBrowsingContext context, String type)
        {
            var services = context.GetServices <IScriptingService>();

            foreach (var service in services)
            {
                if (service.SupportsType(type))
                {
                    return(service);
                }
            }

            return(default(IScriptingService));
        }
コード例 #6
0
 public Boolean SupportsProtocol(String protocol) =>
 _context.GetServices <IRequester>().Any(m => m.SupportsProtocol(protocol));
コード例 #7
0
 /// <summary>
 /// Gets a provider service instance. At most one has to be available.
 /// </summary>
 /// <typeparam name="TProvider">The type of the provider service.</typeparam>
 /// <param name="context">The current context.</param>
 /// <returns>The provider instance or null.</returns>
 public static TProvider GetProvider <TProvider>(this IBrowsingContext context)
     where TProvider : class => context.GetServices <TProvider>().SingleOrDefault();
コード例 #8
0
 /// <summary>
 /// Gets a factory service instance. Exactly one has to be available.
 /// </summary>
 /// <typeparam name="TFactory">The type of the factory service.</typeparam>
 /// <param name="context">The current context.</param>
 /// <returns>The factory instance.</returns>
 public static TFactory GetFactory <TFactory>(this IBrowsingContext context)
     where TFactory : class => context.GetServices <TFactory>().Single();
コード例 #9
0
 /// <summary>
 /// Gets the navigation handler that supports the provided protocol.
 /// </summary>
 /// <param name="context">The browsing context to use.</param>
 /// <param name="url">The URL to navigate to.</param>
 /// <returns>The found navigation handler, if any.</returns>
 public static INavigationHandler GetNavigationHandler(this IBrowsingContext context, Url url) =>
 context.GetServices <INavigationHandler>().FirstOrDefault(m => m.SupportsProtocol(url.Scheme));
コード例 #10
0
 private static IEnumerable <Assembly> GetAssemblies(IBrowsingContext context) => context
 .GetServices <Object>()
 .Select(m => m.GetType().GetAssembly())
 .Distinct()
 .Where(m => m.FullName.StartsWith("AngleSharp"));
コード例 #11
0
 /// <summary>
 /// Gets if the context allows scripting or not.
 /// </summary>
 /// <param name="context">The current context.</param>
 /// <returns>True if a scripting provider is available, otherwise false.</returns>
 public static Boolean IsScripting(this IBrowsingContext context) => context.GetServices <IScriptingService>().Any();
コード例 #12
0
 private static IEnumerable <Assembly> GetAssemblies(IBrowsingContext context) =>
 context.GetServices <Object>().Select(m => m.GetType().Assembly).Distinct();