summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/burn_library.cc
blob: c5e833231c1b3794f0931ee55b92f2f338ba851f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/cros/burn_library.h"

#include <cstring>

#include "base/bind.h"
#include "base/memory/linked_ptr.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/disks/disk_mount_manager.h"
#include "chrome/common/zip.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace chromeos {

class BurnLibraryImpl : public BurnLibrary,
                        public base::SupportsWeakPtr<BurnLibraryImpl> {
 public:
  BurnLibraryImpl();
  virtual ~BurnLibraryImpl();

  // BurnLibrary implementation.
  virtual void AddObserver(Observer* observer) OVERRIDE;
  virtual void RemoveObserver(Observer* observer) OVERRIDE;
  virtual void DoBurn(const FilePath& source_path,
                      const std::string& image_name,
                      const FilePath& target_file_path,
                      const FilePath& target_device_path) OVERRIDE;
  virtual void CancelBurnImage() OVERRIDE;

  // Called by BurnLibraryTaskProxy.
  void UnzipImage();
  void OnImageUnzipped();

 private:
  void Init();
  void BurnImage();
  static void DevicesUnmountedCallback(void* object, bool success);
  void UpdateBurnStatus(const ImageBurnStatus& status, BurnEvent evt);
  static void BurnStatusChangedHandler(void* object,
                                       const BurnStatus& status,
                                       BurnEventType evt);

 private:
  ObserverList<BurnLibrary::Observer> observers_;
  BurnStatusConnection burn_status_connection_;

  FilePath source_zip_file_;
  std::string source_image_file_;
  std::string source_image_name_;
  std::string target_file_path_;
  std::string target_device_path_;

  bool unzipping_;
  bool cancelled_;
  bool burning_;
  bool block_burn_signals_;

  DISALLOW_COPY_AND_ASSIGN(BurnLibraryImpl);
};

class BurnLibraryTaskProxy
    : public base::RefCountedThreadSafe<BurnLibraryTaskProxy> {
 public:
  explicit BurnLibraryTaskProxy(const base::WeakPtr<BurnLibraryImpl>& library)
      : library_(library) {
    library_->DetachFromThread();
  }

  void UnzipImage() {
    if (library_)
      library_->UnzipImage();
  }

  void ImageUnzipped() {
    if (library_)
      library_->OnImageUnzipped();
  }

 private:
  base::WeakPtr<BurnLibraryImpl> library_;

  friend class base::RefCountedThreadSafe<BurnLibraryTaskProxy>;

  DISALLOW_COPY_AND_ASSIGN(BurnLibraryTaskProxy);
};

BurnLibraryImpl::BurnLibraryImpl() : unzipping_(false),
                                     cancelled_(false),
                                     burning_(false),
                                     block_burn_signals_(false) {
}

BurnLibraryImpl::~BurnLibraryImpl() {
  if (burn_status_connection_) {
    DisconnectBurnStatus(burn_status_connection_);
  }
}

void BurnLibraryImpl::Init() {
  DCHECK(CrosLibrary::Get()->libcros_loaded());
  burn_status_connection_ =
      chromeos::MonitorBurnStatus(&BurnStatusChangedHandler, this);
}

void BurnLibraryImpl::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
  if (unzipping_ && !cancelled_)
    UpdateBurnStatus(ImageBurnStatus(), UNZIP_STARTED);
}

void BurnLibraryImpl::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void BurnLibraryImpl::CancelBurnImage() {
  // At the moment, we cannot really stop uzipping or burning. Instead we
  // prevent events from being send to listeners.
  if (burning_)
    block_burn_signals_ = true;
  cancelled_ = true;
}

void BurnLibraryImpl::DoBurn(const FilePath& source_path,
    const std::string& image_name,
    const FilePath& target_file_path,
    const FilePath& target_device_path) {
  if (unzipping_) {
    // We have unzip in progress, maybe it was "cancelled" before and did not
    // finish yet. In that case, let's pretend cancel did not happen.
    cancelled_ = false;
    UpdateBurnStatus(ImageBurnStatus(), UNZIP_STARTED);
    return;
  }

  source_zip_file_ = source_path;
  source_image_file_.clear();
  source_image_name_ = image_name;
  target_file_path_ = target_file_path.value();
  target_device_path_ = target_device_path.value();

  unzipping_ = true;
  cancelled_ = false;
  UpdateBurnStatus(ImageBurnStatus(), UNZIP_STARTED);

  scoped_refptr<BurnLibraryTaskProxy> task =
      new BurnLibraryTaskProxy(AsWeakPtr());
  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
                          base::Bind(&BurnLibraryTaskProxy::UnzipImage,
                                     task));
}

void BurnLibraryImpl::UnzipImage() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  if (zip::Unzip(source_zip_file_, source_zip_file_.DirName())) {
    source_image_file_ =
        source_zip_file_.DirName().Append(source_image_name_).value();
  }

  scoped_refptr<BurnLibraryTaskProxy> task =
      new BurnLibraryTaskProxy(AsWeakPtr());
  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                          base::Bind(&BurnLibraryTaskProxy::ImageUnzipped,
                                     task));
}

void BurnLibraryImpl::OnImageUnzipped() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  bool success = !source_image_file_.empty();
  UpdateBurnStatus(ImageBurnStatus(), (success ? UNZIP_COMPLETE : UNZIP_FAIL));

  unzipping_ = false;
  if (cancelled_) {
    cancelled_ = false;
    return;
  }

  if (!success)
    return;

  burning_ = true;

  chromeos::disks::DiskMountManager::GetInstance()->
      UnmountDeviceRecursive(target_device_path_,
                             &BurnLibraryImpl::DevicesUnmountedCallback,
                             this);
}

void BurnLibraryImpl::DevicesUnmountedCallback(void* object, bool success) {
  DCHECK(object);
  BurnLibraryImpl* self = static_cast<BurnLibraryImpl*>(object);
  if (success) {
    self->BurnImage();
  } else {
    self->UpdateBurnStatus(ImageBurnStatus(self->target_file_path_.c_str(),
        0, 0, "Error unmounting device"), BURN_FAIL);
  }
}

void BurnLibraryImpl::BurnImage() {
  RequestBurn(source_image_file_.c_str(), target_file_path_.c_str(),
      &BurnLibraryImpl::BurnStatusChangedHandler, this);
}

void BurnLibraryImpl::BurnStatusChangedHandler(void* object,
    const BurnStatus& status, BurnEventType evt) {
  DCHECK(object);
  BurnLibraryImpl* self = static_cast<BurnLibraryImpl*>(object);

// Copy burn status because it will be freed after returning from this method.
  ImageBurnStatus status_copy(status);
  BurnEvent event = UNKNOWN;
  switch (evt) {
    case(BURN_CANCELED):
      event = BURN_FAIL;
      break;
    case(BURN_COMPLETE):
      event = BURN_SUCCESS;
      break;
    case(BURN_UPDATED):
    case(BURN_STARTED):
      event = BURN_UPDATE;
      break;
  }
  self->UpdateBurnStatus(status_copy, event);
}

void BurnLibraryImpl::UpdateBurnStatus(const ImageBurnStatus& status,
                                       BurnEvent evt) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (cancelled_)
    return;

  if (evt == BURN_FAIL || evt == BURN_SUCCESS) {
    burning_ = false;
    if (block_burn_signals_) {
      block_burn_signals_ = false;
      return;
    }
  }

  if (block_burn_signals_ && evt == BURN_UPDATE)
    return;

  FOR_EACH_OBSERVER(Observer, observers_,
      BurnProgressUpdated(this, evt, status));
}

class BurnLibraryStubImpl : public BurnLibrary {
 public:
  BurnLibraryStubImpl() {}
  virtual ~BurnLibraryStubImpl() {}

  // BurnLibrary overrides.
  virtual void Init() OVERRIDE {}
  virtual void AddObserver(Observer* observer) OVERRIDE {}
  virtual void RemoveObserver(Observer* observer) OVERRIDE {}
  virtual void DoBurn(const FilePath& source_path,
                      const std::string& image_name,
                      const FilePath& target_file_path,
                      const FilePath& target_device_path) OVERRIDE {
  }
  virtual void CancelBurnImage() OVERRIDE {}

  DISALLOW_COPY_AND_ASSIGN(BurnLibraryStubImpl);
};

// static
BurnLibrary* BurnLibrary::GetImpl(bool stub) {
  BurnLibrary* impl;
  if (stub)
    impl = new BurnLibraryStubImpl();
  else
    impl = new BurnLibraryImpl();
  impl->Init();
  return impl;
}

}  // namespace chromeos