Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2011 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_COMMON_LAUNCHD_MAC_H_
      6 #define CHROME_COMMON_LAUNCHD_MAC_H_
      7 
      8 #include <CoreFoundation/CoreFoundation.h>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/singleton.h"
     12 
     13 class Launchd {
     14  public:
     15   enum Type {
     16     Agent,  // LaunchAgent
     17     Daemon  // LaunchDaemon
     18   };
     19 
     20   // Domains map to NSSearchPathDomainMask so Foundation does not need to be
     21   // included.
     22   enum Domain {
     23     User = 1,  // ~/Library/Launch*
     24     Local = 2,  // /Library/Launch*
     25     Network = 4,  // /Network/Library/Launch*
     26     System = 8  // /System/Library/Launch*
     27   };
     28 
     29   // TODO(dmaclach): Get rid of this pseudo singleton, and inject it
     30   // appropriately wherever it is used.
     31   // http://crbug.com/76925
     32   static Launchd* GetInstance();
     33 
     34   virtual ~Launchd();
     35 
     36   // Return a dictionary with the launchd export settings.
     37   virtual CFDictionaryRef CopyExports();
     38 
     39   // Return a dictionary with the launchd entries for job labeled |name|.
     40   virtual CFDictionaryRef CopyJobDictionary(CFStringRef label);
     41 
     42   // Return a dictionary for launchd process.
     43   virtual CFDictionaryRef CopyDictionaryByCheckingIn(CFErrorRef* error);
     44 
     45   // Remove a launchd process from launchd.
     46   virtual bool RemoveJob(CFStringRef label, CFErrorRef* error);
     47 
     48   // Used by a process controlled by launchd to restart itself.
     49   // |session_type| can be "Aqua", "LoginWindow", "Background", "StandardIO" or
     50   // "System".
     51   // RestartLaunchdJob starts up a separate process to tell launchd to
     52   // send this process a SIGTERM. This call will return, but a SIGTERM will be
     53   // received shortly.
     54   virtual bool RestartJob(Domain domain,
     55                           Type type,
     56                           CFStringRef name,
     57                           CFStringRef session_type);
     58 
     59   // Read a launchd plist from disk.
     60   // |name| should not have an extension.
     61   virtual CFMutableDictionaryRef CreatePlistFromFile(Domain domain,
     62                                                      Type type,
     63                                                      CFStringRef name);
     64   // Write a launchd plist to disk.
     65   // |name| should not have an extension.
     66   virtual bool WritePlistToFile(Domain domain,
     67                                 Type type,
     68                                 CFStringRef name,
     69                                 CFDictionaryRef dict);
     70 
     71   // Delete a launchd plist.
     72   // |name| should not have an extension.
     73   virtual bool DeletePlist(Domain domain, Type type, CFStringRef name);
     74 
     75   // TODO(dmaclach): remove this once http://crbug.com/76925 is fixed.
     76   // Scaffolding for doing unittests with our singleton.
     77   static void SetInstance(Launchd* instance);
     78   class ScopedInstance {
     79    public:
     80     explicit ScopedInstance(Launchd* instance) {
     81       Launchd::SetInstance(instance);
     82     }
     83     ~ScopedInstance() {
     84       Launchd::SetInstance(NULL);
     85     }
     86   };
     87 
     88  protected:
     89   Launchd() { }
     90 
     91  private:
     92   // TODO(dmaclach): remove this once http://crbug.com/76925 is fixed.
     93   // Scaffolding for doing unittests with our singleton.
     94   friend struct DefaultSingletonTraits<Launchd>;
     95   static Launchd* g_instance_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(Launchd);
     98 };
     99 
    100 #endif  // CHROME_COMMON_LAUNCHD_MAC_H_
    101