summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/model_associator.cc
blob: 8fc1411312a96d61facaa5360cff9e0bb0242d22 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (c) 2006-2009 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.

#if defined(BROWSER_SYNC)

#include "chrome/browser/sync/glue/model_associator.h"

#include <stack>

#include "base/message_loop.h"
#include "base/task.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/profile_sync_service.h"

namespace browser_sync {

// The sync protocol identifies top-level entities by means of well-known tags,
// which should not be confused with titles.  Each tag corresponds to a
// singleton instance of a particular top-level node in a user's share; the
// tags are consistent across users. The tags allow us to locate the specific
// folders whose contents we care about synchronizing, without having to do a
// lookup by name or path.  The tags should not be made user-visible.
// For example, the tag "bookmark_bar" represents the permanent node for
// bookmarks bar in Chrome. The tag "other_bookmarks" represents the permanent
// folder Other Bookmarks in Chrome.
//
// It is the responsibility of something upstream (at time of writing,
// the sync server) to create these tagged nodes when initializing sync
// for the first time for a user.  Thus, once the backend finishes
// initializing, the ProfileSyncService can rely on the presence of tagged
// nodes.
//
// TODO(ncarter): Pull these tags from an external protocol specification
// rather than hardcoding them here.
static const wchar_t* kOtherBookmarksTag = L"other_bookmarks";
static const wchar_t* kBookmarkBarTag = L"bookmark_bar";

// Bookmark comparer for map of bookmark nodes.
class BookmarkComparer {
 public:
  // Compares the two given nodes and returns whether node1 should appear
  // before node2 in strict weak ordering.
  bool operator()(const BookmarkNode* node1,
                  const BookmarkNode* node2) const {
    DCHECK(node1);
    DCHECK(node2);

    // Keep folder nodes before non-folder nodes.
    if (node1->is_folder() != node2->is_folder())
      return node1->is_folder();

    int result = node1->GetTitle().compare(node2->GetTitle());
    if (result != 0)
      return result < 0;

    result = node1->GetURL().spec().compare(node2->GetURL().spec());
    if (result != 0)
      return result < 0;

    return false;
  }
};

// Provides the following abstraction: given a parent bookmark node, find best
// matching child node for many sync nodes.
class BookmarkNodeFinder {
 public:
  // Creates an instance with the given parent bookmark node.
  explicit BookmarkNodeFinder(const BookmarkNode* parent_node);

  // Finds best matching node for the given sync node.
  // Returns the matching node if one exists; NULL otherwise. If a matching
  // node is found, it's removed for further matches.
  const BookmarkNode* FindBookmarkNode(const sync_api::BaseNode& sync_node);

 private:
  typedef std::multiset<const BookmarkNode*, BookmarkComparer> BookmarkNodesSet;

  const BookmarkNode* parent_node_;
  BookmarkNodesSet child_nodes_;

  DISALLOW_COPY_AND_ASSIGN(BookmarkNodeFinder);
};

BookmarkNodeFinder::BookmarkNodeFinder(const BookmarkNode* parent_node)
    : parent_node_(parent_node) {
  for (int i = 0; i < parent_node_->GetChildCount(); ++i) {
    child_nodes_.insert(parent_node_->GetChild(i));
  }
}

const BookmarkNode* BookmarkNodeFinder::FindBookmarkNode(
    const sync_api::BaseNode& sync_node) {
  // Create a bookmark node from the given sync node.
  BookmarkNode temp_node(GURL(sync_node.GetURL()));
  temp_node.SetTitle(UTF16ToWide(sync_node.GetTitle()));
  if (sync_node.GetIsFolder())
    temp_node.SetType(BookmarkNode::FOLDER);
  else
    temp_node.SetType(BookmarkNode::URL);

  const BookmarkNode* result = NULL;
  BookmarkNodesSet::iterator iter = child_nodes_.find(&temp_node);
  if (iter != child_nodes_.end()) {
    result = *iter;
    // Remove the matched node so we don't match with it again.
    child_nodes_.erase(iter);
  }

  return result;
}

ModelAssociator::ModelAssociator(ProfileSyncService* sync_service)
    : sync_service_(sync_service),
      task_pending_(false) {
  DCHECK(sync_service_);
}

void ModelAssociator::ClearAll() {
  id_map_.clear();
  id_map_inverse_.clear();
  dirty_assocations_sync_ids_.clear();
}

int64 ModelAssociator::GetSyncIdFromBookmarkId(int64 node_id) const {
  BookmarkIdToSyncIdMap::const_iterator iter = id_map_.find(node_id);
  return iter == id_map_.end() ? sync_api::kInvalidId : iter->second;
}

bool ModelAssociator::GetBookmarkIdFromSyncId(int64 sync_id,
                                              int64* node_id) const {
  SyncIdToBookmarkIdMap::const_iterator iter = id_map_inverse_.find(sync_id);
  if (iter == id_map_inverse_.end())
    return false;
  *node_id = iter->second;
  return true;
}

bool ModelAssociator::InitSyncNodeFromBookmarkId(
    int64 node_id,
    sync_api::BaseNode* sync_node) {
  DCHECK(sync_node);
  int64 sync_id = GetSyncIdFromBookmarkId(node_id);
  if (sync_id == sync_api::kInvalidId)
    return false;
  if (!sync_node->InitByIdLookup(sync_id))
    return false;
  DCHECK(sync_node->GetId() == sync_id);
  return true;
}

const BookmarkNode* ModelAssociator::GetBookmarkNodeFromSyncId(int64 sync_id) {
  int64 node_id;
  if (!GetBookmarkIdFromSyncId(sync_id, &node_id))
    return false;
  BookmarkModel* model = sync_service_->profile()->GetBookmarkModel();
  return model->GetNodeByID(node_id);
}

void ModelAssociator::AssociateIds(int64 node_id, int64 sync_id) {
  DCHECK_NE(sync_id, sync_api::kInvalidId);
  DCHECK(id_map_.find(node_id) == id_map_.end());
  DCHECK(id_map_inverse_.find(sync_id) == id_map_inverse_.end());
  id_map_[node_id] = sync_id;
  id_map_inverse_[sync_id] = node_id;
  dirty_assocations_sync_ids_.insert(sync_id);
  PostPersistAssociationsTask();
}

void ModelAssociator::DisassociateIds(int64 sync_id) {
  SyncIdToBookmarkIdMap::iterator iter = id_map_inverse_.find(sync_id);
  if (iter == id_map_inverse_.end())
    return;
  id_map_.erase(iter->second);
  id_map_inverse_.erase(iter);
  dirty_assocations_sync_ids_.erase(sync_id);
}

bool ModelAssociator::BookmarkModelHasUserCreatedNodes() const {
  BookmarkModel* model = sync_service_->profile()->GetBookmarkModel();
  DCHECK(model->IsLoaded());
  return model->GetBookmarkBarNode()->GetChildCount() > 0 ||
         model->other_node()->GetChildCount() > 0;
}

bool ModelAssociator::SyncModelHasUserCreatedNodes() {
  int64 bookmark_bar_sync_id;
  if (!GetSyncIdForTaggedNode(WideToUTF16(kBookmarkBarTag),
                              &bookmark_bar_sync_id)) {
    sync_service_->OnUnrecoverableError();
    return false;
  }
  int64 other_bookmarks_sync_id;
  if (!GetSyncIdForTaggedNode(WideToUTF16(kOtherBookmarksTag),
                              &other_bookmarks_sync_id)) {
    sync_service_->OnUnrecoverableError();
    return false;
  }

  sync_api::ReadTransaction trans(
      sync_service_->backend()->GetUserShareHandle());

  sync_api::ReadNode bookmark_bar_node(&trans);
  if (!bookmark_bar_node.InitByIdLookup(bookmark_bar_sync_id)) {
    sync_service_->OnUnrecoverableError();
    return false;
  }

  sync_api::ReadNode other_bookmarks_node(&trans);
  if (!other_bookmarks_node.InitByIdLookup(other_bookmarks_sync_id)) {
    sync_service_->OnUnrecoverableError();
    return false;
  }

  // Sync model has user created nodes if either one of the permanent nodes
  // has children.
  return bookmark_bar_node.GetFirstChildId() != sync_api::kInvalidId ||
         other_bookmarks_node.GetFirstChildId() != sync_api::kInvalidId;
}

bool ModelAssociator::NodesMatch(const BookmarkNode* bookmark,
                                 const sync_api::BaseNode* sync_node) const {
  if (bookmark->GetTitle() != UTF16ToWide(sync_node->GetTitle()))
    return false;
  if (bookmark->is_folder() != sync_node->GetIsFolder())
    return false;
  if (bookmark->is_url()) {
    if (bookmark->GetURL() != GURL(sync_node->GetURL()))
      return false;
  }
  // Don't compare favicons here, because they are not really
  // user-updated and we don't have versioning information -- a site changing
  // its favicon shouldn't result in a bookmark mismatch.
  return true;
}

bool ModelAssociator::AssociateTaggedPermanentNode(
    const BookmarkNode* permanent_node,
    const string16 &tag) {
  // Do nothing if |permanent_node| is already initialized and associated.
  int64 sync_id = GetSyncIdFromBookmarkId(permanent_node->id());
  if (sync_id != sync_api::kInvalidId)
    return true;
  if (!GetSyncIdForTaggedNode(tag, &sync_id))
    return false;

  AssociateIds(permanent_node->id(), sync_id);
  return true;
}

bool ModelAssociator::GetSyncIdForTaggedNode(const string16& tag,
                                             int64* sync_id) {
  sync_api::ReadTransaction trans(
      sync_service_->backend()->GetUserShareHandle());
  sync_api::ReadNode sync_node(&trans);
  if (!sync_node.InitByTagLookup(tag.c_str()))
    return false;
  *sync_id = sync_node.GetId();
  return true;
}

bool ModelAssociator::AssociateModels() {
  // Try to load model associations from persisted associations first. If that
  // succeeds, we don't need to run the complex model matching algorithm.
  if (LoadAssociations())
    return true;

  ClearAll();

  // We couldn't load model assocations from persisted assocations. So build
  // them.
  return BuildAssocations();
}

bool ModelAssociator::BuildAssocations() {
  // Algorithm description:
  // Match up the roots and recursively do the following:
  // * For each sync node for the current sync parent node, find the best
  //   matching bookmark node under the corresponding bookmark parent node.
  //   If no matching node is found, create a new bookmark node in the same
  //   position as the corresponding sync node.
  //   If a matching node is found, update the properties of it from the
  //   corresponding sync node.
  // * When all children sync nodes are done, add the extra children bookmark
  //   nodes to the sync parent node.
  //
  // This algorithm will do a good job of merging when folder names are a good
  // indicator of the two folders being the same. It will handle reordering and
  // new node addition very well (without creating duplicates).
  // This algorithm will not do well if the folder name has changes but the
  // children under them are all the same.

  BookmarkModel* model = sync_service_->profile()->GetBookmarkModel();
  DCHECK(model->IsLoaded());

  // To prime our association, we associate the top-level nodes, Bookmark Bar
  // and Other Bookmarks.
  if (!AssociateTaggedPermanentNode(model->other_node(),
                                    WideToUTF16(kOtherBookmarksTag))) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create top-level nodes.  Possibly we "
               << "are running against an out-of-date server?";
    return false;
  }
  if (!AssociateTaggedPermanentNode(model->GetBookmarkBarNode(),
                                    WideToUTF16(kBookmarkBarTag))) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create top-level nodes.  Possibly we "
               << "are running against an out-of-date server?";
    return false;
  }
  int64 bookmark_bar_sync_id = GetSyncIdFromBookmarkId(
      model->GetBookmarkBarNode()->id());
  DCHECK(bookmark_bar_sync_id != sync_api::kInvalidId);
  int64 other_bookmarks_sync_id = GetSyncIdFromBookmarkId(
      model->other_node()->id());
  DCHECK(other_bookmarks_sync_id != sync_api::kInvalidId);

  std::stack<int64> dfs_stack;
  dfs_stack.push(other_bookmarks_sync_id);
  dfs_stack.push(bookmark_bar_sync_id);

  sync_api::WriteTransaction trans(
      sync_service_->backend()->GetUserShareHandle());

  while (!dfs_stack.empty()) {
    int64 sync_parent_id = dfs_stack.top();
    dfs_stack.pop();

    sync_api::ReadNode sync_parent(&trans);
    if (!sync_parent.InitByIdLookup(sync_parent_id)) {
      sync_service_->OnUnrecoverableError();
      return false;
    }
    // Only folder nodes are pushed on to the stack.
    DCHECK(sync_parent.GetIsFolder());

    const BookmarkNode* parent_node = GetBookmarkNodeFromSyncId(sync_parent_id);
    DCHECK(parent_node->is_folder());

    BookmarkNodeFinder node_finder(parent_node);

    int index = 0;
    int64 sync_child_id = sync_parent.GetFirstChildId();
    while (sync_child_id != sync_api::kInvalidId) {
      sync_api::WriteNode sync_child_node(&trans);
      if (!sync_child_node.InitByIdLookup(sync_child_id)) {
        sync_service_->OnUnrecoverableError();
        return false;
      }

      const BookmarkNode* child_node = NULL;
      child_node = node_finder.FindBookmarkNode(sync_child_node);
      if (child_node) {
        model->Move(child_node, parent_node, index);
        // Set the favicon for bookmark node from sync node or vice versa.
        if (ChangeProcessor::SetBookmarkFavicon(&sync_child_node,
            child_node, sync_service_->profile())) {
          ChangeProcessor::SetSyncNodeFavicon(child_node, model,
                                              &sync_child_node);
        }
      } else {
        // Create a new bookmark node for the sync node.
        child_node = ChangeProcessor::CreateBookmarkNode(&sync_child_node,
            parent_node, model, index);
      }
      AssociateIds(child_node->id(), sync_child_id);
      if (sync_child_node.GetIsFolder())
        dfs_stack.push(sync_child_id);

      sync_child_id = sync_child_node.GetSuccessorId();
      ++index;
    }

    // At this point all the children nodes of the parent sync node have
    // corresponding children in the parent bookmark node and they are all in
    // the right positions: from 0 to index - 1.
    // So the children starting from index in the parent bookmark node are the
    // ones that are not present in the parent sync node. So create them.
    for (int i = index; i < parent_node->GetChildCount(); ++i) {
      sync_child_id = ChangeProcessor::CreateSyncNode(parent_node, model, i,
          &trans, this, sync_service_);
      if (parent_node->GetChild(i)->is_folder())
        dfs_stack.push(sync_child_id);
    }
  }
  return true;
}

void ModelAssociator::PostPersistAssociationsTask() {
  // No need to post a task if a task is already pending.
  if (task_pending_)
    return;
  task_pending_ = true;
  MessageLoop::current()->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &ModelAssociator::PersistAssociations));
}

void ModelAssociator::PersistAssociations() {
  DCHECK(task_pending_);
  task_pending_ = false;

  // If there are no dirty assocations we have nothing to do. We handle this
  // explicity instead of letting the for loop do it to avoid creating a write
  // transaction in this case.
  if (dirty_assocations_sync_ids_.empty()) {
    DCHECK(id_map_.empty());
    DCHECK(id_map_inverse_.empty());
    return;
  }

  sync_api::WriteTransaction trans(
      sync_service_->backend()->GetUserShareHandle());
  DirtyAssocationsSyncIds::iterator iter;
  for (iter = dirty_assocations_sync_ids_.begin();
       iter != dirty_assocations_sync_ids_.end();
       ++iter) {
    int64 sync_id = *iter;
    sync_api::WriteNode sync_node(&trans);
    if (!sync_node.InitByIdLookup(sync_id)) {
      sync_service_->OnUnrecoverableError();
      return;
    }
    int64 node_id;
    if (GetBookmarkIdFromSyncId(sync_id, &node_id))
      sync_node.SetExternalId(node_id);
    else
      NOTREACHED();
  }
  dirty_assocations_sync_ids_.clear();
}

bool ModelAssociator::LoadAssociations() {
  BookmarkModel* model = sync_service_->profile()->GetBookmarkModel();
  DCHECK(model->IsLoaded());
  // If the bookmarks changed externally, our previous assocations may not be
  // valid; so return false.
  if (model->file_changed())
    return false;

  // Our persisted assocations should be valid. Try to populate id assocation
  // maps using persisted assocations.

  int64 other_bookmarks_id;
  if (!GetSyncIdForTaggedNode(WideToUTF16(kOtherBookmarksTag),
                              &other_bookmarks_id)) {
    // We should always be able to find the permanent nodes.
    sync_service_->OnUnrecoverableError();
    return false;
  }
  int64 bookmark_bar_id;
  if (!GetSyncIdForTaggedNode(WideToUTF16(kBookmarkBarTag), &bookmark_bar_id)) {
    // We should always be able to find the permanent nodes.
    sync_service_->OnUnrecoverableError();
    return false;
  }

  std::stack<int64> dfs_stack;
  dfs_stack.push(other_bookmarks_id);
  dfs_stack.push(bookmark_bar_id);

  sync_api::ReadTransaction trans(
      sync_service_->backend()->GetUserShareHandle());

  while (!dfs_stack.empty()) {
    int64 parent_id = dfs_stack.top();
    dfs_stack.pop();
    sync_api::ReadNode sync_parent(&trans);
    if (!sync_parent.InitByIdLookup(parent_id)) {
      sync_service_->OnUnrecoverableError();
      return false;
    }

    int64 external_id = sync_parent.GetExternalId();
    if (external_id == 0)
      return false;

    const BookmarkNode* node = model->GetNodeByID(external_id);
    if (!node)
      return false;

    // Don't try to call NodesMatch on permanent nodes like bookmark bar and
    // other bookmarks. They are not expected to match.
    if (node != model->GetBookmarkBarNode() &&
        node != model->other_node() &&
        !NodesMatch(node, &sync_parent))
      return false;

    AssociateIds(external_id, sync_parent.GetId());

    // Add all children of the current node to the stack.
    int64 child_id = sync_parent.GetFirstChildId();
    while (child_id != sync_api::kInvalidId) {
      dfs_stack.push(child_id);
      sync_api::ReadNode child_node(&trans);
      if (!child_node.InitByIdLookup(child_id)) {
        sync_service_->OnUnrecoverableError();
        return false;
      }
      child_id = child_node.GetSuccessorId();
    }
  }
  DCHECK(dfs_stack.empty());
  return true;
}

}  // namespace browser_sync

#endif  // defined(BROWSER_SYNC)