Native Client-application-url Loading

Source: Internet
Author: User
URL Loading Introduction

This sections describes how to use the URLLoader API to load resources such as images and sound files from a server into yo UR application.

The example discussed in the "This" is included in the SDK in the Directoryexamples/api/url_loader. Reference Information

For reference information related to loading data from URLs, see the following documentation:url_loader.h-contains Urll Oader class for loading data from URLs Url_request_info.h-contains URLRequest class for creating and manipulating URL re Quests Url_response_info.h-contains urlresponse class for examaning URL responses Background

When a user launches your Native Client Web application, Chrome downloads and caches your application ' s HTML file, Manifes T file (. nmf), and Native Client module (. Pexe or. nexe). If your application needs additional assets, such as images and sound files, it must explicitly load those assets. You can use the Pepper APIs described in this section to the load assets from a URL into your application.

After your ' ve loaded assets into your application, Chrome would cache those assets. To avoid being in the whim of the Chrome cache, however, you may want to use the Pepper FileIO APIs to write those assets T o a persistent, sandboxed location on the user ' s file system. The url_loader example

The SDK includes an example called Url_loader demonstrating downloading files from a server. This example have these primary files:index.html-the HTML code that launches the Native Client module. Example.js-the JavaScript file for index.html. It has code this sends a POSTMESSAGE request to the Native Client module when the "Get URL" button is clicked. Url_loader_success.html-an HTML file on the server whose contents is being retrieved using Theurlloader API. Url_loader.cc-the code, sets up and provides, and entry point into the Native client module. Url_loader_handler.cc-the code that retrieves the contents of the Url_loader_success.html file and returns the results ( This is where the bulk's is done).

The remainder of this document covers the code in the url_loader.cc and url_loader_handler.cc files. URL Loading Overview

Like many Pepper APIs, The urlloader api includes a set of methods the execute asynchronously and that Invo KE callback functions in your Native Client module. The high-level flow for the url_loader example is described below. Note that methods in the Namespace pp::urlloader are part of the PEPPER URLLOADER API, while the rest Of the functions is part of the code in the Native Client module (specifically in the fileurl_loader_handler.cc). The following image shows the flow of The url_loader_handler code:

Following is the high-level steps involved in URL loading. The Native Client module calls Pp::urlloader::open to begin opening the URL. When Open completes, it invokes a callback function in the Native Client module (in this case, OnOpen). The Native Client module calls the Pepper function urlloader::readresponsebody to begin reading the response body with the Data. Readresponsebody is passed a optional callback function in the Native Client module (in this case, on Read). The callback function is a optional callback becausereadresponsebody may read data and return synchronously if data is AV Ailable (This improves performance for large files and fast connections).

The remainder of this document demonstrates how the previous steps is implemented in the Url_loaderexample. Url_loader Deep Dive Setting up the request

Handlemessage in url_loader.cc creates A urlloaderhandler instance and passes it the URL Of the asset to be retrieved. Then handlemessage calls start to Start retrieving the asset from the server:

void Urlloaderinstance::handlemessage (const pp::var& var_message) {if (!var_message.is_string ()) {return; } std::string message = Var_message.
  Asstring ();
    if (Message.find (kloadurlmethodid) = = 0) {//The argument to GETURL are everything after the first ': '.
    size_t Sep_pos = message.find_first_of (kmessageargumentseparator);
      if (sep_pos! = std::string::npos) {std::string url = message.substr (Sep_pos + 1);
      printf ("Urlloaderinstance::handlemessage ('%s ', '%s ') \ n", Message.c_str (), Url.c_str ());
      Fflush (stdout);
      urlloaderhandler* handler = urlloaderhandler::create (this, URL); if (handler! = NULL) {//starts asynchronous download. When download are finished or when AN//error occurs, |handler|
        Posts the results back to the browser/Vis PostMessage and self-destroys.
      Handler->start (); }
    }
  }
}

Notice the constructor for Urlloaderhandler in url_loader_handler.cc sets up the parameters of the URL request (using SetUrl, Setmethod, and setrecorddownloadprogress):

Urlloaderhandler::urlloaderhandler (pp::instance* Instance,
                                   const std::string& URL)
    : Instance_ ( Instance),
      url_ (URL),
      url_request_ (instance),
      Url_loader_ (instance),
      buffer_ (New Char[read_ Buffer_size]),
      Cc_factory_ (this) {
  url_request_. SetUrl (URL);
  Url_request_. Setmethod ("GET");
  Url_request_. Setrecorddownloadprogress (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.