15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_operation.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace extensions {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace image_writer {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Number of bytes for the maximum partition table size.  GUID partition tables
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// reside in the second sector of the disk.  Disks can have up to 4k sectors.
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// See http://crbug.com/328246 for more information.
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const int kPartitionTableSize = 2 * 4096;
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)DestroyPartitionsOperation::DestroyPartitionsOperation(
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::WeakPtr<OperationManager> manager,
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ExtensionId& extension_id,
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& storage_unit_id)
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : Operation(manager, extension_id, storage_unit_id) {}
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)DestroyPartitionsOperation::~DestroyPartitionsOperation() {}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DestroyPartitionsOperation::StartImpl() {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_)) {
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Error(error::kTempFileError);
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<char[]> buffer(new char[kPartitionTableSize]);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  memset(buffer.get(), 0, kPartitionTableSize);
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (base::WriteFile(image_path_, buffer.get(), kPartitionTableSize) !=
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      kPartitionTableSize) {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Error(error::kTempFileError);
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::BrowserThread::PostTask(
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::BrowserThread::FILE,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      FROM_HERE,
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(&DestroyPartitionsOperation::Write,
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 this,
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 base::Bind(&DestroyPartitionsOperation::Finish, this)));
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace image_writer
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace extensions
51