summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_operation_registry.h
blob: c91424ae7e53d37687457954c83583356be8f8f2 (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
// Copyright (c) 2012 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.

#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_
#pragma once

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/id_map.h"
#include "base/observer_list.h"
#include "base/time.h"

namespace gdata {

// This class tracks all the in-flight GData operation objects and manage their
// lifetime.
class GDataOperationRegistry {
 public:
  GDataOperationRegistry();
  virtual ~GDataOperationRegistry();

  // Unique ID to identify each operation.
  typedef int32 OperationID;

  // Enumeration type for indicating the direction of the operation.
  enum OperationType {
    OPERATION_UPLOAD,
    OPERATION_DOWNLOAD,
    OPERATION_OTHER,
  };

  enum OperationTransferState {
    OPERATION_NOT_STARTED,
    OPERATION_STARTED,
    OPERATION_IN_PROGRESS,
    OPERATION_COMPLETED,
    OPERATION_FAILED,
  };

  static std::string OperationTypeToString(OperationType type);
  static std::string OperationTransferStateToString(
      OperationTransferState state);

  // Structure that packs progress information of each operation.
  struct ProgressStatus {
    ProgressStatus(OperationType type, const FilePath& file_path);
    // For debugging
    std::string ToString() const;

    OperationID operation_id;

    // Type of the operation: upload/download.
    OperationType operation_type;
    // GData path of the file dealt with the current operation.
    FilePath file_path;
    // Current state of the transfer;
    OperationTransferState transfer_state;
    // The time when the operation is initiated.
    base::Time start_time;
    // Current fraction of progress of the operation.
    int64 progress_current;
    // Expected total number of bytes to be transferred in the operation.
    // -1 if no expectation is available (yet).
    int64 progress_total;
  };

  // Observer interface for listening changes in the active set of operations.
  class Observer {
   public:
    // Called when a GData operation started, made some progress, or finished.
    virtual void OnProgressUpdate(const std::vector<ProgressStatus>& list) = 0;
   protected:
    virtual ~Observer() {}
  };

  // Base class for operations that this registry class can maintain.
  // The Operation objects are owned by the registry. In particular, calling
  // NotifyFinish() causes the registry to delete the Operation object itself.
  class Operation {
   public:
    explicit Operation(GDataOperationRegistry* registry);
    Operation(GDataOperationRegistry* registry,
              OperationType type,
              const FilePath& file_path);
    virtual ~Operation();

    // Cancels the ongoing operation. NotifyFinish() is called and the Operation
    // object is deleted once the cancellation is done in DoCancel().
    void Cancel();

    // Retrieves the current progress status of the operation.
    const ProgressStatus& progress_status() const { return progress_status_; }

   protected:
    // Notifies the registry about current status.
    void NotifyStart();
    void NotifyProgress(int64 current, int64 total);
    void NotifyFinish(OperationTransferState status);

   private:
    // Does the cancellation.
    virtual void DoCancel() = 0;

    GDataOperationRegistry* const registry_;
    ProgressStatus progress_status_;
  };

  // Cancels all in-flight operations.
  void CancelAll();

  // Cancels ongoing operation for a given virtual |file_path|. Returns true if
  // the operation was found and canceled.
  bool CancelForFilePath(const FilePath& file_path);

  // Obtains the list of currently active operations.
  std::vector<ProgressStatus> GetProgressStatusList();

  // Sets the observer.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 private:
  // Handlers for notifications from Operations.
  friend class Operation;
  // The registry assigns a fresh operation ID and return it to *id.
  void OnOperationStart(Operation* operation, OperationID* id);
  void OnOperationProgress(OperationID operation);
  void OnOperationFinish(OperationID operation);

  bool IsFileTransferOperation(const Operation* operation) const;

  typedef IDMap<Operation, IDMapOwnPointer> OperationIDMap;
  OperationIDMap in_flight_operations_;
  ObserverList<Observer> observer_list_;

  DISALLOW_COPY_AND_ASSIGN(GDataOperationRegistry);
};

}  // namespace gdata

#endif  // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_