1// Copyright (c) 2008 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#include "chrome/browser/importer/firefox_profile_lock.h"
6
7#include <windows.h>
8
9// This class is based on Firefox code in:
10//   profile/dirserviceprovider/src/nsProfileLock.cpp
11// The license block is:
12
13/* ***** BEGIN LICENSE BLOCK *****
14* Version: MPL 1.1/GPL 2.0/LGPL 2.1
15*
16* The contents of this file are subject to the Mozilla Public License Version
17* 1.1 (the "License"); you may not use this file except in compliance with
18* the License. You may obtain a copy of the License at
19* http://www.mozilla.org/MPL/
20*
21* Software distributed under the License is distributed on an "AS IS" basis,
22* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
23* for the specific language governing rights and limitations under the
24* License.
25*
26* The Original Code is mozilla.org code.
27*
28* The Initial Developer of the Original Code is
29* Netscape Communications Corporation.
30* Portions created by the Initial Developer are Copyright (C) 2002
31* the Initial Developer. All Rights Reserved.
32*
33* Contributor(s):
34*   Conrad Carlen <ccarlen@netscape.com>
35*   Brendan Eich <brendan@mozilla.org>
36*   Colin Blake <colin@theblakes.com>
37*   Javier Pedemonte <pedemont@us.ibm.com>
38*   Mats Palmgren <mats.palmgren@bredband.net>
39*
40* Alternatively, the contents of this file may be used under the terms of
41* either the GNU General Public License Version 2 or later (the "GPL"), or
42* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
43* in which case the provisions of the GPL or the LGPL are applicable instead
44* of those above. If you wish to allow use of your version of this file only
45* under the terms of either the GPL or the LGPL, and not to allow others to
46* use your version of this file under the terms of the MPL, indicate your
47* decision by deleting the provisions above and replace them with the notice
48* and other provisions required by the GPL or the LGPL. If you do not delete
49* the provisions above, a recipient may use your version of this file under
50* the terms of any one of the MPL, the GPL or the LGPL.
51*
52* ***** END LICENSE BLOCK ***** */
53
54void FirefoxProfileLock::Init() {
55  lock_handle_ = INVALID_HANDLE_VALUE;
56}
57
58void FirefoxProfileLock::Lock() {
59  if (HasAcquired())
60    return;
61  lock_handle_ = CreateFile(lock_file_.value().c_str(),
62                            GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
63                            FILE_FLAG_DELETE_ON_CLOSE, NULL);
64}
65
66void FirefoxProfileLock::Unlock() {
67  if (!HasAcquired())
68    return;
69  CloseHandle(lock_handle_);
70  lock_handle_ = INVALID_HANDLE_VALUE;
71}
72
73bool FirefoxProfileLock::HasAcquired() {
74  return (lock_handle_ != INVALID_HANDLE_VALUE);
75}
76