[Author: zork]
[google-gears.git] / gears / localserver / npapi / localserver_np.cc
bloba444ad4ed07b25e8fed0fd4880a8ba8b238cef01
1 // Copyright 2005, 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.
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);
37 // static
38 template<>
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 //-----------------------------------------------------------------------------
50 // CanServeLocally
51 //-----------------------------------------------------------------------------
52 void GearsLocalServer::CanServeLocally(JsCallContext *context) {
53 std::string16 url;
54 JsArgument argv[] = {
55 { JSPARAM_REQUIRED, JSPARAM_STRING16, &url },
57 context->GetArguments(ARRAYSIZE(argv), argv);
58 if (context->is_exception_set())
59 return;
61 std::string16 full_url;
62 if (!ResolveAndNormalize(EnvPageLocationUrl().c_str(), url.c_str(),
63 &full_url)) {
64 context->SetException(STRING16(L"Failed to resolve url."));
65 return;
67 if (!EnvPageSecurityOrigin().IsSameOriginAsUrl(full_url.c_str())) {
68 context->SetException(STRING16(L"Url is not from the same origin"));
69 return;
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 //-----------------------------------------------------------------------------
79 // CreateManagedStore
80 //-----------------------------------------------------------------------------
81 void GearsLocalServer::CreateManagedStore(JsCallContext *context) {
82 std::string16 name;
83 std::string16 required_cookie;
84 if (!GetAndCheckParameters(context, &name, &required_cookie))
85 return;
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."));
91 return;
94 LOG16((L"LocalServer::CreateManagedStore( %s, %s )\n",
95 name.c_str(), required_cookie.c_str()));
97 GComPtr<GearsManagedResourceStore> store(
98 CreateModule<GearsManagedResourceStore>(GetJsRunner()));
99 if (!store.get())
100 return; // Create function sets an error message.
102 if (!store->InitBaseFromSibling(this)) {
103 context->SetException(STRING16(L"Error initializing base class."));
104 return;
107 if (!store->store_.CreateOrOpen(EnvPageSecurityOrigin(),
108 name.c_str(), required_cookie.c_str())) {
109 context->SetException(
110 STRING16(L"Error initializing ManagedResourceStore."));
111 return;
114 context->SetReturnValue(JSPARAM_MODULE, store.get());
117 //-----------------------------------------------------------------------------
118 // OpenManagedStore
119 //-----------------------------------------------------------------------------
120 void GearsLocalServer::OpenManagedStore(JsCallContext *context) {
121 std::string16 name;
122 std::string16 required_cookie;
123 if (!GetAndCheckParameters(context, &name, &required_cookie))
124 return;
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(),
131 name.c_str(),
132 required_cookie.c_str(),
133 &existing_store_id)) {
134 context->SetReturnValue(JSPARAM_NULL, NULL);
135 return;
138 GComPtr<GearsManagedResourceStore> store(
139 CreateModule<GearsManagedResourceStore>(GetJsRunner()));
140 if (!store.get())
141 return; // Create function sets an error message.
143 if (!store->InitBaseFromSibling(this)) {
144 context->SetException(STRING16(L"Error initializing base class."));
145 return;
148 if (!store->store_.Open(existing_store_id)) {
149 context->SetException(
150 STRING16(L"Error initializing ManagedResourceStore."));
151 return;
154 context->SetReturnValue(JSPARAM_MODULE, store.get());
157 //-----------------------------------------------------------------------------
158 // RemoveManagedStore
159 //-----------------------------------------------------------------------------
160 void GearsLocalServer::RemoveManagedStore(JsCallContext *context) {
161 std::string16 name;
162 std::string16 required_cookie;
163 if (!GetAndCheckParameters(context, &name, &required_cookie))
164 return;
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(),
171 name.c_str(),
172 required_cookie.c_str(),
173 &existing_store_id)) {
174 context->SetReturnValue(JSPARAM_NULL, NULL);
175 return;
178 ManagedResourceStore store;
179 if (!store.Open(existing_store_id)) {
180 context->SetException(
181 STRING16(L"Error initializing ManagedResourceStore."));
182 return;
185 if (!store.Remove()) {
186 context->SetException(STRING16(L"Error removing ManagedResourceStore."));
187 return;
191 //-----------------------------------------------------------------------------
192 // CreateStore
193 //-----------------------------------------------------------------------------
194 void GearsLocalServer::CreateStore(JsCallContext *context) {
195 std::string16 name;
196 std::string16 required_cookie;
197 if (!GetAndCheckParameters(context, &name, &required_cookie))
198 return;
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."));
204 return;
207 LOG16((L"LocalServer::CreateStore( %s, %s )\n",
208 name.c_str(), required_cookie.c_str()));
210 GComPtr<GearsResourceStore> store(
211 CreateModule<GearsResourceStore>(GetJsRunner()));
212 if (!store.get())
213 return; // Create function sets an error message.
215 if (!store->InitBaseFromSibling(this)) {
216 context->SetException(STRING16(L"Error initializing base class."));
217 return;
220 if (!store->store_.CreateOrOpen(EnvPageSecurityOrigin(),
221 name.c_str(), required_cookie.c_str())) {
222 context->SetException(STRING16(L"Error initializing ResourceStore."));
223 return;
226 context->SetReturnValue(JSPARAM_MODULE, store.get());
229 //-----------------------------------------------------------------------------
230 // OpenStore
231 //-----------------------------------------------------------------------------
232 void GearsLocalServer::OpenStore(JsCallContext *context) {
233 std::string16 name;
234 std::string16 required_cookie;
235 if (!GetAndCheckParameters(context, &name, &required_cookie))
236 return;
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(),
243 name.c_str(),
244 required_cookie.c_str(),
245 &existing_store_id)) {
246 context->SetReturnValue(JSPARAM_NULL, NULL);
247 return;
250 GComPtr<GearsResourceStore> store(
251 CreateModule<GearsResourceStore>(GetJsRunner()));
252 if (!store.get())
253 return; // Create function sets an error message.
255 if (!store->InitBaseFromSibling(this)) {
256 context->SetException(STRING16(L"Error initializing base class."));
257 return;
260 if (!store->store_.Open(existing_store_id)) {
261 context->SetException(STRING16(L"Error initializing ResourceStore."));
262 return;
265 context->SetReturnValue(JSPARAM_MODULE, store.get());
268 //-----------------------------------------------------------------------------
269 // RemoveStore
270 //-----------------------------------------------------------------------------
271 void GearsLocalServer::RemoveStore(JsCallContext *context) {
272 std::string16 name;
273 std::string16 required_cookie;
274 if (!GetAndCheckParameters(context, &name, &required_cookie))
275 return;
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(),
282 name.c_str(),
283 required_cookie.c_str(),
284 &existing_store_id)) {
285 return;
288 ResourceStore store;
289 if (!store.Open(existing_store_id)) {
290 context->SetException(STRING16(L"Error initializing ResourceStore."));
291 return;
294 if (!store.Remove()) {
295 context->SetException(STRING16(L"Error removing ResourceStore."));
296 return;
300 //------------------------------------------------------------------------------
301 // GetAndCheckParameters
302 //------------------------------------------------------------------------------
303 bool GearsLocalServer::GetAndCheckParameters(JsCallContext *context,
304 std::string16 *name,
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())
312 return false;
314 // Validate parameters
315 if (name->empty()) {
316 context->SetException(STRING16(L"The name parameter is required."));
317 return false;
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());
326 return false;
329 return true;