mContext = aPresContext; mOwner = aFrame;
NS_IMETHODIMP nsPluginInstanceOwner::Init(nsIPresContext* aPresContext, nsObjectFrame *aFrame){  //do not addref to avoid circular refs. MMP  mContext = aPresContext;  mOwner = aFrame;    nsCOMPtr<nsIContent> content;  mOwner->GetContent(getter_AddRefs(content));    // This is way of ensure the previous document is gone. Important when reloading either   // the page or refreshing plugins. In the case of an OBJECT frame,  // we want to flush out the prevous content viewer which will cause the previous document  // and plugins to be cleaned up. Then we can create our new plugin without the old instance  // hanging around.  nsCOMPtr<nsISupports> container;  aPresContext->GetContainer(getter_AddRefs(container));  if (container) {    nsCOMPtr<nsIDocShell> cvc(do_QueryInterface(container));    if (cvc) {      nsCOMPtr<nsIContentViewer> cv;      cvc->GetContentViewer(getter_AddRefs(cv));      if (cv)        cv->Show();    }  }  // register context menu listener  mCXMenuListener = new nsPluginDOMContextMenuListener();  if (mCXMenuListener) {        NS_ADDREF(mCXMenuListener);        mCXMenuListener->Init(aFrame);  }  // Register focus listener  if (content) {    nsCOMPtr<nsIDOMEventReceiver> receiver(do_QueryInterface(content));    if (receiver) {      nsCOMPtr<nsIDOMFocusListener> focusListener;      QueryInterface(NS_GET_IID(nsIDOMFocusListener), getter_AddRefs(focusListener));      if (focusListener) {        receiver->AddEventListenerByIID(focusListener, NS_GET_IID(nsIDOMFocusListener));      }    }  }  // Register mouse listener  if (content) {    nsCOMPtr<nsIDOMEventReceiver> receiver(do_QueryInterface(content));    if (receiver) {      nsCOMPtr<nsIDOMMouseListener> mouseListener;      QueryInterface(NS_GET_IID(nsIDOMMouseListener), getter_AddRefs(mouseListener));      if (mouseListener) {        receiver->AddEventListenerByIID(mouseListener, NS_GET_IID(nsIDOMMouseListener));      }      // now do the mouse motion listener      nsCOMPtr<nsIDOMMouseMotionListener> mouseMotionListener;      QueryInterface(NS_GET_IID(nsIDOMMouseMotionListener), getter_AddRefs(mouseMotionListener));      if (mouseMotionListener) {        receiver->AddEventListenerByIID(mouseMotionListener, NS_GET_IID(nsIDOMMouseMotionListener));      }    }  }  // Register key listener  if (content) {    nsCOMPtr<nsIDOMEventReceiver> receiver(do_QueryInterface(content));    if (receiver) {      nsCOMPtr<nsIDOMKeyListener> keyListener;      QueryInterface(NS_GET_IID(nsIDOMKeyListener), getter_AddRefs(keyListener));      if (keyListener) {        receiver->AddEventListener(NS_LITERAL_STRING("keypress"), keyListener, PR_TRUE);        receiver->AddEventListener(NS_LITERAL_STRING("keydown"), keyListener, PR_TRUE);        receiver->AddEventListener(NS_LITERAL_STRING("keyup"), keyListener, PR_TRUE);      }    }  }    // Register scroll position listener  if (mContext) {    nsCOMPtr<nsIPresShell> presShell;    mContext->GetShell(getter_AddRefs(presShell));    if (presShell) {      nsCOMPtr<nsIViewManager> vm;      presShell->GetViewManager(getter_AddRefs(vm));      if (vm) {        nsIScrollableView* scrollingView = nsnull;        vm->GetRootScrollableView(&scrollingView);        if (scrollingView) {          scrollingView->AddScrollPositionListener((nsIScrollPositionListener *)this);        }      }    }  }  return NS_OK; }