OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 <utility> |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/time/time.h" |
| 9 #include "components/download/internal/proto_conversions.h" |
| 10 #include "net/http/http_request_headers.h" |
| 11 |
| 12 namespace download { |
| 13 |
| 14 protodb::Entry_State ProtoConversions::RequestStateToProto(Entry::State state) { |
| 15 switch (state) { |
| 16 case Entry::State::NEW: |
| 17 return protodb::Entry_State_NEW; |
| 18 case Entry::State::AVAILABLE: |
| 19 return protodb::Entry_State_AVAILABLE; |
| 20 case Entry::State::ACTIVE: |
| 21 return protodb::Entry_State_ACTIVE; |
| 22 case Entry::State::PAUSED: |
| 23 return protodb::Entry_State_PAUSED; |
| 24 case Entry::State::COMPLETE: |
| 25 return protodb::Entry_State_COMPLETE; |
| 26 case Entry::State::WATCHDOG: |
| 27 return protodb::Entry_State_WATCHDOG; |
| 28 } |
| 29 |
| 30 NOTREACHED(); |
| 31 return protodb::Entry_State_NEW; |
| 32 } |
| 33 |
| 34 Entry::State ProtoConversions::RequestStateFromProto( |
| 35 protodb::Entry_State state) { |
| 36 switch (state) { |
| 37 case protodb::Entry_State_NEW: |
| 38 return Entry::State::NEW; |
| 39 case protodb::Entry_State_AVAILABLE: |
| 40 return Entry::State::AVAILABLE; |
| 41 case protodb::Entry_State_ACTIVE: |
| 42 return Entry::State::ACTIVE; |
| 43 case protodb::Entry_State_PAUSED: |
| 44 return Entry::State::PAUSED; |
| 45 case protodb::Entry_State_COMPLETE: |
| 46 return Entry::State::COMPLETE; |
| 47 case protodb::Entry_State_WATCHDOG: |
| 48 return Entry::State::WATCHDOG; |
| 49 } |
| 50 |
| 51 NOTREACHED(); |
| 52 return Entry::State::NEW; |
| 53 } |
| 54 |
| 55 protodb::DownloadClient ProtoConversions::DownloadClientToProto( |
| 56 DownloadClient client) { |
| 57 switch (client) { |
| 58 case DownloadClient::INVALID: |
| 59 return protodb::DownloadClient::INVALID; |
| 60 case DownloadClient::TEST: |
| 61 return protodb::DownloadClient::TEST; |
| 62 case DownloadClient::OFFLINE_PAGE_PREFETCH: |
| 63 return protodb::DownloadClient::OFFLINE_PAGE_PREFETCH; |
| 64 case DownloadClient::BOUNDARY: |
| 65 return protodb::DownloadClient::BOUNDARY; |
| 66 } |
| 67 |
| 68 NOTREACHED(); |
| 69 return protodb::DownloadClient::INVALID; |
| 70 } |
| 71 |
| 72 DownloadClient ProtoConversions::DownloadClientFromProto( |
| 73 protodb::DownloadClient client) { |
| 74 switch (client) { |
| 75 case protodb::DownloadClient::INVALID: |
| 76 return DownloadClient::INVALID; |
| 77 case protodb::DownloadClient::TEST: |
| 78 return DownloadClient::TEST; |
| 79 case protodb::DownloadClient::OFFLINE_PAGE_PREFETCH: |
| 80 return DownloadClient::OFFLINE_PAGE_PREFETCH; |
| 81 case protodb::DownloadClient::BOUNDARY: |
| 82 return DownloadClient::BOUNDARY; |
| 83 } |
| 84 |
| 85 NOTREACHED(); |
| 86 return DownloadClient::INVALID; |
| 87 } |
| 88 |
| 89 SchedulingParams::NetworkRequirements |
| 90 ProtoConversions::NetworkRequirementsFromProto( |
| 91 protodb::SchedulingParams_NetworkRequirements network_requirements) { |
| 92 switch (network_requirements) { |
| 93 case protodb::SchedulingParams_NetworkRequirements_NONE: |
| 94 return SchedulingParams::NetworkRequirements::NONE; |
| 95 case protodb::SchedulingParams_NetworkRequirements_OPTIMISTIC: |
| 96 return SchedulingParams::NetworkRequirements::OPTIMISTIC; |
| 97 case protodb::SchedulingParams_NetworkRequirements_UNMETERED: |
| 98 return SchedulingParams::NetworkRequirements::UNMETERED; |
| 99 } |
| 100 |
| 101 NOTREACHED(); |
| 102 return SchedulingParams::NetworkRequirements::NONE; |
| 103 } |
| 104 |
| 105 protodb::SchedulingParams_NetworkRequirements |
| 106 ProtoConversions::NetworkRequirementsToProto( |
| 107 SchedulingParams::NetworkRequirements network_requirements) { |
| 108 switch (network_requirements) { |
| 109 case SchedulingParams::NetworkRequirements::NONE: |
| 110 return protodb::SchedulingParams_NetworkRequirements_NONE; |
| 111 case SchedulingParams::NetworkRequirements::OPTIMISTIC: |
| 112 return protodb::SchedulingParams_NetworkRequirements_OPTIMISTIC; |
| 113 case SchedulingParams::NetworkRequirements::UNMETERED: |
| 114 return protodb::SchedulingParams_NetworkRequirements_UNMETERED; |
| 115 } |
| 116 |
| 117 NOTREACHED(); |
| 118 return protodb::SchedulingParams_NetworkRequirements_NONE; |
| 119 } |
| 120 |
| 121 SchedulingParams::BatteryRequirements |
| 122 ProtoConversions::BatteryRequirementsFromProto( |
| 123 protodb::SchedulingParams_BatteryRequirements battery_requirements) { |
| 124 switch (battery_requirements) { |
| 125 case protodb::SchedulingParams_BatteryRequirements_BATTERY_INSENSITIVE: |
| 126 return SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE; |
| 127 case protodb::SchedulingParams_BatteryRequirements_BATTERY_SENSITIVE: |
| 128 return SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE; |
| 129 } |
| 130 |
| 131 NOTREACHED(); |
| 132 return SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE; |
| 133 } |
| 134 |
| 135 protodb::SchedulingParams_BatteryRequirements |
| 136 ProtoConversions::BatteryRequirementsToProto( |
| 137 SchedulingParams::BatteryRequirements battery_requirements) { |
| 138 switch (battery_requirements) { |
| 139 case SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE: |
| 140 return protodb::SchedulingParams_BatteryRequirements_BATTERY_INSENSITIVE; |
| 141 case SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE: |
| 142 return protodb::SchedulingParams_BatteryRequirements_BATTERY_SENSITIVE; |
| 143 } |
| 144 |
| 145 NOTREACHED(); |
| 146 return protodb::SchedulingParams_BatteryRequirements_BATTERY_INSENSITIVE; |
| 147 } |
| 148 |
| 149 SchedulingParams::Priority ProtoConversions::SchedulingPriorityFromProto( |
| 150 protodb::SchedulingParams_Priority priority) { |
| 151 switch (priority) { |
| 152 case protodb::SchedulingParams_Priority_LOW: |
| 153 return SchedulingParams::Priority::LOW; |
| 154 case protodb::SchedulingParams_Priority_NORMAL: |
| 155 return SchedulingParams::Priority::NORMAL; |
| 156 case protodb::SchedulingParams_Priority_HIGH: |
| 157 return SchedulingParams::Priority::HIGH; |
| 158 case protodb::SchedulingParams_Priority_UI: |
| 159 return SchedulingParams::Priority::UI; |
| 160 } |
| 161 |
| 162 NOTREACHED(); |
| 163 return SchedulingParams::Priority::LOW; |
| 164 } |
| 165 |
| 166 protodb::SchedulingParams_Priority ProtoConversions::SchedulingPriorityToProto( |
| 167 SchedulingParams::Priority priority) { |
| 168 switch (priority) { |
| 169 case SchedulingParams::Priority::LOW: |
| 170 return protodb::SchedulingParams_Priority_LOW; |
| 171 case SchedulingParams::Priority::NORMAL: |
| 172 return protodb::SchedulingParams_Priority_NORMAL; |
| 173 case SchedulingParams::Priority::HIGH: |
| 174 return protodb::SchedulingParams_Priority_HIGH; |
| 175 case SchedulingParams::Priority::UI: |
| 176 return protodb::SchedulingParams_Priority_UI; |
| 177 } |
| 178 |
| 179 NOTREACHED(); |
| 180 return protodb::SchedulingParams_Priority_LOW; |
| 181 } |
| 182 |
| 183 SchedulingParams ProtoConversions::SchedulingParamsFromProto( |
| 184 const protodb::SchedulingParams& proto) { |
| 185 SchedulingParams scheduling_params; |
| 186 |
| 187 scheduling_params.cancel_time = |
| 188 base::Time::FromInternalValue(proto.cancel_time()); |
| 189 scheduling_params.priority = SchedulingPriorityFromProto(proto.priority()); |
| 190 scheduling_params.network_requirements = |
| 191 NetworkRequirementsFromProto(proto.network_requirements()); |
| 192 scheduling_params.battery_requirements = |
| 193 BatteryRequirementsFromProto(proto.battery_requirements()); |
| 194 |
| 195 return scheduling_params; |
| 196 } |
| 197 |
| 198 void ProtoConversions::SchedulingParamsToProto( |
| 199 const SchedulingParams& scheduling_params, |
| 200 protodb::SchedulingParams* proto) { |
| 201 proto->set_cancel_time(scheduling_params.cancel_time.ToInternalValue()); |
| 202 proto->set_priority(SchedulingPriorityToProto(scheduling_params.priority)); |
| 203 proto->set_network_requirements( |
| 204 NetworkRequirementsToProto(scheduling_params.network_requirements)); |
| 205 proto->set_battery_requirements( |
| 206 BatteryRequirementsToProto(scheduling_params.battery_requirements)); |
| 207 } |
| 208 |
| 209 RequestParams ProtoConversions::RequestParamsFromProto( |
| 210 const protodb::RequestParams& proto) { |
| 211 RequestParams request_params; |
| 212 request_params.url = GURL(proto.url()); |
| 213 request_params.method = proto.method(); |
| 214 |
| 215 for (int i = 0; i < proto.headers_size(); i++) { |
| 216 protodb::RequestHeader header = proto.headers(i); |
| 217 request_params.request_headers.SetHeader(header.key(), header.value()); |
| 218 } |
| 219 |
| 220 return request_params; |
| 221 } |
| 222 |
| 223 void ProtoConversions::RequestParamsToProto(const RequestParams& request_params, |
| 224 protodb::RequestParams* proto) { |
| 225 proto->set_url(request_params.url.spec()); |
| 226 proto->set_method(request_params.method); |
| 227 |
| 228 int i = 0; |
| 229 net::HttpRequestHeaders::Iterator iter(request_params.request_headers); |
| 230 while (iter.GetNext()) { |
| 231 protodb::RequestHeader* header = proto->add_headers(); |
| 232 header->set_key(iter.name()); |
| 233 header->set_value(iter.value()); |
| 234 i++; |
| 235 } |
| 236 } |
| 237 |
| 238 Entry ProtoConversions::EntryFromProto(const protodb::Entry& proto) { |
| 239 Entry entry; |
| 240 |
| 241 entry.guid = proto.guid(); |
| 242 entry.client = DownloadClientFromProto(proto.name_space()); |
| 243 entry.scheduling_params = |
| 244 SchedulingParamsFromProto(proto.scheduling_params()); |
| 245 entry.request_params = RequestParamsFromProto(proto.request_params()); |
| 246 entry.state = RequestStateFromProto(proto.state()); |
| 247 |
| 248 return entry; |
| 249 } |
| 250 |
| 251 protodb::Entry ProtoConversions::EntryToProto(const Entry& entry) { |
| 252 protodb::Entry proto; |
| 253 |
| 254 proto.set_guid(entry.guid); |
| 255 proto.set_name_space(DownloadClientToProto(entry.client)); |
| 256 SchedulingParamsToProto(entry.scheduling_params, |
| 257 proto.mutable_scheduling_params()); |
| 258 RequestParamsToProto(entry.request_params, proto.mutable_request_params()); |
| 259 proto.set_state(RequestStateToProto(entry.state)); |
| 260 |
| 261 return proto; |
| 262 } |
| 263 |
| 264 std::unique_ptr<std::vector<Entry>> ProtoConversions::EntryVectorFromProto( |
| 265 std::unique_ptr<std::vector<protodb::Entry>> protos) { |
| 266 auto entries = base::MakeUnique<std::vector<Entry>>(); |
| 267 for (auto& proto : *protos) { |
| 268 entries->push_back(EntryFromProto(proto)); |
| 269 } |
| 270 |
| 271 return entries; |
| 272 } |
| 273 |
| 274 std::unique_ptr<std::vector<protodb::Entry>> |
| 275 ProtoConversions::EntryVectorToProto( |
| 276 std::unique_ptr<std::vector<Entry>> entries) { |
| 277 auto protos = base::MakeUnique<std::vector<protodb::Entry>>(); |
| 278 for (auto& entry : *entries) { |
| 279 protos->push_back(EntryToProto(entry)); |
| 280 } |
| 281 |
| 282 return protos; |
| 283 } |
| 284 |
| 285 } // namespace download |
OLD | NEW |