Home | History | Annotate | Download | only in imageburner
      1 // Copyright (c) 2013 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/chromeos/imageburner/burn_device_handler.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/logging.h"
     11 #include "chromeos/dbus/cros_disks_client.h"
     12 #include "chromeos/disks/disk_mount_manager.h"
     13 
     14 namespace chromeos {
     15 namespace imageburner {
     16 
     17 using disks::DiskMountManager;
     18 
     19 namespace {
     20 
     21 // Returns true when |disk| is a device on which we can burn recovery image.
     22 bool IsBurnableDevice(const DiskMountManager::Disk& disk) {
     23   return disk.is_parent() && !disk.on_boot_device() && disk.has_media() &&
     24          (disk.device_type() == DEVICE_TYPE_USB ||
     25           disk.device_type() == DEVICE_TYPE_SD);
     26 }
     27 
     28 }  // namespace
     29 
     30 BurnDeviceHandler::BurnDeviceHandler(DiskMountManager* disk_mount_manager)
     31     : disk_mount_manager_(disk_mount_manager) {
     32   DCHECK(disk_mount_manager_);
     33   disk_mount_manager_->AddObserver(this);
     34 }
     35 
     36 BurnDeviceHandler::~BurnDeviceHandler() {
     37   disk_mount_manager_->RemoveObserver(this);
     38 }
     39 
     40 void BurnDeviceHandler::SetCallbacks(const DiskCallback& add_callback,
     41                                      const DiskCallback& remove_callback) {
     42   add_callback_ = add_callback;
     43   remove_callback_ = remove_callback;
     44 }
     45 
     46 std::vector<DiskMountManager::Disk> BurnDeviceHandler::GetBurnableDevices() {
     47   const DiskMountManager::DiskMap& disks = disk_mount_manager_->disks();
     48   std::vector<DiskMountManager::Disk> result;
     49   for (DiskMountManager::DiskMap::const_iterator iter = disks.begin();
     50        iter != disks.end();
     51        ++iter) {
     52     const DiskMountManager::Disk& disk = *iter->second;
     53     if (IsBurnableDevice(disk))
     54       result.push_back(disk);
     55   }
     56   return result;
     57 }
     58 
     59 void BurnDeviceHandler::OnDiskEvent(DiskMountManager::DiskEvent event,
     60                                     const DiskMountManager::Disk* disk) {
     61   // We are only interested in burnable devices.
     62   if (!IsBurnableDevice(*disk))
     63     return;
     64 
     65   switch (event) {
     66     case DiskMountManager::DISK_ADDED:
     67       add_callback_.Run(*disk);
     68       break;
     69     case DiskMountManager::DISK_REMOVED:
     70       remove_callback_.Run(*disk);
     71       break;
     72     default: {
     73       // Do nothing.
     74     }
     75   }
     76 }
     77 
     78 void BurnDeviceHandler::OnDeviceEvent(DiskMountManager::DeviceEvent event,
     79                                       const std::string& device_path) {
     80   // Do nothing.
     81 }
     82 
     83 void BurnDeviceHandler::OnMountEvent(
     84     DiskMountManager::MountEvent event,
     85     MountError error_code,
     86     const DiskMountManager::MountPointInfo& mount_info) {
     87   // Do nothing.
     88 }
     89 
     90 void BurnDeviceHandler::OnFormatEvent(DiskMountManager::FormatEvent event,
     91                                       FormatError error_code,
     92                                       const std::string& device_path) {
     93   // Do nothing.
     94 }
     95 
     96 }  // namespace imageburner
     97 }  // namespace chromeos
     98