Back to index

lightning-sunbird  0.9+nobinonly
nsPluginDocument.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsMediaDocument.h"
00039 #include "nsIPluginDocument.h"
00040 #include "nsHTMLAtoms.h"
00041 #include "nsIPresShell.h"
00042 #include "nsIObjectFrame.h"
00043 #include "nsIPluginInstance.h"
00044 #include "nsIDocShellTreeItem.h"
00045 #include "nsNodeInfoManager.h"
00046 
00047 class nsPluginDocument : public nsMediaDocument,
00048                          public nsIPluginDocument
00049 {
00050 public:
00051   nsPluginDocument();
00052   virtual ~nsPluginDocument();
00053 
00054   NS_DECL_ISUPPORTS
00055   NS_DECL_NSIPLUGINDOCUMENT
00056 
00057   virtual nsresult StartDocumentLoad(const char*         aCommand,
00058                                      nsIChannel*         aChannel,
00059                                      nsILoadGroup*       aLoadGroup,
00060                                      nsISupports*        aContainer,
00061                                      nsIStreamListener** aDocListener,
00062                                      PRBool              aReset = PR_TRUE,
00063                                      nsIContentSink*     aSink = nsnull);
00064 
00065   virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject);
00066   virtual PRBool CanSavePresentation(nsIRequest *aNewRequest);
00067 
00068 protected:
00069   nsresult CreateSyntheticPluginDocument();
00070 
00071   nsCOMPtr<nsIContent>                     mPluginContent;
00072   nsRefPtr<nsMediaDocumentStreamListener>  mStreamListener;
00073   nsCString                                mMimeType;
00074 };
00075 
00076 
00077   // NOTE! nsDocument::operator new() zeroes out all members, so don't
00078   // bother initializing members to 0.
00079 
00080 nsPluginDocument::nsPluginDocument()
00081 {
00082 }
00083 
00084 nsPluginDocument::~nsPluginDocument()
00085 {
00086 }
00087 
00088 NS_IMPL_ADDREF_INHERITED(nsPluginDocument, nsMediaDocument)
00089 NS_IMPL_RELEASE_INHERITED(nsPluginDocument, nsMediaDocument)
00090 
00091 NS_INTERFACE_MAP_BEGIN(nsPluginDocument)
00092   NS_INTERFACE_MAP_ENTRY(nsIPluginDocument)
00093 NS_INTERFACE_MAP_END_INHERITING(nsMediaDocument)
00094 
00095 
00096 void
00097 nsPluginDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
00098 {
00099   if (!aScriptGlobalObject) {
00100     mStreamListener = nsnull;
00101   }
00102 
00103   nsMediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
00104 }
00105 
00106 
00107 PRBool
00108 nsPluginDocument::CanSavePresentation(nsIRequest *aNewRequest)
00109 {
00110   // Full-page plugins cannot be cached, currently, because we don't have
00111   // the stream listener data to feed to the plugin instance.
00112   return PR_FALSE;
00113 }
00114 
00115 
00116 nsresult
00117 nsPluginDocument::StartDocumentLoad(const char*         aCommand,
00118                                     nsIChannel*         aChannel,
00119                                     nsILoadGroup*       aLoadGroup,
00120                                     nsISupports*        aContainer,
00121                                     nsIStreamListener** aDocListener,
00122                                     PRBool              aReset,
00123                                     nsIContentSink*     aSink)
00124 {
00125   nsresult rv =
00126     nsMediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup,
00127                                        aContainer, aDocListener, aReset,
00128                                        aSink);
00129   if (NS_FAILED(rv)) {
00130     return rv;
00131   }
00132 
00133   rv = aChannel->GetContentType(mMimeType);
00134   if (NS_FAILED(rv)) {
00135     return rv;
00136   }
00137 
00138   // Create synthetic document
00139   rv = CreateSyntheticPluginDocument();
00140   if (NS_FAILED(rv)) {
00141     return rv;
00142   }
00143 
00144   mStreamListener = new nsMediaDocumentStreamListener(this);
00145   if (!mStreamListener)
00146     return NS_ERROR_OUT_OF_MEMORY;
00147   NS_ASSERTION(aDocListener, "null aDocListener");
00148   NS_ADDREF(*aDocListener = mStreamListener);
00149 
00150   return rv;
00151 }
00152 
00153 nsresult
00154 nsPluginDocument::CreateSyntheticPluginDocument()
00155 {
00156   // do not allow message panes to host full-page plugins
00157   // returning an error causes helper apps to take over
00158   nsCOMPtr<nsIDocShellTreeItem> dsti (do_QueryReferent(mDocumentContainer));
00159   if (dsti) {
00160     PRBool isMsgPane = PR_FALSE;
00161     dsti->NameEquals(NS_LITERAL_STRING("messagepane").get(), &isMsgPane);
00162     if (isMsgPane)
00163       return NS_ERROR_FAILURE;
00164   }
00165 
00166   // make our generic document
00167   nsresult rv = nsMediaDocument::CreateSyntheticDocument();
00168   NS_ENSURE_SUCCESS(rv, rv);
00169   // then attach our plugin
00170 
00171   nsCOMPtr<nsIContent> body = do_QueryInterface(mBodyContent);
00172   if (!body) {
00173     NS_WARNING("no body on plugin document!");
00174     return NS_ERROR_FAILURE;
00175   }
00176 
00177   // remove margins from body
00178   NS_NAMED_LITERAL_STRING(zero, "0");
00179   body->SetAttr(kNameSpaceID_None, nsHTMLAtoms::marginwidth, zero, PR_FALSE);
00180   body->SetAttr(kNameSpaceID_None, nsHTMLAtoms::marginheight, zero, PR_FALSE);
00181 
00182 
00183   // make plugin content
00184   nsCOMPtr<nsINodeInfo> nodeInfo;
00185   rv = mNodeInfoManager->GetNodeInfo(nsHTMLAtoms::embed, nsnull,
00186                                      kNameSpaceID_None,
00187                                     getter_AddRefs(nodeInfo));
00188   NS_ENSURE_SUCCESS(rv, rv);
00189   mPluginContent = NS_NewHTMLSharedElement(nodeInfo);
00190   if (!mPluginContent) {
00191     return NS_ERROR_OUT_OF_MEMORY;
00192   }
00193 
00194   // make it a named element
00195   mPluginContent->SetAttr(kNameSpaceID_None, nsHTMLAtoms::name,
00196                           NS_LITERAL_STRING("plugin"), PR_FALSE);
00197 
00198   // fill viewport and auto-resize
00199   NS_NAMED_LITERAL_STRING(percent100, "100%");
00200   mPluginContent->SetAttr(kNameSpaceID_None, nsHTMLAtoms::width, percent100,
00201                           PR_FALSE);
00202   mPluginContent->SetAttr(kNameSpaceID_None, nsHTMLAtoms::height, percent100,
00203                           PR_FALSE);
00204 
00205   // set URL
00206   nsCAutoString src;
00207   mDocumentURI->GetSpec(src);
00208   mPluginContent->SetAttr(kNameSpaceID_None, nsHTMLAtoms::src,
00209                           NS_ConvertUTF8toUTF16(src), PR_FALSE);
00210 
00211   // set mime type
00212   mPluginContent->SetAttr(kNameSpaceID_None, nsHTMLAtoms::type,
00213                           NS_ConvertUTF8toUTF16(mMimeType), PR_FALSE);
00214 
00215   body->AppendChildTo(mPluginContent, PR_FALSE);
00216 
00217   return NS_OK;
00218 
00219 
00220 }
00221 
00222 NS_IMETHODIMP
00223 nsPluginDocument::SetStreamListener(nsIStreamListener *aListener)
00224 {
00225   if (mStreamListener)
00226     mStreamListener->SetStreamListener(aListener);
00227 
00228   nsMediaDocument::UpdateTitleAndCharset(mMimeType);
00229 
00230   return NS_OK;
00231 }
00232 
00233 NS_IMETHODIMP
00234 nsPluginDocument::Print()
00235 {
00236   NS_ENSURE_TRUE(mPluginContent, NS_ERROR_FAILURE);
00237 
00238   nsIPresShell *shell = GetShellAt(0);
00239   if (!shell) {
00240     return NS_OK;
00241   }
00242 
00243   nsIFrame* frame = nsnull;
00244   shell->GetPrimaryFrameFor(mPluginContent, &frame);
00245 
00246   nsIObjectFrame* objectFrame = nsnull;
00247   CallQueryInterface(frame, &objectFrame);
00248 
00249   if (objectFrame) {
00250     nsCOMPtr<nsIPluginInstance> pi;
00251     objectFrame->GetPluginInstance(*getter_AddRefs(pi));
00252 
00253     if (pi) {
00254       nsPluginPrint npprint;
00255       npprint.mode = nsPluginMode_Full;
00256       npprint.print.fullPrint.pluginPrinted = PR_FALSE;
00257       npprint.print.fullPrint.printOne = PR_FALSE;
00258       npprint.print.fullPrint.platformPrint = nsnull;
00259 
00260       pi->Print(&npprint);
00261     }
00262   }
00263 
00264   return NS_OK;
00265 }
00266 
00267 nsresult
00268 NS_NewPluginDocument(nsIDocument** aResult)
00269 {
00270   nsPluginDocument* doc = new nsPluginDocument();
00271   if (!doc) {
00272     return NS_ERROR_OUT_OF_MEMORY;
00273   }
00274 
00275   NS_ADDREF(doc);
00276   nsresult rv = doc->Init();
00277 
00278   if (NS_FAILED(rv)) {
00279     NS_RELEASE(doc);
00280   }
00281 
00282   *aResult = doc;
00283 
00284   return rv;
00285 }