mozilla chrome

29
MOZILLA CHROME jako platforma deweloperska

Upload: media

Post on 23-Jan-2016

65 views

Category:

Documents


0 download

DESCRIPTION

jako platforma deweloperska. Mozilla Chrome. O czym dzisiaj?. O mojej pracy Historia Mozilli Architektura Chrome XUL – jak to wygląda? Javascript – jak to działa? XPCOM – co jest w środku? Narzędzia – jak się tym bawić?. Moja praca. Indeksowanie Biblioteka Lucene Archiwizacja - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Mozilla  Chrome

MOZILLA CHROME

jako platforma deweloperska

Page 2: Mozilla  Chrome

O czym dzisiaj?

O mojej pracy Historia Mozilli Architektura Chrome XUL – jak to wygląda? Javascript – jak to działa? XPCOM – co jest w środku? Narzędzia – jak się tym bawić?

Page 3: Mozilla  Chrome

Moja praca

Indeksowanie Biblioteka Lucene Archiwizacja Koncept rozwiązania

Page 4: Mozilla  Chrome

Mozilla – (Mosaic killer)1994 – Netscape Navigator

1998 – Mozilla Organization(przebudowa Netscape)

2003 – Mozilla Foundation(rozwój Mozilla suite)

2003 – Mozilla Corporation(dostarczanie firefoksa)

Page 5: Mozilla  Chrome

Mozilla Dziś

MozillaTh

un

derb

ird

Fir

efo

x

XU

LR

un

ner

Page 6: Mozilla  Chrome

ArchitekturaPlatforma Mozilla

Toolkit

Content

XUL DOM CSS

SVG HTML XML

Zarządzanie Dodatkami Aktualizacja

Sprawdzanie pisowni

NeckoSQLite

XPCOM

XPConnect

JavaScript

Widget

Netscape Portable Runtime

Page 7: Mozilla  Chrome

GUI – co zauważono

Ciężko utrzymywać wygląd aplikacji na wielu platformach

HTML to wygodne narzędzie definiowania wyglądu

Pisząc przeglądarkę należy stworzyć silnik graficzny do renderowania stron HTML

Page 8: Mozilla  Chrome

Czym jest Gecko?

Gecko

Page 9: Mozilla  Chrome

Czym jest XUL?<menu>

<menuitem><menupopup>

Gecko

<window> <label> <button>

Page 10: Mozilla  Chrome

XUL a HTML po krótce

XUL

HTML

Page 11: Mozilla  Chrome

XUL a HTML trochę szerzej Idea Sandbox Odwołania do obiektów XPCOM Odwołania do systemu plików Ograniczony dostęp do obiektów

Javascript Ograniczone znaczniki

Page 12: Mozilla  Chrome

XUL a dodatki<overlay id="helloworld-overlay„ …>

<menupopup id="taskPopup"><menuitem id="helloworld-hello"

label="&helloworld;" </menupopup></overlay>

Jak Znaleźć ID?

Page 13: Mozilla  Chrome

Rejestracja Dodatku .rdf

Page 14: Mozilla  Chrome

Rejestracja… chrome.manifest

content helloworld content/overlay chrome://messenger/content/messenger.xul

chrome://helloworld/content/overlay.xul

locale helloworld en locale/en/locale helloworld pl locale/pl/

skin helloworld classic/1.0 skin/

Page 15: Mozilla  Chrome

JavaScript – jak to działa? Podobnie jak w klasycznym HTML:

<script src="overlay.js"/>

oncommand="Helloworld.onMenuItemCommand(event);"

var Helloworld = { onMenuItemCommand: function() { window.open("chrome://helloworld/content/hello.xul",

"", "chrome"); }};

Page 16: Mozilla  Chrome

JavaScript – jak to działa? Inaczej niż w klasycznym HTML:

var c = Components.classes["@mozilla.org/messenger/msgwindow;1"];var i = c.createInstance(Components.interfaces.nsIMsgWindow);var c2 = Components.classes["@mozilla.org/messengercompose;1"];Var i2 = c2.getService(Components.interfaces.nsIMsgComposeService);

Ponad 130 interfejsów Bardzo mało dokumentacji :/

Page 17: Mozilla  Chrome

XPCOM - co jest w środku? Microsoft COM – Component Object

Model XPCOM – Cross Platform COM

Page 18: Mozilla  Chrome

COM a XPCOM - interfejsy IUnknown

IClassFactory(2)

??

nsISupports

nsIFactory

nsIModule

Page 19: Mozilla  Chrome

COM a XPCOM IDL Delphi, C++, Visual

Basic rejestr

Windows

XPIDL C++, JavaScript,

Python pliki rejestracyjne

(rejestracja w locie) Windows, Linux,

Unix, MacOS, ...

Page 20: Mozilla  Chrome

Świadomość Komponentu

Programowanie Komponentowe

Komponent

Komponent

Interfejs

Kom

pone

nt

Interfejs

Kompo

nent

Inte

rfejs

Komponent

Interfejs

Komponent

Interfejs

Page 21: Mozilla  Chrome

Komponenty - zalety

Łatwy podział obowiązków programistycznych

Łatwa zmiana impleentacji Wydajność – późne ładowanie Utrzymanie modularnego kodu jest

łatwijsze

Page 22: Mozilla  Chrome

Komponent, Serwis, Interfejs, Moduł, Fabryka

Komponent implementuje interfejs Jeden Komponent może implementować

wiele interfejsów Interfejs może być implementowany przez

wiele komponentów Serwis – Komponent o jednej instacji Moduł to zbiór komponentów Fabryka służy do instancjonowania

komponentów i zarządzania ich cyklem życia

Page 23: Mozilla  Chrome

Idea Zamrażania Interfejsów DLL Hell

Interfejsy zamrożone i nie zamrożone

Po co komu nie zamrożone interfejsy

XPCOM Glue (i inne przydatne klasy)

Page 24: Mozilla  Chrome

Rejestracja Komponentu

Module::RegisterSelf

Module::UnregisterSelf

Rejestracja w Mozilli

Page 25: Mozilla  Chrome

Proces Budowania

MozillaBuild (cygwin + perl + skrypty) Microsoft Visual C++ Microsoft Windows SDK

XPIDL Makefile.in

make-makefilemake

.htype library

Page 26: Mozilla  Chrome

Przydatne MAKRA

NS_GENERIC_FACTORYCONSTRUCTOR NS_IMPL_NSGETMODULE NS_IMPL_ISUPPARTS (1…n) NS_DECL_... NS_INIT_ISUPPORTS

Page 27: Mozilla  Chrome

MAKRA c.d.#include <stdio.h> #define MOZILLA_STRICT_API #include "nsIModule.h" #include "nsIFactory.h" #include "nsIComponentManager.h" #include "nsIComponentRegistrar.h" static

const nsIID kIModuleIID = NS_IMODULE_IID; static const nsIID kIFactoryIID = NS_IFACTORY_IID; static const nsIID kISupportsIID = NS_ISUPPORTS_IID; static const nsIID kIComponentRegistrarIID = NS_ICOMPONENTREGISTRAR_IID; #define SAMPLE_CID \ { 0x777f7150, 0x4a2b, 0x4301, \ { 0xad, 0x10, 0x5e, 0xab, 0x25, 0xb3, 0x22, 0xaa}} static const nsCID kSampleCID = SAMPLE_CID; class Sample: public nsISupports { private: nsrefcnt mRefCnt; public: Sample(); virtual ~Sample(); NS_IMETHOD QueryInterface(const nsIID &aIID, void **aResult); NS_IMETHOD_(nsrefcnt) AddRef(void); NS_IMETHOD_(nsrefcnt) Release(void); }; Sample::Sample() { mRefCnt = 0; } Sample::~Sample() { } NS_IMETHODIMP Sample::QueryInterface(const nsIID &aIID, void **aResult) { if (aResult == NULL) { return NS_ERROR_NULL_POINTER; } *aResult = NULL; if (aIID.Equals(kISupportsIID)) { *aResult = (void *) this; } if (*aResult == NULL) { return NS_ERROR_NO_INTERFACE; } AddRef(); return NS_OK; } NS_IMETHODIMP_(nsrefcnt) Sample::AddRef() { return ++mRefCnt; } NS_IMETHODIMP_(nsrefcnt) Sample::Release() { if (--mRefCnt == 0) { delete this; return 0; } return mRefCnt; } // factory implementation class for component class SampleFactory: public nsIFactory{ private: nsrefcnt mRefCnt; public: SampleFactory(); virtual ~SampleFactory(); NS_IMETHOD QueryInterface(const nsIID &aIID, void **aResult); NS_IMETHOD_(nsrefcnt) AddRef(void); NS_IMETHOD_(nsrefcnt) Release(void); NS_IMETHOD CreateInstance(nsISupports *aOuter, const nsIID & iid, void * *result); NS_IMETHOD LockFactory(PRBool lock); }; SampleFactory::SampleFactory() { mRefCnt = 0; } SampleFactory::~SampleFactory() { } NS_IMETHODIMP SampleFactory::QueryInterface(const nsIID &aIID, void **aResult) { if (aResult == NULL) { return NS_ERROR_NULL_POINTER; } *aResult = NULL; if (aIID.Equals(kISupportsIID)) { *aResult = (void *) this; } else if (aIID.Equals(kIFactoryIID)) { *aResult = (void *) this; } if (*aResult == NULL) { return NS_ERROR_NO_INTERFACE; } AddRef(); return NS_OK; } NS_IMETHODIMP_(nsrefcnt) SampleFactory::AddRef() { return ++mRefCnt; } NS_IMETHODIMP_(nsrefcnt) SampleFactory::Release() { if (--mRefCnt == 0) { delete this; return 0; } return mRefCnt; } NS_IMETHODIMP SampleFactory::CreateInstance(nsISupports *aOuter, const nsIID & iid, void * *result) { if (!result) return NS_ERROR_INVALID_ARG; Sample* sample = new Sample(); if (!sample) return NS_ERROR_OUT_OF_MEMORY; nsresult rv = sample->QueryInterface(iid, result); if (NS_FAILED(rv)) { *result = nsnull; delete sample; } return rv; } NS_IMETHODIMP SampleFactory::LockFactory(PRBool lock) { return NS_ERROR_NOT_IMPLEMENTED; } // Module implementation class SampleModule : public nsIModule { public: SampleModule(); virtual ~SampleModule(); // nsISupports methods: NS_IMETHOD QueryInterface(const nsIID & uuid, void * *result); NS_IMETHOD_(nsrefcnt) AddRef(void); NS_IMETHOD_(nsrefcnt) Release(void); // nsIModule methods: NS_IMETHOD GetClassObject(nsIComponentManager *aCompMgr, const nsCID & aClass, const nsIID & aIID, void * *aResult); NS_IMETHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aLocation, const char *aLoaderStr, const char *aType); NS_IMETHOD UnregisterSelf(nsIComponentManager *aCompMgr, nsIFile *aLocation, const char *aLoaderStr); NS_IMETHOD CanUnload(nsIComponentManager *aCompMgr, PRBool *_retval); private: nsrefcnt mRefCnt; }; //---------------------------------------------------------------------- SampleModule::SampleModule() { mRefCnt = 0; } SampleModule::~SampleModule() { } // nsISupports implemention NS_IMETHODIMP_(nsrefcnt) SampleModule::AddRef(void) { return ++mRefCnt; } NS_IMETHODIMP_(nsrefcnt) SampleModule::Release(void) { if (--mRefCnt == 0) { mRefCnt = 1; /* stabilize */ delete this; return 0; } return mRefCnt; } NS_IMETHODIMP SampleModule::QueryInterface(REFNSIID aIID, void** aInstancePtr) { if (!aInstancePtr) return NS_ERROR_NULL_POINTER; nsISupports* foundInterface; if (aIID.Equals(kIModuleIID)) { foundInterface = (nsIModule*) this; } else if ( aIID.Equals(kISupportsIID) ) { foundInterface = (nsISupports*) this; } else { foundInterface = 0; } if (foundInterface) { foundInterface->AddRef(); *aInstancePtr = foundInterface; return NS_OK; } *aInstancePtr = foundInterface; return NS_NOINTERFACE; } // Create a factory object for creating instances of aClass. NS_IMETHODIMP SampleModule::GetClassObject(nsIComponentManager *aCompMgr, const nsCID& aClass, const nsIID& aIID, void** result) { if (!kSampleCID.Equals(aClass)) return NS_ERROR_FACTORY_NOT_REGISTERED; if (!result) return NS_ERROR_INVALID_ARG; SampleFactory* factory = new SampleFactory(); if (!factory) return NS_ERROR_OUT_OF_MEMORY; nsresult rv = factory->QueryInterface(aIID, result); if (NS_FAILED(rv)) { *result = nsnull; delete factory; } return rv; } //---------------------------------------- NS_IMETHODIMP SampleModule::RegisterSelf(nsIComponentManager *aCompMgr, nsIFile* aPath, const char* registryLocation, const char* componentType) { nsIComponentRegistrar* compReg = nsnull; nsresult rv = aCompMgr->QueryInterface(kIComponentRegistrarIID, (void**)&compReg); if (NS_FAILED(rv)) return rv; rv = compReg->RegisterFactoryLocation(kSampleCID, "Sample Class", nsnull, aPath, registryLocation, componentType); compReg->Release(); return rv; } NS_IMETHODIMP SampleModule::UnregisterSelf(nsIComponentManager* aCompMgr, nsIFile* aPath, const char* registryLocation) { nsIComponentRegistrar* compReg = nsnull; nsresult rv = aCompMgr->QueryInterface(kIComponentRegistrarIID, (void**)&compReg); if (NS_FAILED(rv)) return rv; rv = compReg->UnregisterFactoryLocation(kSampleCID, aPath); compReg->Release(); return rv; } NS_IMETHODIMP SampleModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload) { *okToUnload = PR_FALSE; // we do not know how to unload. return NS_OK; } //---------------------------------------------------------------------- extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr, nsIFile* location, nsIModule** return_cobj) { nsresult rv = NS_OK; // Create and initialize the module instance SampleModule *m = new SampleModule(); if (!m) { return NS_ERROR_OUT_OF_MEMORY; } // Increase refcnt and store away nsIModule interface to m in return_cobj rv = m->QueryInterface(kIModuleIID, (void**)return_cobj); if (NS_FAILED(rv)) { delete m; } return rv; }

Page 28: Mozilla  Chrome

MAKRA c.d.#include "nsIGenericFactory.h" #include "nsISupportsUtils.h"

#define SAMPLE_CID \ { 0x777f7150, 0x4a2b, 0x4301, \ { 0xad, 0x10, 0x5e, 0xab, 0x25, 0xb3, 0x22, 0xaa}} class Sample: public nsISupports { public: Sample(); virtual ~Sample(); NS_DECL_ISUPPORTS }; Sample::Sample() { // note: in newer versions of Gecko (1.3 or later) // you don't have to do this: NS_INIT_ISUPPORTS(); } Sample::~Sample() { } NS_IMPL_ISUPPORTS1(Sample, nsISupports); NS_GENERIC_FACTORY_CONSTRUCTOR(Sample); static const nsModuleComponentInfo components[] = { { "Pretty Class Name", SAMPLE_CID, "@company.com/sample", SampleConstructor } }; NS_IMPL_NSGETMODULE(nsSampleModule, components)

Page 29: Mozilla  Chrome

DZIĘKUJĘ ZA UWAGĘ