Home | History | Annotate | Download | only in messaging
      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 CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_
      7 
      8 #include "base/callback_forward.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/platform_file.h"
     11 #include "base/process/process.h"
     12 #include "ui/gfx/native_widget_types.h"
     13 
     14 class CommandLine;
     15 class GURL;
     16 
     17 namespace base {
     18 class FilePath;
     19 }
     20 
     21 namespace extensions {
     22 
     23 class NativeProcessLauncher {
     24  public:
     25   enum LaunchResult {
     26     RESULT_SUCCESS,
     27     RESULT_INVALID_NAME,
     28     RESULT_NOT_FOUND,
     29     RESULT_FORBIDDEN,
     30     RESULT_FAILED_TO_START,
     31   };
     32 
     33   // Callback that's called after the process has been launched. |result| is
     34   // set to false in case of a failure. Handler must take ownership of the IO
     35   // handles.
     36   typedef base::Callback<void (LaunchResult result,
     37                                base::PlatformFile read_file,
     38                                base::PlatformFile write_file)> LaunchedCallback;
     39 
     40   static scoped_ptr<NativeProcessLauncher> CreateDefault(
     41       gfx::NativeView native_view);
     42 
     43   NativeProcessLauncher() {}
     44   virtual ~NativeProcessLauncher() {}
     45 
     46   // Finds native messaging host with the specified name and launches it
     47   // asynchronously. Also checks that the specified |origin| is permitted to
     48   // access the host. |callback| is called after the process has been started.
     49   // If the launcher is destroyed before the callback is called then the call is
     50   // canceled and the process is stopped if it has been started already (by
     51   // closing IO pipes).
     52   virtual void Launch(const GURL& origin,
     53                       const std::string& native_host_name,
     54                       LaunchedCallback callback) const = 0;
     55 
     56  protected:
     57   // The following two methods are platform specific and are implemented in
     58   // platform-specific .cc files.
     59 
     60   // Finds manifest file for the native messaging host |native_host_name|.
     61   static base::FilePath FindManifest(const std::string& native_host_name,
     62                                      std::string* error_message);
     63 
     64   // Launches native messaging process.
     65   static bool LaunchNativeProcess(
     66       const CommandLine& command_line,
     67       base::PlatformFile* read_file,
     68       base::PlatformFile* write_file);
     69 
     70  private:
     71   DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncher);
     72 };
     73 
     74 }  // namespace extensions
     75 
     76 #endif  // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_
     77