ITCamp 2013 - Raffaele Rialdi - Windows Runtime (WinRT) deep dive

379 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
379
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ITCamp 2013 - Raffaele Rialdi - Windows Runtime (WinRT) deep dive

  1. 1. itcampro@ itcamp13# Premium conference on Microsoft technologiesWindows Runtime (WinRT)deep diveRaffaele Rialdi@raffaelerraffaeler@vevy.com http://www.iamraf.net
  2. 2. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileHuge thanks to our sponsors!
  3. 3. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileSpot the differenceUsing a pure .NET libraryUsing a Windows Runtime native library projected in .NET<!DOCTYPE html><html><head><title>IAmRaf - Home Page</title><meta name="description"content="Raffaele Rialdi home page" />…using System.Net.Http;...using (var client = new HttpClient()){var res = await client.GetStringAsync("http://www.iamraf.net");// ...}using Windows.Web.Syndication;...var client = new SyndicationClient();var feed = await client.RetrieveFeedAsync(new Uri("http://www.iamraf.net/rss"));var firstItem = feed.Items.FirstOrDefault();var content = firstItem.Summary.Text;The new Windows start screen 8 shows a number of tiles that may looklike a revamped version of the old shortcuts:But appearances can be deceptive and tiles are not shortcuts and theydo not launch directly the application process.The crucial point is that Windows Runtime (WinRT) is the laye...
  4. 4. itcampro@ itcamp13# Premium conference on Microsoft technologiesCOMPONENTS UNDER THEHOOD
  5. 5. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileClassic C++ polimorphism is static• Polimorphism is possible only if class definitions are known– It must be done at compile time because C++ provides nobinary contractclass myBase{public:virtual void Foo() { …}virtual void Bar() { … }};class myDerived : publicmyBase{public:void Foo() override { … }void Bar() override { … }};vTablemyBase::FoomyBase::BarVTablemyDerived::FoomyDerived::BarPatched at compile time!
  6. 6. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileInter-assembly Inheritance (.NET "trick")Assembly 1public class mybase{public virtual void Foo() { }public void Bar() { }}Assembly 2public class myderived : mybase{public override void Foo() { }public void Qux() { }}MethodTableToStringEqualsGetHashCodeFinalizeFooBarQuxMethodTableToStringEqualsGetHashCodeFinalizeFooBarSystem.ObjectPatched at runtime!
  7. 7. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• We need a binary contract– Metadata to define the Type System– Runtime to manage the runtime behaviorSolution: metadata and runtimeMethodTableAddRefReleaseQueryInterfaceGetIidsGetRuntimeClassNameGetTrustLevelFooBarQuxIUnknownIInspectableIMyBaseIMyDerived1. Dynamic interface discovery• IUnknown.QueryInterface is theruntime equivalent of a castor• IInspectable.GetIids(javascript only)2. New rule: cannot derive anycomponent with theexception of XAML controls• Reason? Versioning!
  8. 8. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileModeling by interface• Why? Because Modeling by interface is simpler!• Interfaces are the public surface of thecomponent– Identified by an IID (Guid)– Can be marked as “exclusiveto” for a specificcomponent• The class implementing the interface– Identified by the namespace.nome string– Class is “hidden” and never visible• Can only be riched with the use of interface• C++ can bypass the interface for perf reasons
  9. 9. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileCode is always accessed by interface!Projections make interfaces appear like they were classesThe "new" operation is fakevar client = new SyndicationClient();ComPtr<ISyndicationClient> spClient;auto classId = HStringReference(RuntimeClass_Windows_Web_Syndication_SyndicationClient).Get();hr = ActivateInstance(classId, &spClient);if(FAILED(hr))return PrintError(hr);HRESULT hr;ComPtr<IActivationFactory> spFactory;auto classId = HStringReference(RuntimeClass_Windows_Web_Syndication_SyndicationClient).Get();hr = GetActivationFactory(classId, &spFactory);if(FAILED(hr)) return PrintError(hr);ComPtr<ISyndicationClient> spSyndicationClient;hr = spActivationFactory->ActivateInstance(&spSyndicationClient);if(FAILED(hr)) return PrintError(hr);The long way(C++ and WRL Library)The short way(C++ and WRL Library)The C++ projection(C++/CX)auto client = ref new SyndicationClient();C# projection
  10. 10. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• How can static methods exists if the surface isonly exposed by interfaces?• COM did not support static methods• WinRT do support them– But static methods cannot be part of an interface!• Solution: statics are instance methods of thefactory– The interface exposing the static methods isautomatically generated by the compiler• Projections make them appear as if they werepart of the classStatic methods
  11. 11. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• QueryInterface– Discover interfaces in a component• IUnknown: AddRef e Release– Managing the lifecycle• Circular References risk– WeakReference is the solution– SDKs/ Projections create the WeakRef for usComponent lifecycleA BRootParentParent
  12. 12. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• Make use of the WinRT metadata to map:– Base types (int, double, string, …)– Complex types• INotifyPropertyChanged, collections, ...• Extension methods for non-mappable types– Streams, Storage, Async• Make use of the CCW and RCW as it was inCOM• Native HRESULTS are converted intoExceptions and vice-versa.NET projection
  13. 13. itcampro@ itcamp13# Premium conference on Microsoft technologiesDEMO: SPYCOMPONENT
  14. 14. itcampro@ itcamp13# Premium conference on Microsoft technologiesTHE VIEW ARCHITECTURE
  15. 15. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• The main function – Its always there• The message pump– Its there too• One single thread allowed to interact withthe UI– Nothing changed here too• But …Once upon a time …
  16. 16. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• Once started the App becomes a component– Like the old COM exe server• OS and Apps interact via Contracts– They are interfaces with new high level IPC• The UI sits on DirectX– Pure DX with C++– Mixed XAML and DX with C++– Pure XAML with C++ and .NET– HTML with JavascriptInfrastructure differences
  17. 17. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileThe view lifecycleWinRT AppIFrameworkViewSourceIFrameworkViewmainCoreApplication.RuncreateCreateViewcreateInitializeSetWindowLoadRunUninitializeUserinteraction
  18. 18. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• Needed because components cannot alwaysbe accessed by any thread• Apartment is a group of one or more threadswho can legally invoke a component– MTA: total threads freedom and better perf– STA: one single thread at a time• calls are enqueued to avoid concurrency– ASTA: new STA which avoid re-entrancy• used for the primary UI thread in Win8 Apps• Marshaling is minimized because of the new"Agile" conceptThreading model: Apartments
  19. 19. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• Most of the WinRT library is marked as"both"– compatbile with MTA and (A)STA• XAML is "both" but at runtime enforce to becalled by ASTA only• Share target Contract are opened in adifferent ASTA!– One of the primary sources of bugs inWindows Store Applications• Be careful to use the correct DispatcherThreading and library
  20. 20. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileThreading, Async and Dispatcher• Languages offer async support in differentflavors– Async/Await (.NET)– Promise (C++, Js)• Async use the SynchronizationContext to findthe primary thread where the callback will beexecuted• If you call Dispacher.Invoke by yourself– Pay attention: every ASTA has a differentDispatcher
  21. 21. itcampro@ itcamp13# Premium conference on Microsoft technologiesSECURITY!
  22. 22. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileAppContainer restrictions• Limitations derived from Low Integrity Level• API restrictions:– Limited Win32 API and COM Interfaces– .NET Windows Store Profile subset• Some WinRT components are brokered– Capabilities are as a sort of "privilege elevation"obtained with Brokered objects– Contracts are a modern IPC mechanism
  23. 23. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &MobileRuntime Security BrokerKernel servicesWinRTRuntimeSecurity BrokerFiltered COM / Win32 APIs Full COM / Win32Comp.DeviceaccessPickerHost …Comp.Brokered UIAppContainer
  24. 24. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• How to avoid capabilities forgery?– Native code has full power in its own process• Assigned SIDs– One SID to identify the application– One SID for every requested capabilityCapabilities under the hoodCompile timeWindowsStorePackage PackageCodesigningPackageRuntimeexecutionManifestSIDassignement
  25. 25. itcampro@ itcamp13# Premium conference on Microsoft technologiesDevelopment &Mobile• WinRT expose the OS in an OOP fashion• Projections hide most of the complexity• C++ and WRL are a good way to explorewhat is behind• Understanding the platform details can be ofgreat help while developingTakeaways
  26. 26. itcampro@ itcamp13# Premium conference on Microsoft technologiesQ & AThank you!

×