Search code examples
google-chromegoogle-chrome-extensiongoogle-chrome-appgoogle-nativeclient

Is chrome.fileSystem usable inside google Native Client


Is it possible to use chrome.fileSystem inside NaCl?

Thanks


Solution

  • The chrome.fileSystem API allows you to access the user's local filesystem via a Chrome App. This requires a user to pick a directory to expose to the App.

    This filesystem can be passed to the NaCl module and then used with the standard NaCl pp::FileSystem API.

    There is an example of this in the NaCl SDK at examples/tutorial/filesystem_passing. You can browse the code for it here.

    Here are the important parts: JavaScript:

    chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(entry) {
      if (!entry) {
        // The user cancelled the dialog.
        return;
      }
    
      // Send the filesystem and the directory path to the NaCl module.
      common.naclModule.postMessage({
        filesystem: entry.filesystem,
        fullPath: entry.fullPath
      });
    });
    

    C++:

    // Got a message from JavaScript. We're assuming it is a dictionary with
    // two elements:
    //   {
    //     filesystem: <A Filesystem var>,
    //     fullPath: <A string>
    //   }
    pp::VarDictionary var_dict(var_message);
    pp::Resource filesystem_resource = var_dict.Get("filesystem").AsResource();
    pp::FileSystem filesystem(filesystem_resource);
    std::string full_path = var_dict.Get("fullPath").AsString();
    std::string save_path = full_path + "/hello_from_nacl.txt";
    std::string contents = "Hello, from Native Client!\n";
    

    It's important to note that all paths in this FileSystem must be prefixed with full_path. Any other accesses will fail.