OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/sync/model/model_type_debug_info.h" | 5 #include "components/sync/model/model_type_debug_info.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "components/sync/model_impl/processor_entity_tracker.h" | 12 #include "components/sync/model_impl/processor_entity_tracker.h" |
13 #include "components/sync/protocol/proto_value_conversions.h" | 13 #include "components/sync/protocol/proto_value_conversions.h" |
14 | 14 |
| 15 using base::DictionaryValue; |
| 16 using base::ListValue; |
| 17 |
15 namespace syncer { | 18 namespace syncer { |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 SharedModelTypeProcessor* GetProcessorFromBridge( | 22 SharedModelTypeProcessor* GetProcessorFromBridge(ModelTypeSyncBridge* bridge) { |
20 const base::WeakPtr<ModelTypeSyncBridge>& bridge) { | 23 ModelTypeChangeProcessor* processor = bridge->change_processor(); |
21 if (!bridge.get()) { | 24 if (processor == nullptr) { |
22 LOG(WARNING) | |
23 << "ModelTypeSyncBridge destroyed before debug info was retrieved."; | |
24 return nullptr; | |
25 } | |
26 SharedModelTypeProcessor* processor = | |
27 static_cast<SharedModelTypeProcessor*>(bridge->change_processor()); | |
28 if (!processor) { | |
29 LOG(WARNING) << "SharedModelTypeProcessor destroyed before debug info was " | 25 LOG(WARNING) << "SharedModelTypeProcessor destroyed before debug info was " |
30 "retrieved."; | 26 "retrieved."; |
31 return nullptr; | |
32 } | 27 } |
33 return processor; | 28 return static_cast<SharedModelTypeProcessor*>(processor); |
34 } | 29 } |
35 | 30 |
36 } // namespace | 31 } // namespace |
37 | 32 |
38 // static | 33 // static |
39 void ModelTypeDebugInfo::GetAllNodes( | 34 void ModelTypeDebugInfo::GetAllNodes( |
40 const base::WeakPtr<ModelTypeSyncBridge>& bridge, | 35 const base::Callback<void(const ModelType, std::unique_ptr<ListValue>)>& |
41 const base::Callback<void(const ModelType, | 36 callback, |
42 std::unique_ptr<base::ListValue>)>& callback) { | 37 ModelTypeSyncBridge* bridge) { |
43 SharedModelTypeProcessor* processor = GetProcessorFromBridge(bridge); | 38 SharedModelTypeProcessor* processor = GetProcessorFromBridge(bridge); |
44 if (processor) { | 39 if (processor) { |
45 bridge->GetAllData(base::Bind(&ModelTypeDebugInfo::MergeDataWithMetadata, | 40 bridge->GetAllData(base::Bind(&ModelTypeDebugInfo::MergeDataWithMetadata, |
46 base::Unretained(processor), callback)); | 41 base::Unretained(processor), callback)); |
47 } | 42 } |
48 } | 43 } |
49 | 44 |
50 // static | 45 // static |
51 void ModelTypeDebugInfo::GetStatusCounters( | 46 void ModelTypeDebugInfo::GetStatusCounters( |
52 const base::WeakPtr<ModelTypeSyncBridge>& bridge, | 47 const base::Callback<void(ModelType, const StatusCounters&)>& callback, |
53 const base::Callback<void(ModelType, const StatusCounters&)>& callback) { | 48 ModelTypeSyncBridge* bridge) { |
54 SharedModelTypeProcessor* processor = GetProcessorFromBridge(bridge); | 49 SharedModelTypeProcessor* processor = GetProcessorFromBridge(bridge); |
55 if (processor) { | 50 if (processor) { |
56 syncer::StatusCounters counters; | 51 StatusCounters counters; |
57 counters.num_entries_and_tombstones = processor->entities_.size(); | 52 counters.num_entries_and_tombstones = processor->entities_.size(); |
58 for (const auto& kv : processor->entities_) { | 53 for (const auto& kv : processor->entities_) { |
59 if (!kv.second->metadata().is_deleted()) { | 54 if (!kv.second->metadata().is_deleted()) { |
60 ++counters.num_entries; | 55 ++counters.num_entries; |
61 } | 56 } |
62 } | 57 } |
63 callback.Run(processor->type_, counters); | 58 callback.Run(processor->type_, counters); |
64 } | 59 } |
65 } | 60 } |
66 | 61 |
67 ModelTypeDebugInfo::ModelTypeDebugInfo() {} | 62 ModelTypeDebugInfo::ModelTypeDebugInfo() {} |
68 | 63 |
69 // static | 64 // static |
70 void ModelTypeDebugInfo::MergeDataWithMetadata( | 65 void ModelTypeDebugInfo::MergeDataWithMetadata( |
71 SharedModelTypeProcessor* processor, | 66 SharedModelTypeProcessor* processor, |
72 const base::Callback<void(const ModelType, | 67 const base::Callback<void(const ModelType, std::unique_ptr<ListValue>)>& |
73 std::unique_ptr<base::ListValue>)>& callback, | 68 callback, |
74 std::unique_ptr<DataBatch> batch) { | 69 std::unique_ptr<DataBatch> batch) { |
75 std::unique_ptr<base::ListValue> all_nodes = | 70 std::unique_ptr<ListValue> all_nodes = base::MakeUnique<ListValue>(); |
76 base::MakeUnique<base::ListValue>(); | |
77 std::string type_string = ModelTypeToString(processor->type_); | 71 std::string type_string = ModelTypeToString(processor->type_); |
78 | 72 |
79 while (batch->HasNext()) { | 73 while (batch->HasNext()) { |
80 KeyAndData data = batch->Next(); | 74 KeyAndData data = batch->Next(); |
81 std::unique_ptr<base::DictionaryValue> node = | 75 std::unique_ptr<DictionaryValue> node = data.second->ToDictionaryValue(); |
82 data.second->ToDictionaryValue(); | |
83 ProcessorEntityTracker* entity = | 76 ProcessorEntityTracker* entity = |
84 processor->GetEntityForStorageKey(data.first); | 77 processor->GetEntityForStorageKey(data.first); |
85 // Entity could be null if there are some unapplied changes. | 78 // Entity could be null if there are some unapplied changes. |
86 if (entity != nullptr) { | 79 if (entity != nullptr) { |
87 node->Set("metadata", EntityMetadataToValue(entity->metadata())); | 80 node->Set("metadata", EntityMetadataToValue(entity->metadata())); |
88 } | 81 } |
89 node->SetString("modelType", type_string); | 82 node->SetString("modelType", type_string); |
90 all_nodes->Append(std::move(node)); | 83 all_nodes->Append(std::move(node)); |
91 } | 84 } |
92 | 85 |
93 // Create a permanent folder for this data type. Since sync server no longer | 86 // Create a permanent folder for this data type. Since sync server no longer |
94 // create root folders, and USS won't migrate root folders from directory, we | 87 // create root folders, and USS won't migrate root folders from directory, we |
95 // create root folders for each data type here. | 88 // create root folders for each data type here. |
96 std::unique_ptr<base::DictionaryValue> rootnode = | 89 std::unique_ptr<DictionaryValue> rootnode = |
97 base::MakeUnique<base::DictionaryValue>(); | 90 base::MakeUnique<DictionaryValue>(); |
98 // Function isTypeRootNode in sync_node_browser.js use PARENT_ID and | 91 // Function isTypeRootNode in sync_node_browser.js use PARENT_ID and |
99 // UNIQUE_SERVER_TAG to check if the node is root node. isChildOf in | 92 // UNIQUE_SERVER_TAG to check if the node is root node. isChildOf in |
100 // sync_node_browser.js uses modelType to check if root node is parent of real | 93 // sync_node_browser.js uses modelType to check if root node is parent of real |
101 // data node. NON_UNIQUE_NAME will be the name of node to display. | 94 // data node. NON_UNIQUE_NAME will be the name of node to display. |
102 rootnode->SetString("PARENT_ID", "r"); | 95 rootnode->SetString("PARENT_ID", "r"); |
103 rootnode->SetString("UNIQUE_SERVER_TAG", type_string); | 96 rootnode->SetString("UNIQUE_SERVER_TAG", type_string); |
104 rootnode->SetBoolean("IS_DIR", true); | 97 rootnode->SetBoolean("IS_DIR", true); |
105 rootnode->SetString("modelType", type_string); | 98 rootnode->SetString("modelType", type_string); |
106 rootnode->SetString("NON_UNIQUE_NAME", type_string); | 99 rootnode->SetString("NON_UNIQUE_NAME", type_string); |
107 all_nodes->Append(std::move(rootnode)); | 100 all_nodes->Append(std::move(rootnode)); |
108 | 101 |
109 callback.Run(processor->type_, std::move(all_nodes)); | 102 callback.Run(processor->type_, std::move(all_nodes)); |
110 } | 103 } |
111 | 104 |
112 } // namespace syncer | 105 } // namespace syncer |
OLD | NEW |