OLD | NEW |
| (Empty) |
1 // Copyright 2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/plugins/update/npapi/np_update.h" | |
17 | |
18 #include <atlbase.h> | |
19 #include <atlcom.h> | |
20 | |
21 #include "omaha/base/debug.h" | |
22 #include "omaha/base/scope_guard.h" | |
23 #include "omaha/base/string.h" | |
24 #include "omaha/plugins/update/config.h" | |
25 #include "omaha/plugins/update/npapi/dispatch_host.h" | |
26 #include "omaha/plugins/update/npapi/urlpropbag.h" | |
27 #include "plugins/update/activex/update_control_idl.h" | |
28 | |
29 NPError NS_PluginInitialize() { | |
30 return NPERR_NO_ERROR; | |
31 } | |
32 | |
33 void NS_PluginShutdown() { | |
34 } | |
35 | |
36 nsPluginInstanceBase* NS_NewPluginInstance(nsPluginCreateData* data) { | |
37 return new omaha::NPUpdate(data->instance, data->type); | |
38 } | |
39 | |
40 void NS_DestroyPluginInstance(nsPluginInstanceBase* plugin) { | |
41 delete plugin; | |
42 } | |
43 | |
44 namespace omaha { | |
45 | |
46 NPUpdate::NPUpdate(NPP instance, const char* mime_type) | |
47 : instance_(instance), | |
48 is_initialized_(false), | |
49 mime_type_(mime_type), | |
50 scriptable_object_(NULL) { | |
51 ASSERT1(instance); | |
52 // TODO(omaha): initialize COM | |
53 } | |
54 | |
55 NPUpdate::~NPUpdate() { | |
56 if (scriptable_object_) { | |
57 NPN_ReleaseObject(scriptable_object_); | |
58 } | |
59 } | |
60 | |
61 NPBool NPUpdate::init(NPWindow* np_window) { | |
62 UNREFERENCED_PARAMETER(np_window); | |
63 is_initialized_ = true; | |
64 return TRUE; | |
65 } | |
66 | |
67 void NPUpdate::shut() { | |
68 is_initialized_ = false; | |
69 } | |
70 | |
71 NPBool NPUpdate::isInitialized() { | |
72 // TODO(omaha): figure the right boolean type to return here... | |
73 return is_initialized_ ? TRUE : FALSE; | |
74 } | |
75 | |
76 NPError NPUpdate::GetValue(NPPVariable variable, void* value) { | |
77 if (!instance_) { | |
78 return NPERR_INVALID_INSTANCE_ERROR; | |
79 } | |
80 | |
81 if (NPPVpluginScriptableNPObject != variable || !value) { | |
82 return NPERR_INVALID_PARAM; | |
83 } | |
84 | |
85 CString url; | |
86 if (!GetCurrentBrowserUrl(&url) || !site_lock_.InApprovedDomain(url)) { | |
87 return NPERR_INVALID_URL; | |
88 } | |
89 | |
90 if (!scriptable_object_) { | |
91 CComPtr<IDispatch> p; | |
92 | |
93 CLSID clsid; | |
94 if (!MapMimeTypeToClsid(&clsid)) { | |
95 return NPERR_INVALID_PLUGIN_ERROR; | |
96 } | |
97 if (FAILED(p.CoCreateInstance(clsid))) { | |
98 return NPERR_OUT_OF_MEMORY_ERROR; | |
99 } | |
100 | |
101 // Store the current URL in a property bag and set it as the site of | |
102 // the object. | |
103 CComPtr<IPropertyBag> pb; | |
104 if (FAILED(UrlPropertyBag::Create(url, &pb))) { | |
105 return NPERR_GENERIC_ERROR; | |
106 } | |
107 CComPtr<IObjectWithSite> sited_obj; | |
108 if (FAILED(p.QueryInterface(&sited_obj))) { | |
109 return NPERR_GENERIC_ERROR; | |
110 } | |
111 if (FAILED(sited_obj->SetSite(pb))) { | |
112 return NPERR_GENERIC_ERROR; | |
113 } | |
114 | |
115 scriptable_object_ = DispatchHost::CreateInstance(instance_, p); | |
116 } | |
117 | |
118 if (scriptable_object_) { | |
119 NPN_RetainObject(scriptable_object_); | |
120 } else { | |
121 return NPERR_OUT_OF_MEMORY_ERROR; | |
122 } | |
123 | |
124 *(reinterpret_cast<NPObject**>(value)) = scriptable_object_; | |
125 return NPERR_NO_ERROR; | |
126 } | |
127 | |
128 bool NPUpdate::MapMimeTypeToClsid(CLSID* clsid) { | |
129 ASSERT1(clsid); | |
130 // TODO(omaha): We could probably abstract this out to a map that can | |
131 // have entries added to it at runtime, making this module fully generic. | |
132 // We could also consider extracting the MIME_TYPE resource from the current | |
133 // DLL and populating it from that. | |
134 if (0 == mime_type_.CompareNoCase(CString(UPDATE3WEB_MIME_TYPE))) { | |
135 *clsid = __uuidof(GoogleUpdate3WebControlCoClass); | |
136 return true; | |
137 } | |
138 if (0 == mime_type_.CompareNoCase(CString(ONECLICK_MIME_TYPE))) { | |
139 *clsid = __uuidof(GoogleUpdateOneClickControlCoClass); | |
140 return true; | |
141 } | |
142 return false; | |
143 } | |
144 | |
145 bool NPUpdate::GetCurrentBrowserUrl(CString* url) { | |
146 ASSERT1(url); | |
147 | |
148 NPObject* window = NULL; | |
149 NPError error = NPN_GetValue(instance_, NPNVWindowNPObject, &window); | |
150 if (NPERR_NO_ERROR != error || !window) { | |
151 ASSERT(false, (L"NPN_GetValue returned error %d", error)); | |
152 return false; | |
153 } | |
154 ON_SCOPE_EXIT(NPN_ReleaseObject, window); | |
155 | |
156 NPIdentifier location_id = NPN_GetStringIdentifier("location"); | |
157 NPVariant location; | |
158 NULL_TO_NPVARIANT(location); | |
159 if (!NPN_GetProperty(instance_, window, location_id, &location)) { | |
160 ASSERT1(false); | |
161 return false; | |
162 } | |
163 ON_SCOPE_EXIT(NPN_ReleaseVariantValue, &location); | |
164 if (!NPVARIANT_IS_OBJECT(location)) { | |
165 ASSERT(false, (L"Variant type: %d", location.type)); | |
166 return false; | |
167 } | |
168 | |
169 NPIdentifier href_id = NPN_GetStringIdentifier("href"); | |
170 NPVariant href; | |
171 NULL_TO_NPVARIANT(href); | |
172 if (!NPN_GetProperty(instance_, NPVARIANT_TO_OBJECT(location), href_id, | |
173 &href)) { | |
174 ASSERT1(false); | |
175 return false; | |
176 } | |
177 ON_SCOPE_EXIT(NPN_ReleaseVariantValue, &href); | |
178 if (!NPVARIANT_IS_STRING(href)) { | |
179 ASSERT(false, (L"Variant type: %d", href.type)); | |
180 return false; | |
181 } | |
182 | |
183 *url = Utf8ToWideChar(NPVARIANT_TO_STRING(href).UTF8Characters, | |
184 NPVARIANT_TO_STRING(href).UTF8Length); | |
185 return true; | |
186 } | |
187 | |
188 } // namespace omaha | |
OLD | NEW |