15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2008 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/importer/firefox_profile_lock.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is based on Firefox code in:
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   profile/dirserviceprovider/src/nsProfileLock.cpp
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The license block is:
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* ***** BEGIN LICENSE BLOCK *****
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Version: MPL 1.1/GPL 2.0/LGPL 2.1
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The contents of this file are subject to the Mozilla Public License Version
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* 1.1 (the "License"); you may not use this file except in compliance with
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the License. You may obtain a copy of the License at
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* http://www.mozilla.org/MPL/
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Software distributed under the License is distributed on an "AS IS" basis,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* for the specific language governing rights and limitations under the
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* License.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The Original Code is mozilla.org code.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The Initial Developer of the Original Code is
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Netscape Communications Corporation.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Portions created by the Initial Developer are Copyright (C) 2002
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the Initial Developer. All Rights Reserved.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Contributor(s):
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*   Conrad Carlen <ccarlen@netscape.com>
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*   Brendan Eich <brendan@mozilla.org>
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*   Colin Blake <colin@theblakes.com>
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*   Javier Pedemonte <pedemont@us.ibm.com>
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*   Mats Palmgren <mats.palmgren@bredband.net>
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Alternatively, the contents of this file may be used under the terms of
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* either the GNU General Public License Version 2 or later (the "GPL"), or
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* in which case the provisions of the GPL or the LGPL are applicable instead
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* of those above. If you wish to allow use of your version of this file only
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* under the terms of either the GPL or the LGPL, and not to allow others to
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* use your version of this file under the terms of the MPL, indicate your
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* decision by deleting the provisions above and replace them with the notice
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* and other provisions required by the GPL or the LGPL. If you do not delete
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the provisions above, a recipient may use your version of this file under
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the terms of any one of the MPL, the GPL or the LGPL.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* ***** END LICENSE BLOCK ***** */
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FirefoxProfileLock::Init() {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lock_handle_ = INVALID_HANDLE_VALUE;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FirefoxProfileLock::Lock() {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (HasAcquired())
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lock_handle_ = CreateFile(lock_file_.value().c_str(),
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            FILE_FLAG_DELETE_ON_CLOSE, NULL);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FirefoxProfileLock::Unlock() {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!HasAcquired())
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CloseHandle(lock_handle_);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lock_handle_ = INVALID_HANDLE_VALUE;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool FirefoxProfileLock::HasAcquired() {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (lock_handle_ != INVALID_HANDLE_VALUE);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
76