1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview Implements CFInstall.check and isAvailable in a way that is
7 * compatible with previous versions. Also supports a more streamlined
8 * method, 'require'.
9 *
10 **/
11
12goog.provide('google.cf.installer.CFInstall');
13
14goog.require('google.cf.ChromeFrame');
15goog.require('google.cf.installer.InlineDelegate');
16goog.require('google.cf.installer.Installer');
17goog.require('google.cf.installer.OverlayDelegate');
18goog.require('google.cf.installer.Prompt');
19
20/**
21 * Instantiates the CFInstall object. Normally there will be only one, at
22 * Window.CFInstall .
23 * @constructor
24 */
25google.cf.installer.CFInstall = function() {
26  this.prompt_ = new google.cf.installer.Prompt();
27  this.chromeFrame_ = new google.cf.ChromeFrame();
28  this.installer_ = new google.cf.installer.Installer(
29      this.prompt_, this.chromeFrame_);
30  this['setDownloadPageUrl'] =
31      goog.bind(this.prompt_.setDownloadPageUrl, this.prompt_);
32  this['setImplementationUrl'] =
33      goog.bind(this.prompt_.setImplementationUrl, this.prompt_);
34  this['setSameDomainResourceUri'] =
35      goog.bind(this.prompt_.setSameDomainResourceUri, this.prompt_);
36  this['setInteractionDelegate'] =
37      goog.bind(this.prompt_.setInteractionDelegate, this.prompt_);
38  this['require'] = goog.bind(this.installer_.require, this.installer_);
39  this['isAvailable'] =
40      goog.bind(this.chromeFrame_.activate, this.chromeFrame_);
41};
42
43/**
44 * TODO(user): This cookie is not currently set anywhere.
45 * @return {boolean} Whether the user has previously declined Chrome Frame.
46 * @private
47 */
48google.cf.installer.CFInstall.isDeclined_ = function() {
49  return document.cookie.indexOf("disableGCFCheck=1") >=0;
50};
51
52/**
53 * Checks to see if Chrome Frame is available, if not, prompts the user to
54 * install. Once installation is begun, a background timer starts,
55 * checkinging for a successful install every 2 seconds. Upon detection of
56 * successful installation, the current page is reloaded, or if a
57 * 'destination' parameter is passed, the page navigates there instead.
58 * @param {Object} args A bag of configuration properties. Respected
59 *     properties are: 'mode', 'url', 'destination', 'node', 'onmissing',
60 *     'preventPrompt', 'oninstall', 'preventInstallDetection', 'cssText', and
61 *     'className'.
62 */
63google.cf.installer.CFInstall.prototype['check'] = function(args) {
64  args = args || {};
65
66  if (!this.chromeFrame_.isPlatformSupported())
67    return;
68
69  if (this.chromeFrame_.activate())
70    return;
71
72  if (args['onmissing'])
73    args['onmissing']();
74
75  if (google.cf.installer.CFInstall.isDeclined_() || args['preventPrompt'])
76    return;
77
78  // NOTE: @slightlyoff, I'm doing away with the window.open option here. Sites
79  // that were using it will now use the popup.
80
81  // In the case of legacy installation parameters, supply a compatible
82  // InteractionDelegate.
83  if (args['mode'] == 'inline' || args['node'] || args['id'] ||
84      args['cssText'] || args['className']) {
85    if (!args['mode'] || args['mode'] == 'inline') {
86      this.prompt_.setInteractionDelegate(
87        new google.cf.installer.InlineDelegate(args));
88    } else {
89      this.prompt_.setInteractionDelegate(
90        new google.cf.installer.OverlayDelegate(args));
91    }
92  }
93
94  if (args['src'])
95    this.prompt_.setDownloadPageUrl(args['src']);
96
97  var onSuccess = function() {
98    if (this.chromeFrame_.activate() && !args['preventInstallDetection']) {
99      if (args['oninstall'])
100        args['oninstall']();
101      window.location.assign(args['destination'] || window.location);
102    }
103  };
104
105  this.prompt_.open(onSuccess, undefined);
106};
107
108// In compiled mode, this binary is wrapped in an anonymous function which
109// receives the outer scope as its only parameter. In non-compiled mode, the
110// outer scope is window.
111
112// Create a single instance of CFInstall and place it in the outer scope
113// (presumably the global window object).
114try {
115  arguments[0]['CFInstall'] = new google.cf.installer.CFInstall();
116} catch (e) {
117  window['CFInstall'] = new google.cf.installer.CFInstall();
118}
119