[Author: nicolasroard]
[google-gears.git] / gears / ui / ie / html_dialog_bridge_iemobile.cc
blob8de8f9d6db2593dda155e0b9ac55eedf0ebe6b89
1 // Copyright 2007, 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 <assert.h>
27 #include <oleidl.h>
28 #include <piedocvw.h>
29 #include <pvdispid.h>
30 #include <shlguid.h>
31 #include <webvw.h>
32 #include <windows.h>
34 #include "gears/base/common/common.h"
35 #include "gears/base/common/thread_locals.h"
36 #include "gears/base/ie/activex_utils.h"
37 #include "gears/base/ie/atl_headers.h"
38 #include "gears/ui/ie/html_dialog_bridge_iemobile.h"
39 #include "gears/third_party/AtlActiveScriptSite.h"
41 // Set the permissions dialog object
42 // window.external does not work on PIE, so we use this instead...
43 // basically we get the browser object used by the permissions dialog
44 // and traverse the dom to find a PIEDialogBridge instance. If there is one,
45 // we can safely set the dialog pointer.
47 HRESULT PIEDialogBridge::AccessPermissionsDialog() {
48 // First we get the global permissions dialog object...
49 // (permissions dialog is modal)
51 CComQIPtr<HtmlDialogHostInterface> permissions_dialog(
52 HtmlDialogHost::html_permissions_dialog_);
54 if (!permissions_dialog)
55 return S_FALSE;
57 HtmlDialogHost* html_dialog = HtmlDialogHost::html_permissions_dialog_;
58 CComPtr<IPIEHTMLDocument2> document(html_dialog->document_);
60 if (!document) return S_FALSE;
62 // then we retrieve all the elements in the document, and we look for us
63 CComPtr<IPIEHTMLElementCollection> html_collection;
64 HRESULT hr = document->get_all(&html_collection);
65 ASSERT(html_collection);
67 long len = 0;
68 hr = html_collection->get_length(&len);
69 if (hr != S_OK)
70 return S_FALSE;
72 for (int i = 0; i < len; i++) {
73 VARIANT index;
74 VariantInit(&index);
75 index.vt = VT_I4;
76 index.intVal = static_cast<int>(i);
78 CComPtr<IDispatch> disp;
79 hr = html_collection->item(index, index, &disp);
81 if (disp) {
82 // get the current element
83 CComQIPtr<IPIEHTMLObjectElement> elem(disp);
85 // if we have an object element, we check that it's a
86 // PIEDialogBridge object; we only set one object per document.
87 if (elem) {
88 CComQIPtr<PIEDialogBridgeInterface> bridge_pointer;
89 CComPtr<IDispatch> disp_object;
90 hr = elem->get_object(&disp_object);
91 bridge_pointer = disp_object;
92 if (bridge_pointer) {
93 bridge_pointer->SetDialog(permissions_dialog);
94 return S_OK;
99 return S_FALSE;
102 HRESULT PIEDialogBridge::SetDialog(IDispatch* dialog) {
103 if (!dialog_) {
104 dialog_ = dialog;
105 if (dialog_) {
106 return S_OK;
107 } else {
108 return S_FALSE;
111 return S_OK;
114 HRESULT PIEDialogBridge::IsPocketIE(VARIANT_BOOL *retval) {
115 if (!dialog_) {
116 AccessPermissionsDialog();
118 if (dialog_) {
119 return dialog_->IsPocketIE(retval);
121 return S_FALSE;
124 HRESULT PIEDialogBridge::GetDialogArguments(BSTR *args_string) {
125 if (!dialog_) {
126 AccessPermissionsDialog();
128 if (dialog_) {
129 HRESULT hr = dialog_->GetDialogArguments(args_string);
130 return hr;
132 return S_FALSE;
135 HRESULT PIEDialogBridge::CloseDialog(const BSTR result_string) {
136 if (!dialog_) {
137 AccessPermissionsDialog();
139 if (dialog_) {
140 return dialog_->CloseDialog(result_string);
142 return S_FALSE;