Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / importer / firefox_profile_lock.h
blobff0d53c4f4189f82134431fa6ca8dfc709af9d89
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.
5 #ifndef CHROME_BROWSER_IMPORTER_FIREFOX_PROFILE_LOCK_H__
6 #define CHROME_BROWSER_IMPORTER_FIREFOX_PROFILE_LOCK_H__
8 #include "build/build_config.h"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #endif
14 #include "base/basictypes.h"
15 #include "base/files/file_path.h"
16 #include "base/gtest_prod_util.h"
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
23 // This class is based on Firefox code in:
24 // profile/dirserviceprovider/src/nsProfileLock.cpp
25 // The license block is:
27 /* ***** BEGIN LICENSE BLOCK *****
28 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
40 * The Original Code is mozilla.org code.
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.
47 * Contributor(s):
48 * Conrad Carlen <ccarlen@netscape.com>
49 * Brendan Eich <brendan@mozilla.org>
50 * Colin Blake <colin@theblakes.com>
51 * Javier Pedemonte <pedemont@us.ibm.com>
52 * Mats Palmgren <mats.palmgren@bredband.net>
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.
66 * ***** END LICENSE BLOCK ***** */
68 class FirefoxProfileLock {
69 public:
70 explicit FirefoxProfileLock(const base::FilePath& path);
71 ~FirefoxProfileLock();
73 // Locks and releases the profile.
74 void Lock();
75 void Unlock();
77 // Returns true if we lock the profile successfully.
78 bool HasAcquired();
80 private:
81 FRIEND_TEST_ALL_PREFIXES(FirefoxProfileLockTest, ProfileLock);
82 FRIEND_TEST_ALL_PREFIXES(FirefoxProfileLockTest, ProfileLockOrphaned);
84 static const base::FilePath::CharType* kLockFileName;
85 static const base::FilePath::CharType* kOldLockFileName;
87 void Init();
89 // Full path of the lock file in the profile folder.
90 base::FilePath lock_file_;
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_;
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_;
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
109 DISALLOW_COPY_AND_ASSIGN(FirefoxProfileLock);
112 #endif // CHROME_BROWSER_IMPORTER_FIREFOX_PROFILE_LOCK_H__