Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsCommandHandler.h"
8 : #include "nsWebBrowser.h"
9 : #include "nsDocShellTreeOwner.h"
10 :
11 : #include "nsMemory.h"
12 : #include "nsPIDOMWindow.h"
13 :
14 0 : nsCommandHandler::nsCommandHandler()
15 0 : : mWindow(nullptr)
16 : {
17 0 : }
18 :
19 0 : nsCommandHandler::~nsCommandHandler()
20 : {
21 0 : }
22 :
23 : nsresult
24 0 : nsCommandHandler::GetCommandHandler(nsICommandHandler** aCommandHandler)
25 : {
26 0 : NS_ENSURE_ARG_POINTER(aCommandHandler);
27 :
28 0 : *aCommandHandler = nullptr;
29 0 : if (!mWindow) {
30 0 : return NS_ERROR_FAILURE;
31 : }
32 :
33 : // Get the document tree owner
34 :
35 : nsCOMPtr<nsIDocShellTreeItem> docShellAsTreeItem =
36 0 : do_QueryInterface(mWindow->GetDocShell());
37 0 : nsIDocShellTreeOwner* treeOwner = nullptr;
38 0 : docShellAsTreeItem->GetTreeOwner(&treeOwner);
39 :
40 : // Make sure the tree owner is an an nsDocShellTreeOwner object
41 : // by QI'ing for a hidden interface. If it doesn't have the interface
42 : // then it's not safe to do the casting.
43 :
44 0 : nsCOMPtr<nsICDocShellTreeOwner> realTreeOwner(do_QueryInterface(treeOwner));
45 0 : if (realTreeOwner) {
46 0 : nsDocShellTreeOwner* tree = static_cast<nsDocShellTreeOwner*>(treeOwner);
47 0 : if (tree->mTreeOwner) {
48 : nsresult rv;
49 0 : rv = tree->mTreeOwner->QueryInterface(NS_GET_IID(nsICommandHandler),
50 0 : (void**)aCommandHandler);
51 0 : NS_RELEASE(treeOwner);
52 0 : return rv;
53 : }
54 :
55 0 : NS_RELEASE(treeOwner);
56 : }
57 :
58 0 : *aCommandHandler = nullptr;
59 :
60 0 : return NS_OK;
61 : }
62 :
63 0 : NS_IMPL_ADDREF(nsCommandHandler)
64 0 : NS_IMPL_RELEASE(nsCommandHandler)
65 :
66 0 : NS_INTERFACE_MAP_BEGIN(nsCommandHandler)
67 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandHandler)
68 0 : NS_INTERFACE_MAP_ENTRY(nsICommandHandlerInit)
69 0 : NS_INTERFACE_MAP_ENTRY(nsICommandHandler)
70 0 : NS_INTERFACE_MAP_END
71 :
72 : ///////////////////////////////////////////////////////////////////////////////
73 : // nsICommandHandlerInit implementation
74 :
75 : NS_IMETHODIMP
76 0 : nsCommandHandler::GetWindow(mozIDOMWindowProxy** aWindow)
77 : {
78 0 : *aWindow = nullptr;
79 0 : return NS_OK;
80 : }
81 :
82 : NS_IMETHODIMP
83 0 : nsCommandHandler::SetWindow(mozIDOMWindowProxy* aWindow)
84 : {
85 0 : if (!aWindow) {
86 0 : return NS_ERROR_FAILURE;
87 : }
88 0 : mWindow = nsPIDOMWindowOuter::From(aWindow);
89 0 : return NS_OK;
90 : }
91 :
92 : ///////////////////////////////////////////////////////////////////////////////
93 : // nsICommandHandler implementation
94 :
95 : NS_IMETHODIMP
96 0 : nsCommandHandler::Exec(const char* aCommand, const char* aStatus,
97 : char** aResult)
98 : {
99 0 : NS_ENSURE_ARG_POINTER(aCommand);
100 0 : NS_ENSURE_ARG_POINTER(aResult);
101 :
102 0 : nsCOMPtr<nsICommandHandler> commandHandler;
103 0 : GetCommandHandler(getter_AddRefs(commandHandler));
104 :
105 : // Call the client's command handler to deal with this command
106 0 : if (commandHandler) {
107 0 : *aResult = nullptr;
108 0 : return commandHandler->Exec(aCommand, aStatus, aResult);
109 : }
110 :
111 : // Return an empty string
112 0 : const char szEmpty[] = "";
113 0 : *aResult = (char*)nsMemory::Clone(szEmpty, sizeof(szEmpty));
114 :
115 0 : return NS_OK;
116 : }
117 :
118 : NS_IMETHODIMP
119 0 : nsCommandHandler::Query(const char* aCommand, const char* aStatus,
120 : char** aResult)
121 : {
122 0 : NS_ENSURE_ARG_POINTER(aCommand);
123 0 : NS_ENSURE_ARG_POINTER(aResult);
124 :
125 0 : nsCOMPtr<nsICommandHandler> commandHandler;
126 0 : GetCommandHandler(getter_AddRefs(commandHandler));
127 :
128 : // Call the client's command handler to deal with this command
129 0 : if (commandHandler) {
130 0 : *aResult = nullptr;
131 0 : return commandHandler->Query(aCommand, aStatus, aResult);
132 : }
133 :
134 : // Return an empty string
135 0 : const char szEmpty[] = "";
136 0 : *aResult = (char*)nsMemory::Clone(szEmpty, sizeof(szEmpty));
137 :
138 0 : return NS_OK;
139 : }
|