summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_sync_client.h
blob: 0f5111bf6a158712c08016f913a7280c29bd6fec (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
// 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_SYNC_CLIENT_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_
#pragma once

#include <queue>
#include <string>
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop_proxy.h"
#include "chrome/browser/chromeos/gdata/gdata_file_system.h"

namespace gdata {

// The GDataSyncClient is used to synchronize pinned files on gdata and the
// cache on the local drive. The sync client works as follows.
//
// When the user pins files on gdata, this client is notified about the files
// that get pinned, and queues tasks and starts fetching these files in the
// background.
//
// When the user unpins files on gdata, this client is notified about the
// files that get unpinned, cancels tasks if these are still in the queue.
//
// If the user logs out before fetching of the pinned files is complete, this
// client resumes fetching operations next time the user logs in, based on
// the states left in the cache.
//
// TODO(satorux): This client should also upload pinned but dirty (locally
// edited) files to gdata. Will work on this once downloading is done.
// crosbug.com/27836.
//

// The interface class is defined to make GDataSyncClient mockable.
class GDataSyncClientInterface : public GDataFileSystem::Observer {
 public:
  // Initializes the GDataSyncClient. |file_system| is used to access to the
  // cache (ex. store a file to the cache when the file is downloaded).
  virtual void Initialize(GDataFileSystemInterface* file_system) = 0;

  virtual ~GDataSyncClientInterface() {}
};

// The production implementation of GDataSyncClientInterface.
class GDataSyncClient : public GDataSyncClientInterface {
 public:
  GDataSyncClient();
  virtual ~GDataSyncClient();

  // GDataSyncClientInterface overrides.
  virtual void Initialize(GDataFileSystemInterface* file_system) OVERRIDE;

  // GDataFileSystem::Observer overrides.
  virtual void OnCacheInitialized() OVERRIDE;
  virtual void OnFilePinned(const std::string& resource_id,
                            const std::string& md5) OVERRIDE;
  virtual void OnFileUnpinned(const std::string& resource_id,
                              const std::string& md5) OVERRIDE;

  // Starts scanning the pinned directory in the cache to collect
  // pinned-but-not-fetched files.
  //
  // TODO(satorux): This function isn't used yet in the production code.
  // We should get notified about completion of the cache initialization, and
  // call this function.
  void StartInitialScan();

  // Runs all pending operations on the background thread, and blocks until
  // they are complete. Used only for testing.
  void FlushForTesting();

  // Returns the contents of |queue_|. Used only for testing.
  std::vector<std::string> GetResourceIdInQueueForTesting();

 private:
  // Called when the initial scan is complete. Receives the resource IDs of
  // pinned-but-not-fetched files as |resource_ids|.
  void OnInitialScanComplete(std::vector<std::string>* resource_ids);

  GDataFileSystemInterface* file_system_;

  // The queue of resource IDs used to fetch pinned-but-not-fetched files in
  // the background thread.
  std::queue<std::string> queue_;

  base::WeakPtrFactory<GDataSyncClient> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(GDataSyncClient);
};

}  // namespace gdata

#endif  // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_