Home | History | Annotate | Download | only in plugin
      1 // Copyright (c) 2013 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 #include "ppapi/native_client/src/trusted/plugin/pnacl_options.h"
      6 
      7 #include <iterator>
      8 #include <vector>
      9 
     10 #include "native_client/src/include/nacl_string.h"
     11 
     12 namespace {
     13 
     14 nacl::string ReplaceBadFSChars(nacl::string str,
     15                                const nacl::string& bad_chars,
     16                                const nacl::string& replacement) {
     17   size_t replace_pos = str.find_first_of(bad_chars);
     18   while (replace_pos != nacl::string::npos) {
     19     str = str.replace(replace_pos, 1, replacement);
     20     replace_pos = str.find_first_of(bad_chars);
     21   }
     22   return str;
     23 }
     24 
     25 }  // namespace
     26 
     27 namespace plugin {
     28 
     29 PnaclOptions::PnaclOptions() : translate_(false), opt_level_(2) { }
     30 
     31 PnaclOptions::~PnaclOptions() {
     32 }
     33 
     34 nacl::string PnaclOptions::GetCacheKey() const {
     35   // TODO(jvoung): We need to read the PNaCl translator's manifest
     36   // to grab the NaCl / PNaCl ABI version too.
     37   nacl::stringstream ss;
     38   // Cast opt_level_ as int so that it doesn't think it's a char.
     39   ss << "-O:" << static_cast<int>(opt_level_)
     40      <<  ";cache_validators:" << cache_validators_;
     41   // HTML5 FileSystem-based cache does not allow some characters which
     42   // may appear in URLs, ETags, or Last-Modified times.  Once we move to
     43   // our own cache-backend, it will be more tolerant of various cache
     44   // key values.
     45   // See: http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#naming-restrictions
     46   nacl::string key = ss.str();
     47   key = ReplaceBadFSChars(key, "/", "_FWDSLASH_");
     48   key = ReplaceBadFSChars(key, "\\", "_BCKSLASH_");
     49   key = ReplaceBadFSChars(key, "\0", "_NULL_");
     50   return key;
     51 }
     52 
     53 void PnaclOptions::set_opt_level(int32_t l) {
     54   if (l <= 0) {
     55     opt_level_ = 0;
     56     return;
     57   }
     58   // Currently only allow 0 or 2, since that is what we test.
     59   opt_level_ = 2;
     60 }
     61 
     62 std::vector<char> PnaclOptions::GetOptCommandline() const {
     63   std::vector<char> result;
     64   nacl::string str;
     65 
     66   nacl::stringstream ss;
     67   ss << "-O" << opt_level_;
     68   str = ss.str();
     69 
     70   std::copy(str.begin(), str.end(), std::back_inserter(result));
     71   result.push_back('\x00');
     72 
     73   return result;
     74 }
     75 
     76 }  // namespace plugin
     77