Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(649)

Side by Side Diff: chrome/browser/sync/engine/syncapi.cc

Issue 7033043: [Sync] Speed up sync node browser/search in about:sync (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/sync/engine/syncapi.h ('k') | chrome/browser/sync/engine/syncapi_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/engine/syncapi.h" 5 #include "chrome/browser/sync/engine/syncapi.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <bitset> 8 #include <bitset>
9 #include <iomanip> 9 #include <iomanip>
10 #include <list> 10 #include <list>
11 #include <map>
11 #include <queue> 12 #include <queue>
12 #include <string> 13 #include <string>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/base64.h" 16 #include "base/base64.h"
17 #include "base/bind.h"
18 #include "base/callback.h"
16 #include "base/command_line.h" 19 #include "base/command_line.h"
17 #include "base/json/json_writer.h" 20 #include "base/json/json_writer.h"
18 #include "base/logging.h" 21 #include "base/logging.h"
19 #include "base/memory/scoped_ptr.h" 22 #include "base/memory/scoped_ptr.h"
20 #include "base/message_loop.h" 23 #include "base/message_loop.h"
21 #include "base/observer_list.h" 24 #include "base/observer_list.h"
22 #include "base/sha1.h" 25 #include "base/sha1.h"
23 #include "base/string_number_conversions.h" 26 #include "base/string_number_conversions.h"
24 #include "base/string_util.h" 27 #include "base/string_util.h"
25 #include "base/synchronization/lock.h" 28 #include "base/synchronization/lock.h"
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 int64 BaseNode::GetFirstChildId() const { 326 int64 BaseNode::GetFirstChildId() const {
324 syncable::Directory* dir = GetTransaction()->GetLookup(); 327 syncable::Directory* dir = GetTransaction()->GetLookup();
325 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans(); 328 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans();
326 syncable::Id id_string = 329 syncable::Id id_string =
327 dir->GetFirstChildId(trans, GetEntry()->Get(syncable::ID)); 330 dir->GetFirstChildId(trans, GetEntry()->Get(syncable::ID));
328 if (id_string.IsRoot()) 331 if (id_string.IsRoot())
329 return kInvalidId; 332 return kInvalidId;
330 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string); 333 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string);
331 } 334 }
332 335
333 DictionaryValue* BaseNode::ToValue() const { 336 DictionaryValue* BaseNode::GetSummaryAsValue() const {
334 DictionaryValue* node_info = new DictionaryValue(); 337 DictionaryValue* node_info = new DictionaryValue();
335 node_info->SetString("id", base::Int64ToString(GetId())); 338 node_info->SetString("id", base::Int64ToString(GetId()));
339 node_info->SetBoolean("isFolder", GetIsFolder());
340 // TODO(akalin): Add a std::string accessor for the title.
341 node_info->SetString("title", WideToUTF8(GetTitle()));
342 node_info->Set("type", ModelTypeToValue(GetModelType()));
343 return node_info;
344 }
345
346 DictionaryValue* BaseNode::GetDetailsAsValue() const {
347 DictionaryValue* node_info = GetSummaryAsValue();
336 // TODO(akalin): Return time in a better format. 348 // TODO(akalin): Return time in a better format.
337 node_info->SetString("modificationTime", 349 node_info->SetString("modificationTime",
338 base::Int64ToString(GetModificationTime())); 350 base::Int64ToString(GetModificationTime()));
339 node_info->SetString("parentId", base::Int64ToString(GetParentId())); 351 node_info->SetString("parentId", base::Int64ToString(GetParentId()));
340 node_info->SetBoolean("isFolder", GetIsFolder());
341 // TODO(akalin): Add a std::string accessor for the title.
342 node_info->SetString("title", WideToUTF8(GetTitle()));
343 node_info->Set("type", ModelTypeToValue(GetModelType()));
344 // Specifics are already in the Entry value, so no need to duplicate 352 // Specifics are already in the Entry value, so no need to duplicate
345 // it here. 353 // it here.
346 node_info->SetString("externalId", 354 node_info->SetString("externalId",
347 base::Int64ToString(GetExternalId())); 355 base::Int64ToString(GetExternalId()));
348 node_info->SetString("predecessorId", 356 node_info->SetString("predecessorId",
349 base::Int64ToString(GetPredecessorId())); 357 base::Int64ToString(GetPredecessorId()));
350 node_info->SetString("successorId", 358 node_info->SetString("successorId",
351 base::Int64ToString(GetSuccessorId())); 359 base::Int64ToString(GetSuccessorId()));
352 node_info->SetString("firstChildId", 360 node_info->SetString("firstChildId",
353 base::Int64ToString(GetFirstChildId())); 361 base::Int64ToString(GetFirstChildId()));
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 node_dict->SetString("id", base::Int64ToString(id)); 1102 node_dict->SetString("id", base::Int64ToString(id));
1095 node_dict->Set("specifics", 1103 node_dict->Set("specifics",
1096 browser_sync::EntitySpecificsToValue(specifics)); 1104 browser_sync::EntitySpecificsToValue(specifics));
1097 if (extra.get()) { 1105 if (extra.get()) {
1098 node_dict->Set("extra", extra->ToValue()); 1106 node_dict->Set("extra", extra->ToValue());
1099 } 1107 }
1100 node_value = node_dict; 1108 node_value = node_dict;
1101 } else { 1109 } else {
1102 ReadNode node(trans); 1110 ReadNode node(trans);
1103 if (node.InitByIdLookup(id)) { 1111 if (node.InitByIdLookup(id)) {
1104 node_value = node.ToValue(); 1112 node_value = node.GetDetailsAsValue();
1105 } 1113 }
1106 } 1114 }
1107 if (!node_value) { 1115 if (!node_value) {
1108 NOTREACHED(); 1116 NOTREACHED();
1109 node_value = Value::CreateNullValue(); 1117 node_value = Value::CreateNullValue();
1110 } 1118 }
1111 value->Set("node", node_value); 1119 value->Set("node", node_value);
1112 return value; 1120 return value;
1113 } 1121 }
1114 1122
1115 bool BaseNode::ContainsString(const std::string& lowercase_query) const {
1116 DCHECK(GetEntry());
1117 // TODO(lipalani) - figure out what to do if the node is encrypted.
1118 const sync_pb::EntitySpecifics& specifics = GetEntry()->Get(SPECIFICS);
1119 std::string temp;
1120 // The protobuf serialized string contains the original strings. So
1121 // we will just serialize it and search it.
1122 specifics.SerializeToString(&temp);
1123
1124 // Now convert to lower case.
1125 StringToLowerASCII(&temp);
1126
1127 if (temp.find(lowercase_query) != std::string::npos)
1128 return true;
1129
1130 // Now go through all the string fields to see if the value is there.
1131 for (int i = syncable::STRING_FIELDS_BEGIN; i < syncable::STRING_FIELDS_END;
1132 ++i) {
1133 std::string value = GetEntry()->Get(
1134 static_cast<syncable::StringField>(i));
1135
1136 StringToLowerASCII(&value);
1137 if (value.find(lowercase_query) != std::string::npos)
1138 return true;
1139 }
1140 return false;
1141 }
1142
1143 SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData() {} 1123 SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData() {}
1144 1124
1145 SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData( 1125 SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData(
1146 const sync_pb::PasswordSpecificsData& data) 1126 const sync_pb::PasswordSpecificsData& data)
1147 : unencrypted_(data) { 1127 : unencrypted_(data) {
1148 } 1128 }
1149 1129
1150 SyncManager::ExtraPasswordChangeRecordData::~ExtraPasswordChangeRecordData() {} 1130 SyncManager::ExtraPasswordChangeRecordData::~ExtraPasswordChangeRecordData() {}
1151 1131
1152 DictionaryValue* SyncManager::ExtraPasswordChangeRecordData::ToValue() const { 1132 DictionaryValue* SyncManager::ExtraPasswordChangeRecordData::ToValue() const {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1217 initialized_(false), 1197 initialized_(false),
1218 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 1198 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
1219 js_directory_change_listener_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 1199 js_directory_change_listener_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
1220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1221 // Pre-fill |notification_info_map_|. 1201 // Pre-fill |notification_info_map_|.
1222 for (int i = syncable::FIRST_REAL_MODEL_TYPE; 1202 for (int i = syncable::FIRST_REAL_MODEL_TYPE;
1223 i < syncable::MODEL_TYPE_COUNT; ++i) { 1203 i < syncable::MODEL_TYPE_COUNT; ++i) {
1224 notification_info_map_.insert( 1204 notification_info_map_.insert(
1225 std::make_pair(syncable::ModelTypeFromInt(i), NotificationInfo())); 1205 std::make_pair(syncable::ModelTypeFromInt(i), NotificationInfo()));
1226 } 1206 }
1207
lipalani1 2011/06/02 23:06:39 This list does not seem to change. So is it possib
akalin 2011/06/02 23:14:58 As we discussed, C++ isn't expressive enough to do
1208 // Bind message handlers.
1209 BindJsMessageHandler(
1210 "getNotificationState",
1211 &SyncManager::SyncInternal::GetNotificationState);
1212 BindJsMessageHandler(
1213 "getNotificationInfo",
1214 &SyncManager::SyncInternal::GetNotificationInfo);
1215 BindJsMessageHandler(
1216 "getRootNodeDetails",
1217 &SyncManager::SyncInternal::GetRootNodeDetails);
1218 BindJsMessageHandler(
1219 "getNodeSummariesById",
1220 &SyncManager::SyncInternal::GetNodeSummariesById);
1221 BindJsMessageHandler(
1222 "getNodeDetailsById",
1223 &SyncManager::SyncInternal::GetNodeDetailsById);
1224 BindJsMessageHandler(
1225 "getChildNodeIds",
1226 &SyncManager::SyncInternal::GetChildNodeIds);
1227 BindJsMessageHandler(
1228 "findNodesContainingString",
1229 &SyncManager::SyncInternal::FindNodesContainingString);
1227 } 1230 }
1228 1231
1229 virtual ~SyncInternal() { 1232 virtual ~SyncInternal() {
1230 CHECK(!core_message_loop_); 1233 CHECK(!core_message_loop_);
1231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1232 } 1235 }
1233 1236
1234 bool Init(const FilePath& database_location, 1237 bool Init(const FilePath& database_location,
1235 const std::string& sync_server_and_path, 1238 const std::string& sync_server_and_path,
1236 int port, 1239 int port,
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 1438
1436 // JsEventRouter implementation. 1439 // JsEventRouter implementation.
1437 virtual void RouteJsEvent( 1440 virtual void RouteJsEvent(
1438 const std::string& name, 1441 const std::string& name,
1439 const browser_sync::JsEventDetails& details) OVERRIDE; 1442 const browser_sync::JsEventDetails& details) OVERRIDE;
1440 virtual void RouteJsMessageReply( 1443 virtual void RouteJsMessageReply(
1441 const std::string& name, 1444 const std::string& name,
1442 const browser_sync::JsArgList& args, 1445 const browser_sync::JsArgList& args,
1443 const browser_sync::JsEventHandler* target) OVERRIDE; 1446 const browser_sync::JsEventHandler* target) OVERRIDE;
1444 1447
1445 ListValue* FindNodesContainingString(const std::string& query); 1448 private:
1449 typedef browser_sync::JsArgList
1450 (SyncManager::SyncInternal::*UnboundJsMessageHandler)(
1451 const browser_sync::JsArgList&);
1452 typedef base::Callback<browser_sync::JsArgList(browser_sync::JsArgList)>
1453 JsMessageHandler;
1454 typedef std::map<std::string, JsMessageHandler> JsMessageHandlerMap;
1446 1455
1447 private:
1448 // Helper to call OnAuthError when no authentication credentials are 1456 // Helper to call OnAuthError when no authentication credentials are
1449 // available. 1457 // available.
1450 void RaiseAuthNeededEvent(); 1458 void RaiseAuthNeededEvent();
1451 1459
1452 // Helper to set initialized_ to true and raise an event to clients to notify 1460 // Helper to set initialized_ to true and raise an event to clients to notify
1453 // that initialization is complete and it is safe to send us changes. If 1461 // that initialization is complete and it is safe to send us changes. If
1454 // already initialized, this is a no-op. 1462 // already initialized, this is a no-op.
1455 void MarkAndNotifyInitializationComplete(); 1463 void MarkAndNotifyInitializationComplete();
1456 1464
1457 // Sends notifications to peers. 1465 // Sends notifications to peers.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 // Helper for migration to new nigori proto to set 1557 // Helper for migration to new nigori proto to set
1550 // 'using_explicit_passphrase' in the NigoriSpecifics. 1558 // 'using_explicit_passphrase' in the NigoriSpecifics.
1551 // TODO(tim): Bug 62103. Remove this after it has been pushed out to dev 1559 // TODO(tim): Bug 62103. Remove this after it has been pushed out to dev
1552 // channel users. 1560 // channel users.
1553 void SetUsingExplicitPassphrasePrefForMigration( 1561 void SetUsingExplicitPassphrasePrefForMigration(
1554 WriteTransaction* const trans); 1562 WriteTransaction* const trans);
1555 1563
1556 // Checks for server reachabilty and requests a nudge. 1564 // Checks for server reachabilty and requests a nudge.
1557 void OnIPAddressChangedImpl(); 1565 void OnIPAddressChangedImpl();
1558 1566
1559 // Functions called by ProcessMessage(). 1567 // Helper function used only by the constructor.
1560 browser_sync::JsArgList ProcessGetNodesByIdMessage( 1568 void BindJsMessageHandler(
1569 const std::string& name,
1570 UnboundJsMessageHandler unbound_message_handler);
1571
1572 // JS message handlers.
1573 browser_sync::JsArgList GetNotificationState(
1561 const browser_sync::JsArgList& args); 1574 const browser_sync::JsArgList& args);
1562 1575
1563 browser_sync::JsArgList ProcessGetChildNodeIdsMessage( 1576 browser_sync::JsArgList GetNotificationInfo(
1564 const browser_sync::JsArgList& args); 1577 const browser_sync::JsArgList& args);
1565 1578
1566 browser_sync::JsArgList ProcessFindNodesContainingString( 1579 browser_sync::JsArgList GetRootNodeDetails(
1580 const browser_sync::JsArgList& args);
1581
1582 browser_sync::JsArgList GetNodeSummariesById(
1583 const browser_sync::JsArgList& args);
1584
1585 browser_sync::JsArgList GetNodeDetailsById(
1586 const browser_sync::JsArgList& args);
1587
1588 browser_sync::JsArgList GetChildNodeIds(
1589 const browser_sync::JsArgList& args);
1590
1591 browser_sync::JsArgList FindNodesContainingString(
1567 const browser_sync::JsArgList& args); 1592 const browser_sync::JsArgList& args);
1568 1593
1569 // We couple the DirectoryManager and username together in a UserShare member 1594 // We couple the DirectoryManager and username together in a UserShare member
1570 // so we can return a handle to share_ to clients of the API for use when 1595 // so we can return a handle to share_ to clients of the API for use when
1571 // constructing any transaction type. 1596 // constructing any transaction type.
1572 UserShare share_; 1597 UserShare share_;
1573 1598
1574 MessageLoop* core_message_loop_; 1599 MessageLoop* core_message_loop_;
1575 1600
1576 ObserverList<SyncManager::Observer> observers_; 1601 ObserverList<SyncManager::Observer> observers_;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1621 // True if the SyncManager should be running in test mode (no syncer thread 1646 // True if the SyncManager should be running in test mode (no syncer thread
1622 // actually communicating with the server). 1647 // actually communicating with the server).
1623 bool setup_for_test_mode_; 1648 bool setup_for_test_mode_;
1624 1649
1625 ScopedRunnableMethodFactory<SyncManager::SyncInternal> method_factory_; 1650 ScopedRunnableMethodFactory<SyncManager::SyncInternal> method_factory_;
1626 1651
1627 // Map used to store the notification info to be displayed in about:sync page. 1652 // Map used to store the notification info to be displayed in about:sync page.
1628 NotificationInfoMap notification_info_map_; 1653 NotificationInfoMap notification_info_map_;
1629 1654
1630 browser_sync::JsDirectoryChangeListener js_directory_change_listener_; 1655 browser_sync::JsDirectoryChangeListener js_directory_change_listener_;
1656
1657 JsMessageHandlerMap js_message_handlers_;
1631 }; 1658 };
1632 const int SyncManager::SyncInternal::kDefaultNudgeDelayMilliseconds = 200; 1659 const int SyncManager::SyncInternal::kDefaultNudgeDelayMilliseconds = 200;
1633 const int SyncManager::SyncInternal::kPreferencesNudgeDelayMilliseconds = 2000; 1660 const int SyncManager::SyncInternal::kPreferencesNudgeDelayMilliseconds = 2000;
1634 1661
1635 SyncManager::Observer::~Observer() {} 1662 SyncManager::Observer::~Observer() {}
1636 1663
1637 SyncManager::SyncManager() { 1664 SyncManager::SyncManager() {
1638 data_ = new SyncInternal(this); 1665 data_ = new SyncInternal(this);
1639 } 1666 }
1640 1667
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
2119 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori); 2146 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori);
2120 node.SetNigoriSpecifics(nigori); 2147 node.SetNigoriSpecifics(nigori);
2121 2148
2122 // TODO(zea): only reencrypt this datatype? ReEncrypting everything is a 2149 // TODO(zea): only reencrypt this datatype? ReEncrypting everything is a
2123 // safer approach, and should not impact anything that is already encrypted 2150 // safer approach, and should not impact anything that is already encrypted
2124 // (redundant changes are ignored). 2151 // (redundant changes are ignored).
2125 ReEncryptEverything(&trans); 2152 ReEncryptEverything(&trans);
2126 return; 2153 return;
2127 } 2154 }
2128 2155
2129 namespace {
2130
2131 void FindChildNodesContainingString(const std::string& lowercase_query,
2132 const ReadNode& parent_node,
2133 sync_api::ReadTransaction* trans,
2134 ListValue* result) {
2135 int64 child_id = parent_node.GetFirstChildId();
2136 while (child_id != kInvalidId) {
2137 ReadNode node(trans);
2138 if (node.InitByIdLookup(child_id)) {
2139 if (node.ContainsString(lowercase_query)) {
2140 result->Append(new StringValue(base::Int64ToString(child_id)));
2141 }
2142 FindChildNodesContainingString(lowercase_query, node, trans, result);
2143 child_id = node.GetSuccessorId();
2144 } else {
2145 LOG(WARNING) << "Lookup of node failed. Id: " << child_id;
2146 return;
2147 }
2148 }
2149 }
2150 } // namespace
2151
2152 // Returned pointer owned by the caller.
2153 ListValue* SyncManager::SyncInternal::FindNodesContainingString(
2154 const std::string& query) {
2155 // Convert the query string to lower case to perform case insensitive
2156 // searches.
2157 std::string lowercase_query = query;
2158 StringToLowerASCII(&lowercase_query);
2159 ReadTransaction trans(GetUserShare());
2160 ReadNode root(&trans);
2161 root.InitByRootLookup();
2162
2163 ListValue* result = new ListValue();
2164
2165 base::Time start_time = base::Time::Now();
2166 FindChildNodesContainingString(lowercase_query, root, &trans, result);
2167 base::Time end_time = base::Time::Now();
2168
2169 base::TimeDelta delta = end_time - start_time;
2170 VLOG(1) << "Time taken in milliseconds to search " << delta.InMilliseconds();
2171
2172 return result;
2173 }
2174
2175 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { 2156 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) {
2176 syncable::ModelTypeSet encrypted_types = 2157 syncable::ModelTypeSet encrypted_types =
2177 GetEncryptedDataTypes(trans->GetWrappedTrans()); 2158 GetEncryptedDataTypes(trans->GetWrappedTrans());
2178 ModelSafeRoutingInfo routes; 2159 ModelSafeRoutingInfo routes;
2179 registrar_->GetModelSafeRoutingInfo(&routes); 2160 registrar_->GetModelSafeRoutingInfo(&routes);
2180 std::string tag; 2161 std::string tag;
2181 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin(); 2162 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin();
2182 iter != encrypted_types.end(); ++iter) { 2163 iter != encrypted_types.end(); ++iter) {
2183 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0) 2164 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0)
2184 continue; 2165 continue;
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after
2678 } 2659 }
2679 2660
2680 lookup->RemoveChangeListener(&js_directory_change_listener_); 2661 lookup->RemoveChangeListener(&js_directory_change_listener_);
2681 } 2662 }
2682 2663
2683 const browser_sync::JsEventRouter* 2664 const browser_sync::JsEventRouter*
2684 SyncManager::SyncInternal::GetParentJsEventRouter() const { 2665 SyncManager::SyncInternal::GetParentJsEventRouter() const {
2685 return parent_router_; 2666 return parent_router_;
2686 } 2667 }
2687 2668
2688 namespace {
2689
2690 void LogNoRouter(const std::string& name,
2691 const browser_sync::JsArgList& args) {
2692 VLOG(1) << "No parent router; not replying to message " << name
2693 << " with args " << args.ToString();
2694 }
2695
2696 } // namespace
2697
2698 void SyncManager::SyncInternal::ProcessMessage( 2669 void SyncManager::SyncInternal::ProcessMessage(
2699 const std::string& name, const browser_sync::JsArgList& args, 2670 const std::string& name, const browser_sync::JsArgList& args,
2700 const browser_sync::JsEventHandler* sender) { 2671 const browser_sync::JsEventHandler* sender) {
2701 DCHECK(initialized_); 2672 DCHECK(initialized_);
2702 if (name == "getNotificationState") { 2673 if (!parent_router_) {
2703 if (!parent_router_) { 2674 VLOG(1) << "No parent router; not replying to message " << name
2704 LogNoRouter(name, args); 2675 << " with args " << args.ToString();
2705 return; 2676 return;
2706 } 2677 }
2707 bool notifications_enabled = allstatus_.status().notifications_enabled;
2708 ListValue return_args;
2709 return_args.Append(Value::CreateBooleanValue(notifications_enabled));
2710 parent_router_->RouteJsMessageReply(
2711 name, browser_sync::JsArgList(&return_args), sender);
2712 } else if (name == "getNotificationInfo") {
2713 if (!parent_router_) {
2714 LogNoRouter(name, args);
2715 return;
2716 }
2717 2678
2718 ListValue return_args; 2679 JsMessageHandler js_message_handler = js_message_handlers_[name];
2719 return_args.Append(NotificationInfoToValue(notification_info_map_)); 2680 if (js_message_handler.is_null()) {
2720 parent_router_->RouteJsMessageReply(
2721 name, browser_sync::JsArgList(&return_args), sender);
2722 } else if (name == "getRootNode") {
2723 if (!parent_router_) {
2724 LogNoRouter(name, args);
2725 return;
2726 }
2727 ReadTransaction trans(GetUserShare());
2728 ReadNode root(&trans);
2729 root.InitByRootLookup();
2730 ListValue return_args;
2731 return_args.Append(root.ToValue());
2732 parent_router_->RouteJsMessageReply(
2733 name, browser_sync::JsArgList(&return_args), sender);
2734 } else if (name == "getNodesById") {
2735 if (!parent_router_) {
2736 LogNoRouter(name, args);
2737 return;
2738 }
2739 parent_router_->RouteJsMessageReply(
2740 name, ProcessGetNodesByIdMessage(args), sender);
2741 } else if (name == "getChildNodeIds") {
2742 if (!parent_router_) {
2743 LogNoRouter(name, args);
2744 return;
2745 }
2746 parent_router_->RouteJsMessageReply(
2747 name, ProcessGetChildNodeIdsMessage(args), sender);
2748 } else if (name == "findNodesContainingString") {
2749 if (!parent_router_) {
2750 LogNoRouter(name, args);
2751 return;
2752 }
2753 parent_router_->RouteJsMessageReply(
2754 name, ProcessFindNodesContainingString(args), sender);
2755 } else {
2756 VLOG(1) << "Dropping unknown message " << name 2681 VLOG(1) << "Dropping unknown message " << name
2757 << " with args " << args.ToString(); 2682 << " with args " << args.ToString();
2683 return;
2758 } 2684 }
2685
2686 parent_router_->RouteJsMessageReply(
2687 name, js_message_handler.Run(args), sender);
2759 } 2688 }
2760 2689
2761 void SyncManager::SyncInternal::RouteJsEvent( 2690 void SyncManager::SyncInternal::RouteJsEvent(
2762 const std::string& name, 2691 const std::string& name,
2763 const browser_sync::JsEventDetails& details) { 2692 const browser_sync::JsEventDetails& details) {
2764 if (!parent_router_) { 2693 if (!parent_router_) {
2765 return; 2694 return;
2766 } 2695 }
2767 parent_router_->RouteJsEvent(name, details); 2696 parent_router_->RouteJsEvent(name, details);
2768 } 2697 }
2769 2698
2770 void SyncManager::SyncInternal::RouteJsMessageReply( 2699 void SyncManager::SyncInternal::RouteJsMessageReply(
2771 const std::string& name, 2700 const std::string& name,
2772 const browser_sync::JsArgList& args, 2701 const browser_sync::JsArgList& args,
2773 const browser_sync::JsEventHandler* target) { 2702 const browser_sync::JsEventHandler* target) {
2774 if (!parent_router_) { 2703 if (!parent_router_) {
2775 return; 2704 return;
2776 } 2705 }
2777 parent_router_->RouteJsMessageReply(name, args, target); 2706 parent_router_->RouteJsMessageReply(name, args, target);
2778 } 2707 }
2779 2708
2709 void SyncManager::SyncInternal::BindJsMessageHandler(
2710 const std::string& name,
2711 UnboundJsMessageHandler unbound_message_handler) {
2712 js_message_handlers_[name] =
2713 base::Bind(unbound_message_handler, base::Unretained(this));
2714 }
2715
2716 browser_sync::JsArgList
2717 SyncManager::SyncInternal::GetNotificationState(
2718 const browser_sync::JsArgList& args) {
2719 bool notifications_enabled = allstatus_.status().notifications_enabled;
2720 ListValue return_args;
2721 return_args.Append(Value::CreateBooleanValue(notifications_enabled));
2722 return browser_sync::JsArgList(&return_args);
2723 }
2724
2725 browser_sync::JsArgList
2726 SyncManager::SyncInternal::GetNotificationInfo(
2727 const browser_sync::JsArgList& args) {
2728 ListValue return_args;
2729 return_args.Append(NotificationInfoToValue(notification_info_map_));
2730 return browser_sync::JsArgList(&return_args);
2731 }
2732
2733 browser_sync::JsArgList
2734 SyncManager::SyncInternal::GetRootNodeDetails(
2735 const browser_sync::JsArgList& args) {
2736 ReadTransaction trans(GetUserShare());
2737 ReadNode root(&trans);
2738 root.InitByRootLookup();
2739 ListValue return_args;
2740 return_args.Append(root.GetDetailsAsValue());
2741 return browser_sync::JsArgList(&return_args);
2742 }
2743
2780 namespace { 2744 namespace {
2781 2745
2782 bool GetId(const ListValue& ids, int i, int64* id) { 2746 int64 GetId(const ListValue& ids, int i) {
2783 std::string id_str; 2747 std::string id_str;
2784 if (!ids.GetString(i, &id_str)) { 2748 if (!ids.GetString(i, &id_str)) {
2785 return false; 2749 return kInvalidId;
2786 } 2750 }
2787 if (!base::StringToInt64(id_str, id)) { 2751 int64 id = kInvalidId;
2788 return false; 2752 if (!base::StringToInt64(id_str, &id)) {
2753 return kInvalidId;
2789 } 2754 }
2790 if (*id == kInvalidId) { 2755 return id;
2791 return false;
2792 }
2793 return true;
2794 } 2756 }
2795 2757
2796 } // namespace 2758 browser_sync::JsArgList GetNodeInfoById(
2797 2759 const browser_sync::JsArgList& args,
2798 browser_sync::JsArgList SyncManager::SyncInternal::ProcessGetNodesByIdMessage( 2760 UserShare* user_share,
2799 const browser_sync::JsArgList& args) { 2761 DictionaryValue* (BaseNode::*info_getter)() const) {
2762 CHECK(info_getter);
2800 ListValue return_args; 2763 ListValue return_args;
2801 ListValue* nodes = new ListValue(); 2764 ListValue* node_summaries = new ListValue();
2802 return_args.Append(nodes); 2765 return_args.Append(node_summaries);
2803 ListValue* id_list = NULL; 2766 ListValue* id_list = NULL;
2804 ReadTransaction trans(GetUserShare()); 2767 ReadTransaction trans(user_share);
2805 if (args.Get().GetList(0, &id_list)) { 2768 if (args.Get().GetList(0, &id_list)) {
2769 CHECK(id_list);
2806 for (size_t i = 0; i < id_list->GetSize(); ++i) { 2770 for (size_t i = 0; i < id_list->GetSize(); ++i) {
2807 int64 id = kInvalidId; 2771 int64 id = GetId(*id_list, i);
2808 if (!GetId(*id_list, i, &id)) { 2772 if (id == kInvalidId) {
2809 continue; 2773 continue;
2810 } 2774 }
2811 ReadNode node(&trans); 2775 ReadNode node(&trans);
2812 if (!node.InitByIdLookup(id)) { 2776 if (!node.InitByIdLookup(id)) {
2813 continue; 2777 continue;
2814 } 2778 }
2815 nodes->Append(node.ToValue()); 2779 node_summaries->Append((node.*info_getter)());
2780 }
2781 }
2782 return browser_sync::JsArgList(&return_args);
2783 }
2784
2785 } // namespace
2786
2787 browser_sync::JsArgList
2788 SyncManager::SyncInternal::GetNodeSummariesById(
2789 const browser_sync::JsArgList& args) {
2790 return GetNodeInfoById(args, GetUserShare(), &BaseNode::GetSummaryAsValue);
2791 }
2792
2793 browser_sync::JsArgList
2794 SyncManager::SyncInternal::GetNodeDetailsById(
2795 const browser_sync::JsArgList& args) {
2796 return GetNodeInfoById(args, GetUserShare(), &BaseNode::GetDetailsAsValue);
2797 }
2798
2799 browser_sync::JsArgList SyncManager::SyncInternal::
2800 GetChildNodeIds(
2801 const browser_sync::JsArgList& args) {
2802 ListValue return_args;
2803 ListValue* child_ids = new ListValue();
2804 return_args.Append(child_ids);
2805 int64 id = GetId(args.Get(), 0);
2806 if (id != kInvalidId) {
2807 ReadTransaction trans(GetUserShare());
2808 syncable::Directory::ChildHandles child_handles;
2809 trans.GetLookup()->GetChildHandlesByHandle(trans.GetWrappedTrans(),
2810 id, &child_handles);
2811 for (syncable::Directory::ChildHandles::const_iterator it =
2812 child_handles.begin(); it != child_handles.end(); ++it) {
2813 child_ids->Append(Value::CreateStringValue(
2814 base::Int64ToString(*it)));
2816 } 2815 }
2817 } 2816 }
2818 return browser_sync::JsArgList(&return_args); 2817 return browser_sync::JsArgList(&return_args);
2819 } 2818 }
2820 2819
2821 browser_sync::JsArgList SyncManager::SyncInternal:: 2820 browser_sync::JsArgList SyncManager::SyncInternal::
2822 ProcessGetChildNodeIdsMessage( 2821 FindNodesContainingString(
2823 const browser_sync::JsArgList& args) { 2822 const browser_sync::JsArgList& args) {
2824 ListValue return_args;
2825 ListValue* child_ids = new ListValue();
2826 return_args.Append(child_ids);
2827 int64 id = kInvalidId;
2828 if (GetId(args.Get(), 0, &id)) {
2829 ReadTransaction trans(GetUserShare());
2830 ReadNode node(&trans);
2831 if (node.InitByIdLookup(id)) {
2832 int64 child_id = node.GetFirstChildId();
2833 while (child_id != kInvalidId) {
2834 ReadNode child_node(&trans);
2835 if (!child_node.InitByIdLookup(child_id)) {
2836 break;
2837 }
2838 child_ids->Append(Value::CreateStringValue(
2839 base::Int64ToString(child_id)));
2840 child_id = child_node.GetSuccessorId();
2841 }
2842 }
2843 }
2844 return browser_sync::JsArgList(&return_args);
2845 }
2846
2847 browser_sync::JsArgList SyncManager::SyncInternal::
2848 ProcessFindNodesContainingString(
2849 const browser_sync::JsArgList& args) {
2850 std::string query; 2823 std::string query;
2851 ListValue return_args; 2824 ListValue return_args;
2852 if (!args.Get().GetString(0, &query)) { 2825 if (!args.Get().GetString(0, &query)) {
2853 return_args.Append(new ListValue()); 2826 return_args.Append(new ListValue());
2854 return browser_sync::JsArgList(&return_args); 2827 return browser_sync::JsArgList(&return_args);
2855 } 2828 }
2856 2829
2857 ListValue* result = FindNodesContainingString(query); 2830 // Convert the query string to lower case to perform case insensitive
2831 // searches.
2832 std::string lowercase_query = query;
2833 StringToLowerASCII(&lowercase_query);
2834
2835 ListValue* result = new ListValue();
2858 return_args.Append(result); 2836 return_args.Append(result);
2837
2838 ReadTransaction trans(GetUserShare());
2839 std::vector<const syncable::EntryKernel*> entry_kernels;
2840 trans.GetLookup()->GetAllEntryKernels(trans.GetWrappedTrans(),
2841 &entry_kernels);
2842
2843 for (std::vector<const syncable::EntryKernel*>::const_iterator it =
2844 entry_kernels.begin(); it != entry_kernels.end(); ++it) {
2845 if ((*it)->ContainsString(lowercase_query)) {
2846 result->Append(new StringValue(base::Int64ToString(
2847 (*it)->ref(syncable::META_HANDLE))));
2848 }
2849 }
2850
2859 return browser_sync::JsArgList(&return_args); 2851 return browser_sync::JsArgList(&return_args);
2860 } 2852 }
2861 2853
2862 void SyncManager::SyncInternal::OnNotificationStateChange( 2854 void SyncManager::SyncInternal::OnNotificationStateChange(
2863 bool notifications_enabled) { 2855 bool notifications_enabled) {
2864 VLOG(1) << "P2P: Notifications enabled = " 2856 VLOG(1) << "P2P: Notifications enabled = "
2865 << (notifications_enabled ? "true" : "false"); 2857 << (notifications_enabled ? "true" : "false");
2866 allstatus_.SetNotificationsEnabled(notifications_enabled); 2858 allstatus_.SetNotificationsEnabled(notifications_enabled);
2867 if (syncer_thread()) { 2859 if (syncer_thread()) {
2868 syncer_thread()->set_notifications_enabled(notifications_enabled); 2860 syncer_thread()->set_notifications_enabled(notifications_enabled);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
2995 void SyncManager::LogUnsyncedItems(int level) const { 2987 void SyncManager::LogUnsyncedItems(int level) const {
2996 std::vector<int64> unsynced_handles; 2988 std::vector<int64> unsynced_handles;
2997 sync_api::ReadTransaction trans(GetUserShare()); 2989 sync_api::ReadTransaction trans(GetUserShare());
2998 trans.GetWrappedTrans()->directory()->GetUnsyncedMetaHandles( 2990 trans.GetWrappedTrans()->directory()->GetUnsyncedMetaHandles(
2999 trans.GetWrappedTrans(), &unsynced_handles); 2991 trans.GetWrappedTrans(), &unsynced_handles);
3000 2992
3001 for (std::vector<int64>::const_iterator it = unsynced_handles.begin(); 2993 for (std::vector<int64>::const_iterator it = unsynced_handles.begin();
3002 it != unsynced_handles.end(); ++it) { 2994 it != unsynced_handles.end(); ++it) {
3003 ReadNode node(&trans); 2995 ReadNode node(&trans);
3004 if (node.InitByIdLookup(*it)) { 2996 if (node.InitByIdLookup(*it)) {
3005 scoped_ptr<DictionaryValue> value(node.ToValue()); 2997 scoped_ptr<DictionaryValue> value(node.GetDetailsAsValue());
3006 std::string info; 2998 std::string info;
3007 base::JSONWriter::Write(value.get(), true, &info); 2999 base::JSONWriter::Write(value.get(), true, &info);
3008 VLOG(level) << info; 3000 VLOG(level) << info;
3009 } 3001 }
3010 } 3002 }
3011 } 3003 }
3012 3004
3013 void SyncManager::TriggerOnNotificationStateChangeForTest( 3005 void SyncManager::TriggerOnNotificationStateChangeForTest(
3014 bool notifications_enabled) { 3006 bool notifications_enabled) {
3015 data_->OnNotificationStateChange(notifications_enabled); 3007 data_->OnNotificationStateChange(notifications_enabled);
3016 } 3008 }
3017 3009
3018 void SyncManager::TriggerOnIncomingNotificationForTest( 3010 void SyncManager::TriggerOnIncomingNotificationForTest(
3019 const syncable::ModelTypeBitSet& model_types) { 3011 const syncable::ModelTypeBitSet& model_types) {
3020 syncable::ModelTypePayloadMap model_types_with_payloads = 3012 syncable::ModelTypePayloadMap model_types_with_payloads =
3021 syncable::ModelTypePayloadMapFromBitSet(model_types, 3013 syncable::ModelTypePayloadMapFromBitSet(model_types,
3022 std::string()); 3014 std::string());
3023 3015
3024 data_->OnIncomingNotification(model_types_with_payloads); 3016 data_->OnIncomingNotification(model_types_with_payloads);
3025 } 3017 }
3026 3018
3027 } // namespace sync_api 3019 } // namespace sync_api
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/syncapi.h ('k') | chrome/browser/sync/engine/syncapi_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698