~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to gears/factory/factory_ff.cc

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006, Google Inc.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without 
 
4
// modification, are permitted provided that the following conditions are met:
 
5
//
 
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.
 
14
//
 
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.
 
25
 
 
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>
 
30
 
 
31
#include "gears/factory/factory_ff.h"
 
32
 
 
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"
 
36
 
 
37
 
 
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)
 
45
NS_INTERFACE_MAP_END
 
46
 
 
47
 
 
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}};
 
52
 
 
53
 
 
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;
 
61
  }
 
62
#if defined(LINUX) && !defined(OS_MACOSX)
 
63
  InitializeGtkSignalEmissionHooks();
 
64
#endif
 
65
  unload_monitor_.reset(new JsEventMonitor(module_environment->js_runner_,
 
66
                                           JSEVENT_UNLOAD, this));
 
67
  return NS_OK;
 
68
}
 
69
 
 
70
 
 
71
NS_IMETHODIMP GearsFactory::Create(nsISupports **retval) {
 
72
  return DelegateToFactoryImpl("create", false);
 
73
}
 
74
 
 
75
 
 
76
NS_IMETHODIMP GearsFactory::GetBuildInfo(nsAString &retval) {
 
77
  return DelegateToFactoryImpl("getBuildInfo", false);
 
78
}
 
79
 
 
80
 
 
81
NS_IMETHODIMP GearsFactory::GetHasPermission(PRBool *retval) {
 
82
  return DelegateToFactoryImpl("hasPermission", true);
 
83
}
 
84
 
 
85
 
 
86
NS_IMETHODIMP GearsFactory::GetPermission(PRBool *retval) {
 
87
  return DelegateToFactoryImpl("getPermission", false);
 
88
}
 
89
 
 
90
 
 
91
NS_IMETHODIMP GearsFactory::GetVersion(nsAString &retval) {
 
92
  return DelegateToFactoryImpl("version", true);
 
93
}
 
94
 
 
95
 
 
96
NS_IMETHODIMP GearsFactory::DelegateToFactoryImpl(const char *name,
 
97
                                                  bool is_property) {
 
98
  // If the factory no longer exists, fail early.
 
99
  if (!factory_impl_.get()) {
 
100
    return NS_ERROR_FAILURE;
 
101
  }
 
102
 
 
103
  nsresult nr;
 
104
  nsCOMPtr<nsIXPConnect> xpc =
 
105
      do_GetService("@mozilla.org/js/xpc/XPConnect;1", &nr);
 
106
  if (!xpc || NS_FAILED(nr)) { return NS_ERROR_FAILURE; }
 
107
 
 
108
#if BROWSER_FF3
 
109
  nsAXPCNativeCallContext *ncc = NULL;
 
110
  nr = xpc->GetCurrentNativeCallContext(&ncc);
 
111
#else
 
112
  nsCOMPtr<nsIXPCNativeCallContext> ncc;
 
113
  nr = xpc->GetCurrentNativeCallContext(getter_AddRefs(ncc));
 
114
#endif
 
115
  if (!ncc || NS_FAILED(nr)) { return NS_ERROR_FAILURE; }
 
116
 
 
117
  PRUint32 argc;
 
118
  JsToken *argv;
 
119
  JsContextPtr js_context;
 
120
  JsToken *retval;
 
121
  ncc->GetArgc(&argc);
 
122
  ncc->GetArgvPtr(&argv);
 
123
  ncc->GetJSContext(&js_context);
 
124
  ncc->GetRetValPtr(&retval);
 
125
 
 
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);
 
132
  if (is_property) {
 
133
    if (!dispatcher->GetProperty(dispatch_id, &context)) {
 
134
      return NS_ERROR_FAILURE;
 
135
    }
 
136
  } else {
 
137
    if (!dispatcher->CallMethod(dispatch_id, &context)) {
 
138
      return NS_ERROR_FAILURE;
 
139
    }
 
140
  }
 
141
  if (context.is_exception_set()) {
 
142
    ncc->SetExceptionWasThrown(PR_TRUE);
 
143
  } else {
 
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;
 
150
    }
 
151
    ncc->SetReturnValueWasSet(PR_TRUE);
 
152
  }
 
153
  return NS_OK;
 
154
}
 
155
 
 
156
void GearsFactory::HandleEvent(JsEventType event_type) {
 
157
  assert(event_type == JSEVENT_UNLOAD);
 
158
 
 
159
  factory_impl_.reset();
 
160
}