1
// Copyright 2006, Google Inc.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are met:
6
// 1. Redistributions of source code must retain the above copyright notice,
7
// this list of conditions and the following disclaimer.
8
// 2. Redistributions in binary form must reproduce the above copyright notice,
9
// this list of conditions and the following disclaimer in the documentation
10
// and/or other materials provided with the distribution.
11
// 3. Neither the name of Google Inc. nor the names of its contributors may be
12
// used to endorse or promote products derived from this software without
13
// specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include <gecko_sdk/include/nsCOMPtr.h>
27
#include <gecko_sdk/include/nsXPCOM.h>
28
#include <gecko_internal/nsIDOMClassInfo.h>
29
#include <gecko_internal/nsIXPConnect.h>
31
#include "gears/factory/factory_ff.h"
33
#include "gears/base/common/base_class.h"
34
#include "gears/base/common/js_types.h"
35
#include "gears/desktop/drag_and_drop_utils_ff.h"
38
// Boilerplate. == NS_IMPL_ISUPPORTS + ..._MAP_ENTRY_EXTERNAL_DOM_CLASSINFO
39
NS_IMPL_ADDREF(GearsFactory)
40
NS_IMPL_RELEASE(GearsFactory)
41
NS_INTERFACE_MAP_BEGIN(GearsFactory)
42
NS_INTERFACE_MAP_ENTRY(GearsFactoryInterface)
43
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, GearsFactoryInterface)
44
NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(GearsFactory)
48
const char *kGearsFactoryContractId = "@google.com/gears/factory;1";
49
const char *kGearsFactoryClassName = "GearsFactory";
50
const nsCID kGearsFactoryClassId = {0x93b2e433, 0x35ab, 0x46e7,
51
{0xa9, 0x50, 0x41, 0x8f, 0x92, 0x2c, 0xc6, 0xef}};
54
NS_IMETHODIMP GearsFactory::InitFactoryFromDOM() {
55
scoped_refptr<ModuleEnvironment> module_environment(
56
ModuleEnvironment::CreateFromDOM());
57
if (!module_environment ||
58
!CreateModule<GearsFactoryImpl>(module_environment.get(),
59
NULL, &factory_impl_)) {
60
return NS_ERROR_FAILURE;
62
#if defined(LINUX) && !defined(OS_MACOSX)
63
InitializeGtkSignalEmissionHooks();
65
unload_monitor_.reset(new JsEventMonitor(module_environment->js_runner_,
66
JSEVENT_UNLOAD, this));
71
NS_IMETHODIMP GearsFactory::Create(nsISupports **retval) {
72
return DelegateToFactoryImpl("create", false);
76
NS_IMETHODIMP GearsFactory::GetBuildInfo(nsAString &retval) {
77
return DelegateToFactoryImpl("getBuildInfo", false);
81
NS_IMETHODIMP GearsFactory::GetHasPermission(PRBool *retval) {
82
return DelegateToFactoryImpl("hasPermission", true);
86
NS_IMETHODIMP GearsFactory::GetPermission(PRBool *retval) {
87
return DelegateToFactoryImpl("getPermission", false);
91
NS_IMETHODIMP GearsFactory::GetVersion(nsAString &retval) {
92
return DelegateToFactoryImpl("version", true);
96
NS_IMETHODIMP GearsFactory::DelegateToFactoryImpl(const char *name,
98
// If the factory no longer exists, fail early.
99
if (!factory_impl_.get()) {
100
return NS_ERROR_FAILURE;
104
nsCOMPtr<nsIXPConnect> xpc =
105
do_GetService("@mozilla.org/js/xpc/XPConnect;1", &nr);
106
if (!xpc || NS_FAILED(nr)) { return NS_ERROR_FAILURE; }
109
nsAXPCNativeCallContext *ncc = NULL;
110
nr = xpc->GetCurrentNativeCallContext(&ncc);
112
nsCOMPtr<nsIXPCNativeCallContext> ncc;
113
nr = xpc->GetCurrentNativeCallContext(getter_AddRefs(ncc));
115
if (!ncc || NS_FAILED(nr)) { return NS_ERROR_FAILURE; }
119
JsContextPtr js_context;
122
ncc->GetArgvPtr(&argv);
123
ncc->GetJSContext(&js_context);
124
ncc->GetRetValPtr(&retval);
126
DispatcherInterface *dispatcher =
127
factory_impl_->GetWrapper()->GetDispatcher();
128
DispatchId dispatch_id = dispatcher->GetDispatchId(name);
129
assert(dispatch_id != NULL);
130
JsCallContext context(js_context, factory_impl_->GetJsRunner(),
131
static_cast<int>(argc), argv, retval);
133
if (!dispatcher->GetProperty(dispatch_id, &context)) {
134
return NS_ERROR_FAILURE;
137
if (!dispatcher->CallMethod(dispatch_id, &context)) {
138
return NS_ERROR_FAILURE;
141
if (context.is_exception_set()) {
142
ncc->SetExceptionWasThrown(PR_TRUE);
144
if (!context.is_return_value_set()) {
145
// Properties should always either throw an exception, or return a value.
146
assert(!is_property);
147
// We had a method call that didn't throw an exception, but instead
148
// returned void. That's perfectly fine.
149
*retval = JSVAL_VOID;
151
ncc->SetReturnValueWasSet(PR_TRUE);
156
void GearsFactory::HandleEvent(JsEventType event_type) {
157
assert(event_type == JSEVENT_UNLOAD);
159
factory_impl_.reset();