added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / sync / sync_config.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2017 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include "sync/sync_config.hpp"
20
21 #include "sync/sync_manager.hpp"
22
23 #include <realm/sync/crypto.hpp>
24
25 namespace realm {
26
27 namespace {
28
29 // Construct an identifier for this partially synced Realm by combining client and user identifiers.
30 std::string partial_sync_identifier(const SyncUser& user)
31 {
32     std::string raw_identifier = SyncManager::shared().client_uuid() + "/" + user.local_identity();
33     uint8_t identifier[20];
34     sync::crypto::sha1(raw_identifier.data(), raw_identifier.size(), (char *)&identifier[0]);
35
36     std::stringstream ss;
37     ss << std::hex << std::setfill('0');
38     for (uint8_t c : identifier)
39         ss << std::setw(2) << (unsigned)c;
40     return ss.str();
41 }
42
43 } // unnamed namespace
44
45 std::string SyncConfig::realm_url() const
46 {
47     REALM_ASSERT(reference_realm_url.length() > 0);
48     REALM_ASSERT(user);
49
50     if (!is_partial)
51         return reference_realm_url;
52
53     std::string base_url = reference_realm_url;
54     if (base_url.back() == '/')
55         base_url.pop_back();
56
57     if (custom_partial_sync_identifier)
58         return base_url + "/__partial/" + *custom_partial_sync_identifier;
59
60     return base_url + "/__partial/" + partial_sync_identifier(*user);
61 }
62
63 } // namespace realm