1// Copyright (c) 2010 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/sync/engine/cleanup_disabled_types_command.h"
6
7#include "chrome/browser/sync/sessions/sync_session.h"
8#include "chrome/browser/sync/sessions/sync_session_context.h"
9#include "chrome/browser/sync/syncable/directory_manager.h"
10#include "chrome/browser/sync/syncable/model_type.h"
11#include "chrome/browser/sync/syncable/syncable.h"
12
13namespace browser_sync {
14
15CleanupDisabledTypesCommand::CleanupDisabledTypesCommand() {}
16CleanupDisabledTypesCommand::~CleanupDisabledTypesCommand() {}
17
18void CleanupDisabledTypesCommand::ExecuteImpl(sessions::SyncSession* session) {
19  syncable::ModelTypeSet to_cleanup;
20  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
21       i < syncable::MODEL_TYPE_COUNT; i++) {
22    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
23
24    if (session->routing_info().count(model_type))
25      continue;
26
27    // The type isn't currently desired.  Because a full directory purge is
28    // slow, we avoid purging undesired types unless we have reason to believe
29    // they were previously enabled.  Because purging could theoretically fail,
30    // on the first sync session (when there's no previous routing info) we pay
31    // the full directory scan price once and do a "deep clean" of types that
32    // may potentially need cleanup so that we converge to the correct state.
33    //
34    //                          in_previous  |   !in_previous
35    //                                       |
36    //   initial_sync_ended     should clean |  may have attempted cleanup
37    //  !initial_sync_ended     should clean |  may have never been enabled, or
38    //                                       |  could have been disabled before
39    //                                       |  initial sync ended and cleanup
40    //                                       |  may not have happened yet
41    //                                       |  (failure, browser restart
42    //                                       |  before another sync session,..)
43    const ModelSafeRoutingInfo& previous_routing =
44        session->context()->previous_session_routing_info();
45    if (previous_routing.empty() || previous_routing.count(model_type))
46      to_cleanup.insert(model_type);
47  }
48
49  if (to_cleanup.empty())
50    return;
51
52  syncable::ScopedDirLookup dir(session->context()->directory_manager(),
53                                session->context()->account_name());
54  if (!dir.good()) {
55    LOG(ERROR) << "Scoped dir lookup failed!";
56    return;
57  }
58
59  dir->PurgeEntriesWithTypeIn(to_cleanup);
60}
61
62}  // namespace browser_sync
63
64