Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2010 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_IMPORTER_FIREFOX_PROFILE_LOCK_H__
      6 #define CHROME_BROWSER_IMPORTER_FIREFOX_PROFILE_LOCK_H__
      7 
      8 #include "build/build_config.h"
      9 
     10 #if defined(OS_WIN)
     11 #include <windows.h>
     12 #endif
     13 
     14 #include "base/basictypes.h"
     15 #include "base/files/file_path.h"
     16 #include "base/gtest_prod_util.h"
     17 
     18 // Firefox is designed to allow only one application to access its
     19 // profile at the same time.
     20 // Reference:
     21 //   http://kb.mozillazine.org/Profile_in_use
     22 //
     23 // This class is based on Firefox code in:
     24 //   profile/dirserviceprovider/src/nsProfileLock.cpp
     25 // The license block is:
     26 
     27 /* ***** BEGIN LICENSE BLOCK *****
     28 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     29 *
     30 * The contents of this file are subject to the Mozilla Public License Version
     31 * 1.1 (the "License"); you may not use this file except in compliance with
     32 * the License. You may obtain a copy of the License at
     33 * http://www.mozilla.org/MPL/
     34 *
     35 * Software distributed under the License is distributed on an "AS IS" basis,
     36 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     37 * for the specific language governing rights and limitations under the
     38 * License.
     39 *
     40 * The Original Code is mozilla.org code.
     41 *
     42 * The Initial Developer of the Original Code is
     43 * Netscape Communications Corporation.
     44 * Portions created by the Initial Developer are Copyright (C) 2002
     45 * the Initial Developer. All Rights Reserved.
     46 *
     47 * Contributor(s):
     48 *   Conrad Carlen <ccarlen (at) netscape.com>
     49 *   Brendan Eich <brendan (at) mozilla.org>
     50 *   Colin Blake <colin (at) theblakes.com>
     51 *   Javier Pedemonte <pedemont (at) us.ibm.com>
     52 *   Mats Palmgren <mats.palmgren (at) bredband.net>
     53 *
     54 * Alternatively, the contents of this file may be used under the terms of
     55 * either the GNU General Public License Version 2 or later (the "GPL"), or
     56 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     57 * in which case the provisions of the GPL or the LGPL are applicable instead
     58 * of those above. If you wish to allow use of your version of this file only
     59 * under the terms of either the GPL or the LGPL, and not to allow others to
     60 * use your version of this file under the terms of the MPL, indicate your
     61 * decision by deleting the provisions above and replace them with the notice
     62 * and other provisions required by the GPL or the LGPL. If you do not delete
     63 * the provisions above, a recipient may use your version of this file under
     64 * the terms of any one of the MPL, the GPL or the LGPL.
     65 *
     66 * ***** END LICENSE BLOCK ***** */
     67 
     68 class FirefoxProfileLock {
     69  public:
     70   explicit FirefoxProfileLock(const base::FilePath& path);
     71   ~FirefoxProfileLock();
     72 
     73   // Locks and releases the profile.
     74   void Lock();
     75   void Unlock();
     76 
     77   // Returns true if we lock the profile successfully.
     78   bool HasAcquired();
     79 
     80  private:
     81   FRIEND_TEST_ALL_PREFIXES(FirefoxProfileLockTest, ProfileLock);
     82   FRIEND_TEST_ALL_PREFIXES(FirefoxProfileLockTest, ProfileLockOrphaned);
     83 
     84   static const base::FilePath::CharType* kLockFileName;
     85   static const base::FilePath::CharType* kOldLockFileName;
     86 
     87   void Init();
     88 
     89   // Full path of the lock file in the profile folder.
     90   base::FilePath lock_file_;
     91 
     92   // The handle of the lock file.
     93 #if defined(OS_WIN)
     94   HANDLE lock_handle_;
     95 #elif defined(OS_POSIX)
     96   int lock_fd_;
     97 
     98   // On Posix systems Firefox apparently first tries to put a fcntl lock
     99   // on a file and if that fails, it does a regular exculsive open on another
    100   // file. This variable contains the location of this other file.
    101   base::FilePath old_lock_file_;
    102 
    103   // Method that tries to put a fcntl lock on file specified by |lock_file_|.
    104   // Returns false if lock is already held by another process. true in all
    105   // other cases.
    106   bool LockWithFcntl();
    107 #endif
    108 
    109   DISALLOW_COPY_AND_ASSIGN(FirefoxProfileLock);
    110 };
    111 
    112 #endif  // CHROME_BROWSER_IMPORTER_FIREFOX_PROFILE_LOCK_H__
    113