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/module_wrapper.h"
29 #include "gears/base/common/paths.h"
30 #include "gears/base/common/url_utils.h"
31 #include "gears/localserver/common/http_request.h"
32 #include "gears/localserver/npapi/managed_resource_store_np.h"
33 #include "gears/localserver/npapi/resource_store_np.h"
35 DECLARE_GEARS_WRAPPER(GearsLocalServer
);
39 void Dispatcher
<GearsLocalServer
>::Init() {
40 RegisterMethod("canServeLocally", &GearsLocalServer::CanServeLocally
);
41 RegisterMethod("createManagedStore", &GearsLocalServer::CreateManagedStore
);
42 RegisterMethod("openManagedStore", &GearsLocalServer::OpenManagedStore
);
43 RegisterMethod("removeManagedStore", &GearsLocalServer::RemoveManagedStore
);
44 RegisterMethod("createStore", &GearsLocalServer::CreateStore
);
45 RegisterMethod("openStore", &GearsLocalServer::OpenStore
);
46 RegisterMethod("removeStore", &GearsLocalServer::RemoveStore
);
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 void GearsLocalServer::CanServeLocally(JsCallContext
*context
) {
55 { JSPARAM_REQUIRED
, JSPARAM_STRING16
, &url
},
57 context
->GetArguments(ARRAYSIZE(argv
), argv
);
58 if (context
->is_exception_set())
61 std::string16 full_url
;
62 if (!ResolveAndNormalize(EnvPageLocationUrl().c_str(), url
.c_str(),
64 context
->SetException(STRING16(L
"Failed to resolve url."));
67 if (!EnvPageSecurityOrigin().IsSameOriginAsUrl(full_url
.c_str())) {
68 context
->SetException(STRING16(L
"Url is not from the same origin"));
72 bool can
= LocalServer::CanServeLocally(full_url
.c_str());
73 LOG16((L
"LocalServer::CanServeLocally( %s ) %s\n",
74 url
.c_str(), can
? STRING16(L
"TRUE") : STRING16(L
"FALSE")));
75 context
->SetReturnValue(JSPARAM_BOOL
, &can
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 void GearsLocalServer::CreateManagedStore(JsCallContext
*context
) {
83 std::string16 required_cookie
;
84 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
87 // Check that this page uses a supported URL scheme.
88 if (!HttpRequest::IsSchemeSupported(
89 EnvPageSecurityOrigin().scheme().c_str())) {
90 context
->SetException(STRING16(L
"URL scheme not supported."));
94 LOG16((L
"LocalServer::CreateManagedStore( %s, %s )\n",
95 name
.c_str(), required_cookie
.c_str()));
97 GComPtr
<GearsManagedResourceStore
> store(
98 CreateModule
<GearsManagedResourceStore
>(GetJsRunner()));
100 return; // Create function sets an error message.
102 if (!store
->InitBaseFromSibling(this)) {
103 context
->SetException(STRING16(L
"Error initializing base class."));
107 if (!store
->store_
.CreateOrOpen(EnvPageSecurityOrigin(),
108 name
.c_str(), required_cookie
.c_str())) {
109 context
->SetException(
110 STRING16(L
"Error initializing ManagedResourceStore."));
114 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
120 void GearsLocalServer::OpenManagedStore(JsCallContext
*context
) {
122 std::string16 required_cookie
;
123 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
126 LOG16((L
"LocalServer::OpenManagedStore( %s, %s )\n",
127 name
.c_str(), required_cookie
.c_str()));
129 int64 existing_store_id
= WebCacheDB::kInvalidID
;
130 if (!ManagedResourceStore::ExistsInDB(EnvPageSecurityOrigin(),
132 required_cookie
.c_str(),
133 &existing_store_id
)) {
134 context
->SetReturnValue(JSPARAM_NULL
, NULL
);
138 GComPtr
<GearsManagedResourceStore
> store(
139 CreateModule
<GearsManagedResourceStore
>(GetJsRunner()));
141 return; // Create function sets an error message.
143 if (!store
->InitBaseFromSibling(this)) {
144 context
->SetException(STRING16(L
"Error initializing base class."));
148 if (!store
->store_
.Open(existing_store_id
)) {
149 context
->SetException(
150 STRING16(L
"Error initializing ManagedResourceStore."));
154 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
157 //-----------------------------------------------------------------------------
158 // RemoveManagedStore
159 //-----------------------------------------------------------------------------
160 void GearsLocalServer::RemoveManagedStore(JsCallContext
*context
) {
162 std::string16 required_cookie
;
163 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
166 LOG16((L
"LocalServer::RemoveManagedStore( %s, %s )\n",
167 name
.c_str(), required_cookie
.c_str()));
169 int64 existing_store_id
= WebCacheDB::kInvalidID
;
170 if (!ManagedResourceStore::ExistsInDB(EnvPageSecurityOrigin(),
172 required_cookie
.c_str(),
173 &existing_store_id
)) {
174 context
->SetReturnValue(JSPARAM_NULL
, NULL
);
178 ManagedResourceStore store
;
179 if (!store
.Open(existing_store_id
)) {
180 context
->SetException(
181 STRING16(L
"Error initializing ManagedResourceStore."));
185 if (!store
.Remove()) {
186 context
->SetException(STRING16(L
"Error removing ManagedResourceStore."));
191 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 void GearsLocalServer::CreateStore(JsCallContext
*context
) {
196 std::string16 required_cookie
;
197 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
200 // Check that this page uses a supported URL scheme.
201 if (!HttpRequest::IsSchemeSupported(
202 EnvPageSecurityOrigin().scheme().c_str())) {
203 context
->SetException(STRING16(L
"URL scheme not supported."));
207 LOG16((L
"LocalServer::CreateStore( %s, %s )\n",
208 name
.c_str(), required_cookie
.c_str()));
210 GComPtr
<GearsResourceStore
> store(
211 CreateModule
<GearsResourceStore
>(GetJsRunner()));
213 return; // Create function sets an error message.
215 if (!store
->InitBaseFromSibling(this)) {
216 context
->SetException(STRING16(L
"Error initializing base class."));
220 if (!store
->store_
.CreateOrOpen(EnvPageSecurityOrigin(),
221 name
.c_str(), required_cookie
.c_str())) {
222 context
->SetException(STRING16(L
"Error initializing ResourceStore."));
226 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
232 void GearsLocalServer::OpenStore(JsCallContext
*context
) {
234 std::string16 required_cookie
;
235 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
238 LOG16((L
"LocalServer::OpenStore( %s, %s )\n",
239 name
.c_str(), required_cookie
.c_str()));
241 int64 existing_store_id
= WebCacheDB::kInvalidID
;
242 if (!ResourceStore::ExistsInDB(EnvPageSecurityOrigin(),
244 required_cookie
.c_str(),
245 &existing_store_id
)) {
246 context
->SetReturnValue(JSPARAM_NULL
, NULL
);
250 GComPtr
<GearsResourceStore
> store(
251 CreateModule
<GearsResourceStore
>(GetJsRunner()));
253 return; // Create function sets an error message.
255 if (!store
->InitBaseFromSibling(this)) {
256 context
->SetException(STRING16(L
"Error initializing base class."));
260 if (!store
->store_
.Open(existing_store_id
)) {
261 context
->SetException(STRING16(L
"Error initializing ResourceStore."));
265 context
->SetReturnValue(JSPARAM_MODULE
, store
.get());
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
271 void GearsLocalServer::RemoveStore(JsCallContext
*context
) {
273 std::string16 required_cookie
;
274 if (!GetAndCheckParameters(context
, &name
, &required_cookie
))
277 LOG16((L
"LocalServer::RemoveStore( %s, %s )\n",
278 name
.c_str(), required_cookie
.c_str()));
280 int64 existing_store_id
= WebCacheDB::kInvalidID
;
281 if (!ResourceStore::ExistsInDB(EnvPageSecurityOrigin(),
283 required_cookie
.c_str(),
284 &existing_store_id
)) {
289 if (!store
.Open(existing_store_id
)) {
290 context
->SetException(STRING16(L
"Error initializing ResourceStore."));
294 if (!store
.Remove()) {
295 context
->SetException(STRING16(L
"Error removing ResourceStore."));
300 //------------------------------------------------------------------------------
301 // GetAndCheckParameters
302 //------------------------------------------------------------------------------
303 bool GearsLocalServer::GetAndCheckParameters(JsCallContext
*context
,
305 std::string16
*required_cookie
) {
306 JsArgument argv
[] = {
307 { JSPARAM_REQUIRED
, JSPARAM_STRING16
, name
},
308 { JSPARAM_OPTIONAL
, JSPARAM_STRING16
, required_cookie
},
310 context
->GetArguments(ARRAYSIZE(argv
), argv
);
311 if (context
->is_exception_set())
314 // Validate parameters
316 context
->SetException(STRING16(L
"The name parameter is required."));
320 // TODO(michaeln): validate the required_cookie parameter value, parse the
321 // name & value, name must not be empty, value must not contain ';' unless
322 // it's the kNegatedRequiredCookieValue.
323 std::string16 error_message
;
324 if (!IsUserInputValidAsPathComponent(*name
, &error_message
)) {
325 context
->SetException(error_message
.c_str());