Gears User Guide

Embed Size (px)

Citation preview

Tool Developer (PHP)

INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0INTELLI BITZ TechnologiesGears User GuideTD_GRS_V1.0.0

Google Gears

Official Gears site:

http://code.google.com/apis/gears/index.html

Google Gears Discussion:

http://groups.google.com/group/google-gears

www.intellibitz.com [email protected]

Gears - Introduction

Google Gears is an open source browser extension that lets developers create web applications that can run offline.

http://code.google.com/apis/gears/install.html

Google Gears consists of three modules that address the core challenges in making web applications work offline.

www.intellibitz.com [email protected]

Gears - Features

LocalServer

Cache and serve application resources (HTML, JavaScript, images, etc.) locally

Database

Store data locally in a fully-searchable relational database

WorkerPool

Make your web apps responsive by performing resource-intensive operations asynchronously

www.intellibitz.com [email protected]

Resources and Tools

http://code.google.com/apis/gears/tools.html

The gears_init.js source file

Always include the gears_init.js file in your application to get access to the Google Gears factory and APIs.

Database Query Tool (dbquery.html)

The database query tool allows you to run SQL queries interactively against a Gears database. Run from the same domain as the database to inspect.

LocalServer Inspector (webcachetool.html)

Inspect the contents of the LocalServer, capture URLs, and test ManagedResourceStores.

www.intellibitz.com [email protected]

Detecting and Installing

Detect whether or not Google Gears is installed on a user's machine before calling the APIs, and also to display an installation prompt to the user if Gears not found.

Always initialize Google Gears using gears_init.js . If Gears is installed, then google.gears will be defined.

www.intellibitz.com [email protected]

Detecting and Installing

If Gears isn't installed, you can direct the user to a customized installation page.

if (!window.google || !google.gears) {

location.href = "http://gears.google.com/?action=install&message=" +

"&return=";

}

The user will be directed back to 'return' URL after installation.

www.intellibitz.com [email protected]

Factory

The Factory class is used to instantiate all other Google Gears objects. Using the create method, an application specifies the interface version it uses.

Use gears_init.js in your application for the easiest way to create a Factory object. The script defines google.gears.factory to refer to a Factory object.

www.intellibitz.com [email protected]

Factory

API: Factory class

Object create(className, version)

string getBuildInfo()

Supported class names are:

beta.database Database

beta.localserver LocalServer

beta.workerpool WorkerPool

www.intellibitz.com [email protected]

CODE: gears_init.js defines google.gears.factory

var db = google.gears.factory.create

('beta.database', '1.0');

If version is omitted, then create() returns the latest version of the object

Gears Getting Started

Going Offline

The first thing you need to run a web application offline is the ability to start it without an Internet connection. This is the purpose of the LocalServer module.

LocalServer API and the manifest file, are the key components that cache your application's resources and make it available offline.

www.intellibitz.com [email protected]

LocalServer

The LocalServer module is a specialized URL cache that the web application controls. Requests for URLs in the LocalServer's cache are intercepted and served locally from the user's disk.

Resource stores

A resource store is a container of URLs. Using the LocalServer module, applications can create any number of resource stores, and a resource store can contain any number of URLs.

www.intellibitz.com [email protected]

Creating LocalServer

localServer = google.gears.factory.create('beta.localserver', '1.0');

API: LocalServer class

boolean canServeLocally(string url)

ResourceStore createStore(string name, [string requiredCookie])

ResourceStore openStore(string name, [string requiredCookie])

void removeStore(string name, [string requiredCookie])

ManagedResourceStore createManagedStore(string name, [string requiredCookie])

ManagedResourceStore openManagedStore(string name, [string requiredCookie])

void removeManagedStore(string name, [string requiredCookie])

www.intellibitz.com [email protected]

LocalServer Resource Stores

ResourceStore

for capturing ad-hoc URLs using JavaScript. The ResourceStore allows an application to capture user data files that need to be addressed with a URL, such as a PDF file or an image.

ManagedResourceStore

for capturing a related set of URLs that are declared in a manifest file, and are updated automatically.

www.intellibitz.com [email protected]

Creating ResourceStore

localServer = google.gears.factory.create('beta.localserver');

store = localServer.openStore(STORE_NAME);

store.capture(filesToCapture, captureCallback);

API: ResourceStore class

int capture(urlOrUrlArray, callback)

readwrite attribute boolean enabled

void abortCapture(captureId)

void remove(url)

void copy(srcUrl, destUrl)

boolean isCaptured(url)

void captureFile(fileInputElement, url)

string getCapturedFileName(url)

string getHeader(url, name)

FileSubmitter createFileSubmitter() ...

www.intellibitz.com [email protected]

ResourceStore code snippets

localServer = google.gears.factory.create

('beta.localserver', '1.0');

store = localServer.openStore(STORE_NAME);

// If the store already exists, it will be opened

store = localServer.createStore(STORE_NAME);

store.capture (['sample.js', 'sample.css', 'gears_init.js'], captureCallback);

localServer.removeStore(STORE_NAME);

www.intellibitz.com [email protected]

ManagedResourceStore

localServer = google.gears.factory.create('beta.localserver');

store = localServer.createManagedStore(STORE_NAME);

store.manifestUrl = 'manifest_v1.json';

store.checkForUpdate();

API: ManagedResourceStore class

readonly attribute string name

readonly attribute string requiredCookie

readwrite attribute boolean enabled

readwrite attribute string manifestUrl

readonly attribute int lastUpdateCheckTime

readonly attribute int updateStatus

readonly attribute string lastErrorMessage

readonly attribute string currentVersion

void checkForUpdate()

www.intellibitz.com [email protected]

Manifest file

A manifest file lists all of the URLs to be captured by a ManagedResourceStore. It also contains the version for the manifest file format, the version of the contents of the manifest, and an optional redirection URL.

Using the ManagedResourceStore requires that you create a manifest file.

The manifest file is written in JavaScript Object Notation (JSON) format.

www.intellibitz.com [email protected]

Manifest file

The manifest file and all the URLs listed in it must follow the "same-origin policy", which means that all the URLs must originate from the same URI scheme, host, and port.

An application can have any number of manifest files and ManagedResourceStores. You specify which manifest file to use when you create an instance of ManagedResourceStore.

www.intellibitz.com [email protected]

Manifest file

{

// version of the manifest file "betaManifestVersion": 1,

// version of the set of resources described in this manifest file

"version": "my_version_string",

// optional.. // If the store specifies a requiredCookie, when a request would hit an entry contained in the manifest except the requiredCookie is not present, the local server responds with a redirect to this URL.

"redirectUrl": "login.html",

www.intellibitz.com [email protected]

// URLs to be cached (URLs are given relative to the manifest URL)

"entries": [

{ "url": "main.html", "src": "main_offline.html" },

{ "url": ".", "redirect": "main.html" },

{ "url": "main.js" }

{ "url": "formHandler.html", "ignoreQuery": true },

]

}

Gears Getting Started

Storing User's Data

Applications that are more than just static files have data that is typically stored on the server. For the application to be useful offline, this data must be accessible locally. The Database module provides a relational database for storing data.

www.intellibitz.com [email protected]

Storing User's Data

When an offline application reconnects, you will need to synchronize any changes made in the local database with the server. There are many different approaches to synchronizing data, and there is no single perfect approach.

An additional feature of the Google Gears database is Full-Text Search, providing a fast way to search text within a database file.

www.intellibitz.com [email protected]

Gears Database Module

The Database module provides browser-local relational data storage to your JavaScript web application. Google Gears uses the open source SQLite database system.

The Database module is used to persistently store an application user's data on the user's computer.

www.intellibitz.com [email protected]

Gears Database Module

Data is stored using the same-origin security policy, meaning that a web application cannot access data outside of its domain (see Security).

Data is stored and retrieved by executing SQL statements. Google Gears includes SQLite's full-text search extension fts2.

www.intellibitz.com [email protected]

Gears Database Module

To create a Database object, use the Gears Factory as follows:

var db = google.gears.factory.create('beta.database', '1.0');

API: Database class

void open([name])

ResultSet execute(sqlStatement, [argArray])

void close()

readonly attribute int lastInsertRowId

www.intellibitz.com [email protected]

Database ResultSet

A ResultSet is returned from a successful call to Database.execute(). It contains the results of executing the SQL statement.

A ResultSet is immutable, subsequent changes to the underlying database do not affect the contents.

www.intellibitz.com [email protected]

Database ResultSet

Iterate over the rows of the result set using isValidRow(), next(), and close(), calling data extraction methods for valid rows.

while (rs.isValidRow()) {

console.log(rs.fieldName(0) + " == " + rs.field(0));

rs.next();

}

rs.close();

www.intellibitz.com [email protected]

ResultSet API & DB location

API: ResultSet class

boolean isValidRow()

void next()

void close()

int fieldCount()

string fieldName(int fieldIndex)

variant field(int fieldIndex)

variant fieldByName(string fieldName)

www.intellibitz.com [email protected]

Linux - Firefox - Database files are stored in the user home directory.

Location: ~bob/.mozilla/firefox/