[Author: mpcomplete]
[google-gears.git] / gears / localserver / npapi / localserver_np.cc
blob084169b35a3fc489421021e19836ab9b36048156
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/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);
35 // static
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 //-----------------------------------------------------------------------------
47 // CanServeLocally
48 //-----------------------------------------------------------------------------
49 void GearsLocalServer::CanServeLocally(JsCallContext *context) {
50 bool retval = false;
51 context->SetReturnValue(JSPARAM_BOOL, &retval);
52 context->SetException(STRING16(L"Not Implemented"));
55 //-----------------------------------------------------------------------------
56 // CreateManagedStore
57 //-----------------------------------------------------------------------------
58 void GearsLocalServer::CreateManagedStore(JsCallContext *context) {
59 std::string16 name;
60 std::string16 required_cookie;
61 if (!GetAndCheckParameters(context, &name, &required_cookie))
62 return;
64 GComPtr<GearsManagedResourceStore> store(
65 CreateModule<GearsManagedResourceStore>(EnvPageJsContext()));
66 if (!store.get())
67 return; // Create function sets an error message.
69 if (!store->InitBaseFromSibling(this)) {
70 context->SetException(STRING16(L"Error initializing base class."));
71 return;
74 context->SetReturnValue(JSPARAM_MODULE, store.get());
75 context->SetException(STRING16(L"Not Implemented"));
78 //-----------------------------------------------------------------------------
79 // OpenManagedStore
80 //-----------------------------------------------------------------------------
81 void GearsLocalServer::OpenManagedStore(JsCallContext *context) {
82 std::string16 name;
83 std::string16 required_cookie;
84 if (!GetAndCheckParameters(context, &name, &required_cookie))
85 return;
87 context->SetReturnValue(JSPARAM_NULL, NULL);
88 context->SetException(STRING16(L"Not Implemented"));
91 //-----------------------------------------------------------------------------
92 // RemoveManagedStore
93 //-----------------------------------------------------------------------------
94 void GearsLocalServer::RemoveManagedStore(JsCallContext *context) {
95 std::string16 name;
96 std::string16 required_cookie;
97 if (!GetAndCheckParameters(context, &name, &required_cookie))
98 return;
100 context->SetException(STRING16(L"Not Implemented"));
103 //-----------------------------------------------------------------------------
104 // CreateStore
105 //-----------------------------------------------------------------------------
106 void GearsLocalServer::CreateStore(JsCallContext *context) {
107 std::string16 name;
108 std::string16 required_cookie;
109 if (!GetAndCheckParameters(context, &name, &required_cookie))
110 return;
112 GComPtr<GearsResourceStore> store(
113 CreateModule<GearsResourceStore>(EnvPageJsContext()));
114 if (!store.get())
115 return; // Create function sets an error message.
117 if (!store->InitBaseFromSibling(this)) {
118 context->SetException(STRING16(L"Error initializing base class."));
119 return;
122 context->SetReturnValue(JSPARAM_MODULE, store.get());
123 context->SetException(STRING16(L"Not Implemented"));
126 //-----------------------------------------------------------------------------
127 // OpenStore
128 //-----------------------------------------------------------------------------
129 void GearsLocalServer::OpenStore(JsCallContext *context) {
130 std::string16 name;
131 std::string16 required_cookie;
132 if (!GetAndCheckParameters(context, &name, &required_cookie))
133 return;
135 context->SetReturnValue(JSPARAM_NULL, NULL);
136 context->SetException(STRING16(L"Not Implemented"));
139 //-----------------------------------------------------------------------------
140 // RemoveStore
141 //-----------------------------------------------------------------------------
142 void GearsLocalServer::RemoveStore(JsCallContext *context) {
143 std::string16 name;
144 std::string16 required_cookie;
145 if (!GetAndCheckParameters(context, &name, &required_cookie))
146 return;
148 context->SetException(STRING16(L"Not Implemented"));
151 //------------------------------------------------------------------------------
152 // GetAndCheckParameters
153 //------------------------------------------------------------------------------
154 bool GearsLocalServer::GetAndCheckParameters(JsCallContext *context,
155 std::string16 *name,
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())
163 return false;
165 // Validate parameters
166 if (name->empty()) {
167 context->SetException(STRING16(L"The name parameter is required."));
168 return false;
171 std::string16 error_message;
172 if (!IsUserInputValidAsPathComponent(*name, &error_message)) {
173 context->SetException(error_message.c_str());
174 return false;
177 return true;