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