Home | History | Annotate | Download | only in plugin
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_
      6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "native_client/src/include/nacl_macros.h"
     12 #include "native_client/src/include/nacl_string.h"
     13 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
     14 
     15 #include "ppapi/c/private/pp_file_handle.h"
     16 #include "ppapi/cpp/completion_callback.h"
     17 
     18 #include "ppapi/native_client/src/trusted/plugin/nexe_arch.h"
     19 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
     20 
     21 namespace plugin {
     22 
     23 class Manifest;
     24 class Plugin;
     25 class PnaclCoordinator;
     26 
     27 // Constants for loading LLC and LD.
     28 class PnaclUrls {
     29  public:
     30   // Get the base URL prefix for Pnacl resources (without platform prefix).
     31   static nacl::string GetBaseUrl();
     32 
     33   // Return {platform_prefix}/url
     34   static nacl::string PrependPlatformPrefix(const nacl::string& url);
     35 
     36   static bool IsPnaclComponent(const nacl::string& full_url);
     37   static nacl::string PnaclComponentURLToFilename(
     38       const nacl::string& full_url);
     39 
     40   // Get the URL for the resource info JSON file that contains information
     41   // about loadable resources.
     42   static const nacl::string GetResourceInfoUrl() {
     43     return nacl::string(kResourceInfoUrl);
     44   }
     45  private:
     46   static const char kResourceInfoUrl[];
     47 };
     48 
     49 // Loads a list of resources, providing a way to get file descriptors for
     50 // these resources.  URLs for resources are resolved by the manifest
     51 // and point to pnacl component filesystem resources.
     52 class PnaclResources {
     53  public:
     54   PnaclResources(Plugin* plugin,
     55                  PnaclCoordinator* coordinator,
     56                  const Manifest* manifest)
     57       : plugin_(plugin),
     58         coordinator_(coordinator),
     59         manifest_(manifest) {
     60   }
     61   virtual ~PnaclResources();
     62 
     63   // Read the resource info JSON file.  This is the first step after
     64   // construction; it has to be completed before StartLoad is called.
     65   virtual void ReadResourceInfo(
     66       const nacl::string& resource_info_url,
     67       const pp::CompletionCallback& resource_info_read_cb);
     68 
     69   // Start loading the resources.
     70   virtual void StartLoad(
     71       const pp::CompletionCallback& all_loaded_callback);
     72 
     73   const nacl::string& GetLlcUrl() {
     74     return llc_tool_name;
     75   }
     76 
     77   const nacl::string& GetLdUrl() {
     78     return ld_tool_name;
     79   }
     80 
     81   // Get file descs by name. Only valid after StartLoad's completion callback
     82   // fired.
     83   nacl::DescWrapper* WrapperForUrl(const nacl::string& url);
     84 
     85   static int32_t GetPnaclFD(Plugin* plugin, const char* filename);
     86 
     87  private:
     88   NACL_DISALLOW_COPY_AND_ASSIGN(PnaclResources);
     89 
     90   // The plugin requesting the resource loading.
     91   Plugin* plugin_;
     92   // The coordinator responsible for reporting errors, etc.
     93   PnaclCoordinator* coordinator_;
     94   // The manifest for looking up resource URLs.
     95   const Manifest* manifest_;
     96   // The descriptor wrappers for the downloaded URLs.  Only valid
     97   // once all_loaded_callback_ has been invoked.
     98   std::map<nacl::string, nacl::DescWrapper*> resource_wrappers_;
     99 
    100   // Tool names for llc and ld; read from the resource info file.
    101   nacl::string llc_tool_name;
    102   nacl::string ld_tool_name;
    103 
    104   // Parses resource info json data in |buf|.  Returns true if successful.
    105   // Otherwise returns false and places an error message in |errmsg|.
    106   bool ParseResourceInfo(const nacl::string& buf, nacl::string& errmsg);
    107 
    108   // Convenience function for reporting an error while reading the resource
    109   // info file.
    110   void ReadResourceInfoError(const nacl::string& msg);
    111 };
    112 
    113 }  // namespace plugin;
    114 #endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_
    115