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#ifndef CHROME_BROWSER_HISTORY_DELETE_DIRECTIVE_HANDLER_H_
6#define CHROME_BROWSER_HISTORY_DELETE_DIRECTIVE_HANDLER_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/memory/weak_ptr.h"
10#include "base/threading/thread_checker.h"
11#include "chrome/browser/common/cancelable_request.h"
12#include "sync/api/sync_change_processor.h"
13#include "sync/api/sync_data.h"
14
15namespace sync_pb {
16class HistoryDeleteDirectiveSpecifics;
17}
18
19class HistoryService;
20
21namespace history {
22
23// DeleteDirectiveHandler sends delete directives created locally to sync
24// engine to propagate to other clients. It also expires local history entries
25// according to given delete directives from server.
26class DeleteDirectiveHandler {
27 public:
28  DeleteDirectiveHandler();
29  ~DeleteDirectiveHandler();
30
31  // Start/stop processing delete directives when sync is enabled/disabled.
32  void Start(HistoryService* history_service,
33             const syncer::SyncDataList& initial_sync_data,
34             scoped_ptr<syncer::SyncChangeProcessor> sync_processor);
35  void Stop();
36
37  // Create delete directives for the deletion of visits identified by
38  // |global_ids| (which may be empty), in the time range specified by
39  // |begin_time| and |end_time|.
40  bool CreateDeleteDirectives(
41      const std::set<int64>& global_ids,
42      base::Time begin_time,
43      base::Time end_time);
44
45  // Sends the given |delete_directive| to SyncChangeProcessor (if it exists).
46  // Returns any error resulting from sending the delete directive to sync.
47  // NOTE: the given |delete_directive| is not processed to remove local
48  //       history entries that match. Caller still needs to call other
49  //       interfaces, e.g. HistoryService::ExpireHistoryBetween(), to delete
50  //       local history entries.
51  syncer::SyncError ProcessLocalDeleteDirective(
52      const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive);
53
54  // Expires local history entries according to delete directives from server.
55  syncer::SyncError ProcessSyncChanges(
56      HistoryService* history_service,
57      const syncer::SyncChangeList& change_list);
58
59 private:
60  class DeleteDirectiveTask;
61  friend class DeleteDirectiveTask;
62
63  // Action to take on processed delete directives.
64  enum PostProcessingAction {
65    KEEP_AFTER_PROCESSING,
66    DROP_AFTER_PROCESSING
67  };
68
69  // Callback when history backend finishes deleting visits according to
70  // |delete_directives|.
71  void FinishProcessing(PostProcessingAction post_processing_action,
72                        const syncer::SyncDataList& delete_directives);
73
74  CancelableRequestConsumer internal_consumer_;
75  scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
76  base::ThreadChecker thread_checker_;
77  base::WeakPtrFactory<DeleteDirectiveHandler> weak_ptr_factory_;
78
79  DISALLOW_COPY_AND_ASSIGN(DeleteDirectiveHandler);
80};
81
82}  // namespace history
83
84#endif  // CHROME_BROWSER_HISTORY_DELETE_DIRECTIVE_HANDLER_H_
85