1 // Copyright 2005, 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 "gears/localserver/npapi/localserver_np.h"
28 #include "gears/base/common/paths.h"
29 #include "gears/base/npapi/module_wrapper.h"
30 #include "gears/localserver/npapi/managed_resource_store_np.h"
31 #include "gears/localserver/npapi/resource_store_np.h"
33 DECLARE_GEARS_WRAPPER(GearsLocalServer
);
36 void Dispatcher
<GearsLocalServer
>::Init() {
37 RegisterMethod("canServeLocally", &GearsLocalServer::CanServeLocally
);
38 RegisterMethod("createManagedStore", &GearsLocalServer::CreateManagedStore
);
39 RegisterMethod("openManagedStore", &GearsLocalServer::OpenManagedStore
);
40 RegisterMethod("removeManagedStore", &GearsLocalServer::RemoveManagedStore
);
41 RegisterMethod("createStore", &GearsLocalServer::CreateStore
);
42 RegisterMethod("openStore", &GearsLocalServer::OpenStore
);
43 RegisterMethod("removeStore", &GearsLocalServer::RemoveStore
);
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 void GearsLocalServer::CanServeLocally(JsCallContext
*context
) {
51 context
->SetReturnValue(JSPARAM_BOOL
, &retval
);
52 context
->SetException(STRING16(L
"Not Implemented"));
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 void GearsLocalServer::CreateManagedStore(JsCallContext
*context
) {
60 std::string16 required_cookie
;
61 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
64 GComPtr
<GearsManagedResourceStore
> store(
65 CreateModule
<GearsManagedResourceStore
>(EnvPageJsContext()));
67 return; // Create function sets an error message.
69 if (!store
->InitBaseFromSibling(this)) {
70 context
->SetException(STRING16(L
"Error initializing base class."));
74 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
75 context
->SetException(STRING16(L
"Not Implemented"));
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 void GearsLocalServer::OpenManagedStore(JsCallContext
*context
) {
83 std::string16 required_cookie
;
84 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
87 context
->SetReturnValue(JSPARAM_NULL
, NULL
);
88 context
->SetException(STRING16(L
"Not Implemented"));
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
94 void GearsLocalServer::RemoveManagedStore(JsCallContext
*context
) {
96 std::string16 required_cookie
;
97 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
100 context
->SetException(STRING16(L
"Not Implemented"));
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
106 void GearsLocalServer::CreateStore(JsCallContext
*context
) {
108 std::string16 required_cookie
;
109 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
112 GComPtr
<GearsResourceStore
> store(
113 CreateModule
<GearsResourceStore
>(EnvPageJsContext()));
115 return; // Create function sets an error message.
117 if (!store
->InitBaseFromSibling(this)) {
118 context
->SetException(STRING16(L
"Error initializing base class."));
122 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
123 context
->SetException(STRING16(L
"Not Implemented"));
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 void GearsLocalServer::OpenStore(JsCallContext
*context
) {
131 std::string16 required_cookie
;
132 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
135 context
->SetReturnValue(JSPARAM_NULL
, NULL
);
136 context
->SetException(STRING16(L
"Not Implemented"));
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
142 void GearsLocalServer::RemoveStore(JsCallContext
*context
) {
144 std::string16 required_cookie
;
145 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
148 context
->SetException(STRING16(L
"Not Implemented"));
151 //------------------------------------------------------------------------------
152 // GetAndCheckParameters
153 //------------------------------------------------------------------------------
154 bool GearsLocalServer::GetAndCheckParameters(JsCallContext
*context
,
156 std::string16
*required_cookie
) {
157 JsArgument argv
[] = {
158 { JSPARAM_REQUIRED
, JSPARAM_STRING16
, name
},
159 { JSPARAM_OPTIONAL
, JSPARAM_STRING16
, required_cookie
},
161 int argc
= context
->GetArguments(ARRAYSIZE(argv
), argv
);
162 if (context
->is_exception_set())
165 // Validate parameters
167 context
->SetException(STRING16(L
"The name parameter is required."));
171 std::string16 error_message
;
172 if (!IsUserInputValidAsPathComponent(*name
, &error_message
)) {
173 context
->SetException(error_message
.c_str());