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