|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "Activity.h" |
|
6 |
|
7 #include "nsContentUtils.h" |
|
8 #include "nsDOMClassInfo.h" |
|
9 #include "nsIConsoleService.h" |
|
10 #include "nsIDocShell.h" |
|
11 #include "nsIDocument.h" |
|
12 |
|
13 using namespace mozilla::dom; |
|
14 |
|
15 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Activity) |
|
16 NS_INTERFACE_MAP_END_INHERITING(DOMRequest) |
|
17 |
|
18 NS_IMPL_ADDREF_INHERITED(Activity, DOMRequest) |
|
19 NS_IMPL_RELEASE_INHERITED(Activity, DOMRequest) |
|
20 |
|
21 NS_IMPL_CYCLE_COLLECTION_INHERITED(Activity, DOMRequest, |
|
22 mProxy) |
|
23 |
|
24 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(Activity, DOMRequest) |
|
25 NS_IMPL_CYCLE_COLLECTION_TRACE_END |
|
26 |
|
27 /* virtual */ JSObject* |
|
28 Activity::WrapObject(JSContext* aCx) |
|
29 { |
|
30 return MozActivityBinding::Wrap(aCx, this); |
|
31 } |
|
32 |
|
33 nsresult |
|
34 Activity::Initialize(nsPIDOMWindow* aWindow, |
|
35 JSContext* aCx, |
|
36 const ActivityOptions& aOptions) |
|
37 { |
|
38 MOZ_ASSERT(aWindow); |
|
39 |
|
40 nsCOMPtr<nsIDocument> document = aWindow->GetExtantDoc(); |
|
41 |
|
42 bool isActive; |
|
43 aWindow->GetDocShell()->GetIsActive(&isActive); |
|
44 |
|
45 if (!isActive && |
|
46 !nsContentUtils::IsChromeDoc(document)) { |
|
47 nsCOMPtr<nsIDOMRequestService> rs = |
|
48 do_GetService("@mozilla.org/dom/dom-request-service;1"); |
|
49 rs->FireErrorAsync(static_cast<DOMRequest*>(this), |
|
50 NS_LITERAL_STRING("NotUserInput")); |
|
51 |
|
52 nsCOMPtr<nsIConsoleService> console( |
|
53 do_GetService("@mozilla.org/consoleservice;1")); |
|
54 NS_ENSURE_TRUE(console, NS_OK); |
|
55 |
|
56 nsString message = |
|
57 NS_LITERAL_STRING("Can only start activity from user input or chrome code"); |
|
58 console->LogStringMessage(message.get()); |
|
59 |
|
60 return NS_OK; |
|
61 } |
|
62 |
|
63 // Instantiate a JS proxy that will do the child <-> parent communication |
|
64 // with the JS implementation of the backend. |
|
65 nsresult rv; |
|
66 mProxy = do_CreateInstance("@mozilla.org/dom/activities/proxy;1", &rv); |
|
67 NS_ENSURE_SUCCESS(rv, rv); |
|
68 |
|
69 JS::Rooted<JS::Value> optionsValue(aCx); |
|
70 if (!aOptions.ToObject(aCx, &optionsValue)) { |
|
71 return NS_ERROR_FAILURE; |
|
72 } |
|
73 |
|
74 mProxy->StartActivity(static_cast<nsIDOMDOMRequest*>(this), optionsValue, aWindow); |
|
75 return NS_OK; |
|
76 } |
|
77 |
|
78 Activity::~Activity() |
|
79 { |
|
80 if (mProxy) { |
|
81 mProxy->Cleanup(); |
|
82 } |
|
83 } |
|
84 |
|
85 Activity::Activity(nsPIDOMWindow* aWindow) |
|
86 : DOMRequest(aWindow) |
|
87 { |
|
88 MOZ_ASSERT(IsDOMBinding()); |
|
89 } |
|
90 |