Home | History | Annotate | Download | only in renderer
      1 // Copyright 2014 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 COMPONENTS_NACL_RENDERER_NEXE_LOAD_MANAGER_H_
      6 #define COMPONENTS_NACL_RENDERER_NEXE_LOAD_MANAGER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/files/file.h"
     12 #include "base/macros.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/time/time.h"
     16 #include "ppapi/c/private/ppb_nacl_private.h"
     17 #include "url/gurl.h"
     18 
     19 namespace content {
     20 class PepperPluginInstance;
     21 }
     22 
     23 namespace nacl {
     24 
     25 class ManifestServiceChannel;
     26 class TrustedPluginChannel;
     27 
     28 // NexeLoadManager provides methods for reporting the progress of loading a
     29 // nexe.
     30 class NexeLoadManager {
     31  public:
     32   explicit NexeLoadManager(PP_Instance instance);
     33   ~NexeLoadManager();
     34 
     35   void NexeFileDidOpen(int32_t pp_error,
     36                        const base::File& file,
     37                        int32_t http_status,
     38                        int64_t nexe_bytes_read,
     39                        const std::string& url,
     40                        base::TimeDelta time_since_open);
     41   void ReportLoadSuccess(const std::string& url,
     42                          uint64_t loaded_bytes,
     43                          uint64_t total_bytes);
     44   void ReportLoadError(PP_NaClError error,
     45                        const std::string& error_message);
     46 
     47   // console_message is a part of the error that is logged to
     48   // the JavaScript console but is not reported to JavaScript via
     49   // the lastError property.  This is used to report internal errors which
     50   // may easily change in new versions of the browser and we don't want apps
     51   // to come to depend on the details of these errors.
     52   void ReportLoadError(PP_NaClError error,
     53                        const std::string& error_message,
     54                        const std::string& console_message);
     55   void ReportLoadAbort();
     56   void NexeDidCrash(const char* crash_log);
     57 
     58   // TODO(dmichael): Everything below this comment should eventually be made
     59   // private, when ppb_nacl_private_impl.cc is no longer using them directly.
     60   // The intent is for this class to only expose functions for reporting a
     61   // load state transition (e.g., ReportLoadError, ReportProgress,
     62   // ReportLoadAbort, etc.)
     63   void set_trusted_plugin_channel(scoped_ptr<TrustedPluginChannel> channel);
     64   void set_manifest_service_channel(
     65       scoped_ptr<ManifestServiceChannel> channel);
     66 
     67   PP_NaClReadyState nacl_ready_state();
     68   void set_nacl_ready_state(PP_NaClReadyState ready_state);
     69 
     70   void SetReadOnlyProperty(PP_Var key, PP_Var value);
     71   void SetLastError(const std::string& error);
     72   void LogToConsole(const std::string& message);
     73 
     74   bool is_installed() const { return is_installed_; }
     75 
     76   int32_t exit_status() const { return exit_status_; }
     77   void set_exit_status(int32_t exit_status);
     78 
     79   void InitializePlugin(uint32_t argc, const char* argn[], const char* argv[]);
     80 
     81   void ReportStartupOverhead() const;
     82 
     83   int64_t nexe_size() const { return nexe_size_; }
     84 
     85   bool RequestNaClManifest(const std::string& url);
     86   void ProcessNaClManifest(const std::string& program_url);
     87 
     88   // URL resolution support.
     89   // plugin_base_url is the URL used for resolving relative URLs used in
     90   // src="...".
     91   const GURL& plugin_base_url() const { return plugin_base_url_; }
     92 
     93   // manifest_base_url is the URL used for resolving relative URLs mentioned
     94   // in manifest files.  If the manifest is a data URI, this is an empty string
     95   const GURL& manifest_base_url() const { return manifest_base_url_; }
     96 
     97   // Returns the manifest URL passed as an argument for this plugin instance.
     98   std::string GetManifestURLArgument() const;
     99 
    100   // Returns true if the MIME type for this plugin matches the type for PNaCl,
    101   // false otherwise.
    102   bool IsPNaCl() const;
    103 
    104   // Returns true if dev interfaces are enabled for this plugin.
    105   bool DevInterfacesEnabled() const;
    106 
    107  private:
    108   DISALLOW_COPY_AND_ASSIGN(NexeLoadManager);
    109 
    110   void ReportDeadNexe();
    111 
    112   // Copies a crash log to the console, one line at a time.
    113   void CopyCrashLogToJsConsole(const std::string& crash_log);
    114 
    115   PP_Instance pp_instance_;
    116   PP_NaClReadyState nacl_ready_state_;
    117   bool nexe_error_reported_;
    118 
    119   // A flag indicating if the NaCl executable is being loaded from an installed
    120   // application.  This flag is used to bucket UMA statistics more precisely to
    121   // help determine whether nexe loading problems are caused by networking
    122   // issues.  (Installed applications will be loaded from disk.)
    123   // Unfortunately, the definition of what it means to be part of an installed
    124   // application is a little murky - for example an installed application can
    125   // register a mime handler that loads NaCl executables into an arbitrary web
    126   // page.  As such, the flag actually means "our best guess, based on the URLs
    127   // for NaCl resources that we have seen so far".
    128   bool is_installed_;
    129 
    130   // Time of a successful nexe load.
    131   base::Time ready_time_;
    132 
    133   // Time of plugin initialization.
    134   base::Time init_time_;
    135 
    136   // Time of the start of loading a NaCl module.
    137   base::Time load_start_;
    138 
    139   // The exit status of the plugin process.
    140   // This will have a value in the range (0x00-0xff) if the exit status is set,
    141   // or -1 if set_exit_status() has never been called.
    142   int32_t exit_status_;
    143 
    144   // Size of the downloaded nexe, in bytes.
    145   int64_t nexe_size_;
    146 
    147   // Non-owning.
    148   content::PepperPluginInstance* plugin_instance_;
    149 
    150   // The URL for the document corresponding to this plugin instance.
    151   GURL plugin_base_url_;
    152 
    153   GURL manifest_base_url_;
    154 
    155   // Arguments passed to this plugin instance from the DOM.
    156   std::map<std::string, std::string> args_;
    157 
    158   // We store mime_type_ outside of args_ explicitly because we change it to be
    159   // lowercase.
    160   std::string mime_type_;
    161 
    162   scoped_ptr<TrustedPluginChannel> trusted_plugin_channel_;
    163   scoped_ptr<ManifestServiceChannel> manifest_service_channel_;
    164   base::WeakPtrFactory<NexeLoadManager> weak_factory_;
    165 };
    166 
    167 }  // namespace nacl
    168 
    169 #endif  // COMPONENTS_NACL_RENDERER_NEXE_LOAD_MANAGER_H_
    170