Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/glue/typed_url_model_associator.h" | 5 #include "chrome/browser/sync/glue/typed_url_model_associator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/tracked.h" | 11 #include "base/tracked.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/history/history_backend.h" | 13 #include "chrome/browser/history/history_backend.h" |
| 14 #include "chrome/browser/sync/api/sync_error.h" | 14 #include "chrome/browser/sync/api/sync_error.h" |
| 15 #include "chrome/browser/sync/engine/syncapi.h" | 15 #include "chrome/browser/sync/engine/syncapi.h" |
| 16 #include "chrome/browser/sync/profile_sync_service.h" | 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 17 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" | 17 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" |
| 18 | 18 |
| 19 namespace browser_sync { | 19 namespace browser_sync { |
| 20 | 20 |
| 21 // The server backend can't handle arbitrarily large node sizes, so to keep | |
| 22 // the size under control we limit the visit array. | |
| 23 static const int kMaxTypedUrlVisits = 100; | |
| 24 | |
| 21 const char kTypedUrlTag[] = "google_chrome_typed_urls"; | 25 const char kTypedUrlTag[] = "google_chrome_typed_urls"; |
| 22 | 26 |
| 23 static bool CheckVisitOrdering(const history::VisitVector& visits) { | 27 static bool CheckVisitOrdering(const history::VisitVector& visits) { |
| 24 int64 previous_visit_time = 0; | 28 int64 previous_visit_time = 0; |
| 25 for (history::VisitVector::const_iterator visit = visits.begin(); | 29 for (history::VisitVector::const_iterator visit = visits.begin(); |
| 26 visit != visits.end(); ++visit) { | 30 visit != visits.end(); ++visit) { |
| 27 if (visit != visits.begin() && | 31 if (visit != visits.begin() && |
| 28 previous_visit_time >= visit->visit_time.ToInternalValue()) { | 32 previous_visit_time >= visit->visit_time.ToInternalValue()) { |
| 29 return false; | 33 return false; |
| 30 } | 34 } |
| (...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 530 | 534 |
| 531 new_url->set_last_visit(visits->back().visit_time); | 535 new_url->set_last_visit(visits->back().visit_time); |
| 532 return different; | 536 return different; |
| 533 } | 537 } |
| 534 | 538 |
| 535 // static | 539 // static |
| 536 void TypedUrlModelAssociator::WriteToSyncNode( | 540 void TypedUrlModelAssociator::WriteToSyncNode( |
| 537 const history::URLRow& url, | 541 const history::URLRow& url, |
| 538 const history::VisitVector& visits, | 542 const history::VisitVector& visits, |
| 539 sync_api::WriteNode* node) { | 543 sync_api::WriteNode* node) { |
| 544 sync_pb::TypedUrlSpecifics typed_url; | |
| 545 WriteToTypedUrlSpecifics(url, visits, &typed_url); | |
| 546 node->SetTypedUrlSpecifics(typed_url); | |
| 547 } | |
| 548 | |
| 549 void TypedUrlModelAssociator::WriteToTypedUrlSpecifics( | |
| 550 const history::URLRow& url, | |
| 551 const history::VisitVector& visits, | |
| 552 sync_pb::TypedUrlSpecifics* typed_url) { | |
| 553 | |
| 540 DCHECK(!url.last_visit().is_null()); | 554 DCHECK(!url.last_visit().is_null()); |
| 541 DCHECK(!visits.empty()); | 555 DCHECK(!visits.empty()); |
| 542 DCHECK(url.last_visit() == visits.back().visit_time); | 556 DCHECK(url.last_visit() == visits.back().visit_time); |
| 543 | 557 |
| 544 sync_pb::TypedUrlSpecifics typed_url; | 558 typed_url->set_url(url.url().spec()); |
| 545 typed_url.set_url(url.url().spec()); | 559 typed_url->set_title(UTF16ToUTF8(url.title())); |
| 546 typed_url.set_title(UTF16ToUTF8(url.title())); | 560 typed_url->set_hidden(url.hidden()); |
| 547 typed_url.set_hidden(url.hidden()); | |
| 548 | 561 |
| 549 DCHECK(CheckVisitOrdering(visits)); | 562 DCHECK(CheckVisitOrdering(visits)); |
| 563 | |
| 564 bool only_typed = false; | |
| 565 int skip_count = 0; | |
| 566 | |
| 567 if (visits.size() > static_cast<size_t>(kMaxTypedUrlVisits)) { | |
| 568 int typed_count = 0; | |
| 569 int total = 0; | |
| 570 // Walk the passed-in visit vector and count the # typed visits. | |
|
Rick Campbell
2011/08/09 21:25:26
uber-nit: ... the # of typed visits.
Andrew T Wilson (Slow)
2011/08/10 21:11:31
Done.
| |
| 571 for (history::VisitVector::const_iterator visit = visits.begin(); | |
| 572 visit != visits.end(); ++visit) { | |
| 573 PageTransition::Type transition = static_cast<PageTransition::Type>( | |
| 574 visit->transition) & PageTransition::CORE_MASK; | |
| 575 // We ignore reload visits. | |
| 576 if (transition == PageTransition::RELOAD) | |
| 577 continue; | |
| 578 total++; | |
|
Rick Campbell
2011/08/09 21:25:26
nit: ++total
This is definitely deep into nit-pic
Andrew T Wilson (Slow)
2011/08/10 21:11:31
Done.
| |
| 579 if (transition == PageTransition::TYPED) | |
| 580 typed_count++; | |
|
Rick Campbell
2011/08/09 21:25:26
++typed_count
Andrew T Wilson (Slow)
2011/08/10 21:11:31
Done.
| |
| 581 } | |
| 582 | |
| 583 if (typed_count > kMaxTypedUrlVisits) { | |
| 584 only_typed = true; | |
| 585 skip_count = typed_count - kMaxTypedUrlVisits; | |
| 586 } else if (total > kMaxTypedUrlVisits) { | |
| 587 skip_count = total - kMaxTypedUrlVisits; | |
| 588 } | |
| 589 } | |
| 590 | |
| 591 | |
| 550 for (history::VisitVector::const_iterator visit = visits.begin(); | 592 for (history::VisitVector::const_iterator visit = visits.begin(); |
| 551 visit != visits.end(); ++visit) { | 593 visit != visits.end(); ++visit) { |
| 552 typed_url.add_visits(visit->visit_time.ToInternalValue()); | 594 PageTransition::Type transition = static_cast<PageTransition::Type>( |
| 553 typed_url.add_visit_transitions(visit->transition); | 595 visit->transition) & PageTransition::CORE_MASK; |
| 596 // Skip reload visits. | |
| 597 if (transition == PageTransition::RELOAD) | |
| 598 continue; | |
| 599 | |
| 600 // If we only have room for typed visits, then only add typed visits. | |
| 601 if (only_typed && transition != PageTransition::TYPED) | |
| 602 continue; | |
| 603 | |
| 604 if (skip_count > 0) { | |
| 605 // We have too many entries to fit, so we need to skip the oldest ones. | |
| 606 // Only skip typed URLs if there are too many typed URLs to fit. | |
| 607 if (only_typed || transition != PageTransition::TYPED) { | |
| 608 skip_count--; | |
|
Rick Campbell
2011/08/09 21:25:26
--skip_count
Andrew T Wilson (Slow)
2011/08/10 21:11:31
Done.
| |
| 609 continue; | |
| 610 } | |
| 611 } | |
| 612 typed_url->add_visits(visit->visit_time.ToInternalValue()); | |
| 613 typed_url->add_visit_transitions(visit->transition); | |
| 554 } | 614 } |
| 555 | 615 CHECK_GT(typed_url->visits_size(), 0); |
|
Rick Campbell
2011/08/09 21:25:26
Maybe DCHECK(skip_count == 0) here?
Andrew T Wilson (Slow)
2011/08/10 21:11:31
Done.
| |
| 556 node->SetTypedUrlSpecifics(typed_url); | 616 CHECK_LE(typed_url->visits_size(), kMaxTypedUrlVisits); |
| 557 } | 617 } |
| 558 | 618 |
| 559 // static | 619 // static |
| 560 void TypedUrlModelAssociator::DiffVisits( | 620 void TypedUrlModelAssociator::DiffVisits( |
| 561 const history::VisitVector& old_visits, | 621 const history::VisitVector& old_visits, |
| 562 const sync_pb::TypedUrlSpecifics& new_url, | 622 const sync_pb::TypedUrlSpecifics& new_url, |
| 563 std::vector<history::VisitInfo>* new_visits, | 623 std::vector<history::VisitInfo>* new_visits, |
| 564 history::VisitVector* removed_visits) { | 624 history::VisitVector* removed_visits) { |
| 565 size_t old_visit_count = old_visits.size(); | 625 size_t old_visit_count = old_visits.size(); |
| 566 size_t new_visit_count = new_url.visits_size(); | 626 size_t new_visit_count = new_url.visits_size(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 608 bool TypedUrlModelAssociator::CryptoReadyIfNecessary() { | 668 bool TypedUrlModelAssociator::CryptoReadyIfNecessary() { |
| 609 // We only access the cryptographer while holding a transaction. | 669 // We only access the cryptographer while holding a transaction. |
| 610 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 670 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 611 syncable::ModelTypeSet encrypted_types; | 671 syncable::ModelTypeSet encrypted_types; |
| 612 encrypted_types = sync_api::GetEncryptedTypes(&trans); | 672 encrypted_types = sync_api::GetEncryptedTypes(&trans); |
| 613 return encrypted_types.count(syncable::TYPED_URLS) == 0 || | 673 return encrypted_types.count(syncable::TYPED_URLS) == 0 || |
| 614 sync_service_->IsCryptographerReady(&trans); | 674 sync_service_->IsCryptographerReady(&trans); |
| 615 } | 675 } |
| 616 | 676 |
| 617 } // namespace browser_sync | 677 } // namespace browser_sync |
| OLD | NEW |