diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 4e09eacc95..8eb0376753 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -27,6 +27,7 @@ #include "platform/local_country_file_utils.hpp" #include "platform/location.hpp" #include "platform/measurement_utils.hpp" +#include "platform/network_policy.hpp" #include "platform/platform.hpp" #include "platform/preferred_languages.hpp" #include "platform/settings.hpp" @@ -37,9 +38,19 @@ android::Framework * g_framework = 0; +namespace platform +{ +// Dummy, implementation and location should be chosen by android developer. +NetworkPolicy ToNativeNetworkPolicy(jobject obj) +{ + return NetworkPolicy(true); +} +} // namespace platform + using namespace storage; using platform::CountryFile; using platform::LocalCountryFile; +using platform::ToNativeNetworkPolicy; static_assert(sizeof(int) >= 4, "Size of jint in less than 4 bytes."); @@ -473,13 +484,21 @@ place_page::Info & Framework::GetPlacePageInfo() void Framework::RequestBookingMinPrice(string const & hotelId, string const & currencyCode, function const & callback) { - return m_work.GetBookingApi().GetMinPrice(hotelId, currencyCode, callback); + // Stub obj must be changed. + jobject obj; + auto const bookingApi = m_work.GetBookingApi(ToNativeNetworkPolicy(obj)); + if (bookingApi) + bookingApi->GetMinPrice(hotelId, currencyCode, callback); } void Framework::RequestBookingInfo(string const & hotelId, string const & lang, function const & callback) { - return m_work.GetBookingApi().GetHotelInfo(hotelId, lang, callback); + // Stub obj must be changed. + jobject obj; + auto const bookingApi = m_work.GetBookingApi(ToNativeNetworkPolicy(obj)); + if (bookingApi) + bookingApi->GetHotelInfo(hotelId, lang, callback); } bool Framework::HasSpaceForMigration() @@ -517,7 +536,13 @@ uint64_t Framework::RequestUberProducts(ms::LatLon const & from, ms::LatLon cons uber::ProductsCallback const & callback, uber::ErrorCallback const & errorCallback) { - return m_work.GetUberApi().GetAvailableProducts(from, to, callback, errorCallback); + // Stub obj must be changed. + jobject obj; + auto const uberApi = m_work.GetUberApi(ToNativeNetworkPolicy(obj)); + if (!uberApi) + return 0; + + return uberApi->GetAvailableProducts(from, to, callback, errorCallback); } uber::RideRequestLinks Framework::GetUberLinks(string const & productId, ms::LatLon const & from, ms::LatLon const & to) diff --git a/iphone/Maps/Classes/MWMPlacePageData.mm b/iphone/Maps/Classes/MWMPlacePageData.mm index 7474ae9bf3..bd9de153fd 100644 --- a/iphone/Maps/Classes/MWMPlacePageData.mm +++ b/iphone/Maps/Classes/MWMPlacePageData.mm @@ -2,6 +2,8 @@ #include "Framework.h" +#include "platform/network_policy.hpp" + #include "base/string_utils.hpp" #include "3party/opening_hours/opening_hours.hpp" @@ -13,6 +15,7 @@ NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS"; } // namespace using namespace place_page; +using platform::MakeNetworkPolicyIos; @implementation MWMPlacePageData { @@ -257,7 +260,7 @@ using namespace place_page; currencyFormatter.maximumFractionDigits = 0; string const currency = currencyFormatter.currencyCode.UTF8String; - auto & api = GetFramework().GetBookingApi(); + auto const api = GetFramework().GetBookingApi(MakeNetworkPolicyIos(true)); auto const func = [self, label, currency, currencyFormatter](string const & minPrice, string const & priceCurrency) { @@ -284,7 +287,8 @@ using namespace place_page; }); }; - api.GetMinPrice(self.sponsoredId.UTF8String, currency, func); + if (api) + api->GetMinPrice(self.sponsoredId.UTF8String, currency, func); } - (NSString *)address { return @(m_info.GetAddress().c_str()); } diff --git a/iphone/Maps/Classes/MWMPlacePageEntity.mm b/iphone/Maps/Classes/MWMPlacePageEntity.mm index 3f9790512a..d3c099f052 100644 --- a/iphone/Maps/Classes/MWMPlacePageEntity.mm +++ b/iphone/Maps/Classes/MWMPlacePageEntity.mm @@ -9,9 +9,11 @@ #include "platform/measurement_utils.hpp" #include "platform/mwm_version.hpp" +#include "platform/network_policy.hpp" #include "platform/platform.hpp" using feature::Metadata; +using platform::MakeNetworkPolicyIos; static NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS"; @@ -131,7 +133,9 @@ void initFieldsMap() currencyFormatter.numberStyle = NSNumberFormatterCurrencyStyle; currencyFormatter.maximumFractionDigits = 0; string const currency = currencyFormatter.currencyCode.UTF8String; - GetFramework().GetBookingApi().GetMinPrice( + auto const api = GetFramework().GetBookingApi(MakeNetworkPolicyIos(true)); + if (api) + api->GetMinPrice( m_info.GetMetadata().Get(Metadata::FMD_SPONSORED_ID), currency, [self, completion, failure, currency, currencyFormatter](string const & minPrice, string const & priceCurrency) { diff --git a/map/framework.cpp b/map/framework.cpp index 6a7b95527b..dbc5adf380 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -59,6 +59,7 @@ #include "platform/local_country_file_utils.hpp" #include "platform/measurement_utils.hpp" #include "platform/mwm_version.hpp" +#include "platform/network_policy.hpp" #include "platform/platform.hpp" #include "platform/preferred_languages.hpp" #include "platform/settings.hpp" @@ -447,6 +448,30 @@ Framework::~Framework() m_model.SetOnMapDeregisteredCallback(nullptr); } +BookingApi * Framework::GetBookingApi(platform::NetworkPolicy const & policy) +{ + if (policy.CanUse()) + return m_bookingApi.get(); + + return nullptr; +} + +BookingApi const * Framework::GetBookingApi(platform::NetworkPolicy const & policy) const +{ + if (policy.CanUse()) + return m_bookingApi.get(); + + return nullptr; +} + +uber::Api * Framework::GetUberApi(platform::NetworkPolicy const & policy) +{ + if (policy.CanUse()) + return m_uberApi.get(); + + return nullptr; +} + void Framework::DrawWatchFrame(m2::PointD const & center, int zoomModifier, uint32_t pxWidth, uint32_t pxHeight, df::watch::FrameSymbols const & symbols, @@ -795,10 +820,11 @@ void Framework::FillInfoFromFeatureType(FeatureType const & ft, place_page::Info if (ftypes::IsBookingChecker::Instance()(ft)) { + ASSERT(m_bookingApi, ()); info.m_sponsoredType = SponsoredType::Booking; auto const & baseUrl = info.GetMetadata().Get(feature::Metadata::FMD_WEBSITE); - info.m_sponsoredUrl = GetBookingApi().GetBookHotelUrl(baseUrl); - info.m_sponsoredDescriptionUrl = GetBookingApi().GetDescriptionUrl(baseUrl); + info.m_sponsoredUrl = m_bookingApi->GetBookHotelUrl(baseUrl); + info.m_sponsoredDescriptionUrl = m_bookingApi->GetDescriptionUrl(baseUrl); } else if (ftypes::IsOpentableChecker::Instance()(ft)) { diff --git a/map/framework.hpp b/map/framework.hpp index 338eacc328..33d31f9880 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -91,6 +91,11 @@ namespace df } } +namespace platform +{ +class NetworkPolicy; +} + /// Uncomment line to make fixed position settings and /// build version for screenshots. //#define FIXED_LOCATION @@ -160,9 +165,8 @@ protected: BookmarkManager m_bmManager; - BookingApi m_bookingApi; - - uber::Api m_uberApi; + unique_ptr m_bookingApi = make_unique(); + unique_ptr m_uberApi = make_unique(); df::DrapeApi m_drapeApi; @@ -195,13 +199,12 @@ public: virtual ~Framework(); /// Get access to booking api helpers - BookingApi & GetBookingApi() { return m_bookingApi; } - BookingApi const & GetBookingApi() const { return m_bookingApi; } + BookingApi * GetBookingApi(platform::NetworkPolicy const & policy); + BookingApi const * GetBookingApi(platform::NetworkPolicy const & policy) const; + uber::Api * GetUberApi(platform::NetworkPolicy const & policy); df::DrapeApi & GetDrapeApi() { return m_drapeApi; } - uber::Api & GetUberApi() { return m_uberApi;} - /// Migrate to new version of very different data. bool IsEnoughSpaceForMigrate() const; storage::TCountryId PreMigrate(ms::LatLon const & position, storage::Storage::TChangeCountryFunction const & change, diff --git a/platform/network_policy.hpp b/platform/network_policy.hpp new file mode 100644 index 0000000000..a1e64112a2 --- /dev/null +++ b/platform/network_policy.hpp @@ -0,0 +1,30 @@ +#pragma once + +class _jobject; +typedef _jobject * jobject; + +namespace platform +{ +/// Class that is used to allow or disallow remote calls. +class NetworkPolicy +{ + // Maker for android. + friend NetworkPolicy ToNativeNetworkPolicy(jobject obj); + // Maker for ios. + // Dummy, real signature should be chosen by ios developer. + friend NetworkPolicy MakeNetworkPolicyIos(bool canUseNetwork); + +public: + bool CanUse() const { return m_canUse; } + +private: + NetworkPolicy(bool const canUseNetwork) : m_canUse(canUseNetwork) {} + + bool m_canUse = false; +}; +// Dummy, real signature, implementation and location should be chosen by ios developer. +inline NetworkPolicy MakeNetworkPolicyIos(bool canUseNetwork) +{ + return NetworkPolicy(canUseNetwork); +} +} // namespace platform diff --git a/platform/platform.pro b/platform/platform.pro index 92950be6b5..01d2aa56d3 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -87,6 +87,7 @@ HEADERS += \ measurement_utils.hpp \ mwm_traits.hpp \ mwm_version.hpp \ + network_policy.hpp \ platform.hpp \ preferred_languages.hpp \ servers_list.hpp \ diff --git a/xcode/platform/platform.xcodeproj/project.pbxproj b/xcode/platform/platform.xcodeproj/project.pbxproj index 9ad9390d13..84729dc48b 100644 --- a/xcode/platform/platform.xcodeproj/project.pbxproj +++ b/xcode/platform/platform.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 3D30587D1D8320E4004AC712 /* http_client.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D30587B1D8320E4004AC712 /* http_client.hpp */; }; 3D30587F1D880910004AC712 /* http_client_apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3D30587E1D880910004AC712 /* http_client_apple.mm */; }; 3D97F64B1D9C05E800380945 /* http_client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D97F64A1D9C05E800380945 /* http_client.cpp */; }; + 3DE8B98F1DEC3115000E6083 /* network_policy.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DE8B98E1DEC3115000E6083 /* network_policy.hpp */; }; 56EB1EDC1C6B6E6C0022D831 /* file_logging.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56EB1ED81C6B6E6C0022D831 /* file_logging.cpp */; }; 56EB1EDD1C6B6E6C0022D831 /* file_logging.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56EB1ED91C6B6E6C0022D831 /* file_logging.hpp */; }; 56EB1EDE1C6B6E6C0022D831 /* mwm_traits.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56EB1EDA1C6B6E6C0022D831 /* mwm_traits.cpp */; }; @@ -111,6 +112,7 @@ 3D30587B1D8320E4004AC712 /* http_client.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = http_client.hpp; sourceTree = ""; }; 3D30587E1D880910004AC712 /* http_client_apple.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = http_client_apple.mm; sourceTree = ""; }; 3D97F64A1D9C05E800380945 /* http_client.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = http_client.cpp; sourceTree = ""; }; + 3DE8B98E1DEC3115000E6083 /* network_policy.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = network_policy.hpp; sourceTree = ""; }; 56EB1ED81C6B6E6C0022D831 /* file_logging.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = file_logging.cpp; sourceTree = ""; }; 56EB1ED91C6B6E6C0022D831 /* file_logging.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = file_logging.hpp; sourceTree = ""; }; 56EB1EDA1C6B6E6C0022D831 /* mwm_traits.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mwm_traits.cpp; sourceTree = ""; }; @@ -316,6 +318,7 @@ 6753437A1A3F5CF500A0A8C3 /* platform */ = { isa = PBXGroup; children = ( + 3DE8B98E1DEC3115000E6083 /* network_policy.hpp */, 34513AF71DCB37C100471BDA /* marketing_service_ios.mm */, 34513AF81DCB37C100471BDA /* marketing_service.cpp */, 34513AF91DCB37C100471BDA /* marketing_service.hpp */, @@ -429,6 +432,7 @@ 674125091B4C00CC00A3E828 /* country_defines.hpp in Headers */, 675343CD1A3F5D5A00A0A8C3 /* platform.hpp in Headers */, 6741250F1B4C00CC00A3E828 /* local_country_file.hpp in Headers */, + 3DE8B98F1DEC3115000E6083 /* network_policy.hpp in Headers */, 675E88A11DB7B0F200F8EBDA /* test_socket.hpp in Headers */, 675343CF1A3F5D5A00A0A8C3 /* preferred_languages.hpp in Headers */, 3D30587D1D8320E4004AC712 /* http_client.hpp in Headers */,