LCOV - code coverage report
Current view: top level - editor/composer - nsComposerDocumentCommands.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 235 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 13 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : 
       7             : #include "mozilla/HTMLEditor.h"         // for HTMLEditor
       8             : #include "nsCOMPtr.h"                   // for nsCOMPtr, do_QueryInterface, etc
       9             : #include "nsCRT.h"                      // for nsCRT
      10             : #include "nsComposerCommands.h"         // for nsSetDocumentOptionsCommand, etc
      11             : #include "nsDebug.h"                    // for NS_ENSURE_ARG_POINTER, etc
      12             : #include "nsError.h"                    // for NS_ERROR_INVALID_ARG, etc
      13             : #include "nsICommandParams.h"           // for nsICommandParams
      14             : #include "nsIDOMDocument.h"             // for nsIDOMDocument
      15             : #include "nsIDocShell.h"                // for nsIDocShell
      16             : #include "nsIDocument.h"                // for nsIDocument
      17             : #include "nsIEditingSession.h"          // for nsIEditingSession, etc
      18             : #include "nsIEditor.h"                  // for nsIEditor
      19             : #include "nsIHTMLEditor.h"              // for nsIHTMLEditor
      20             : #include "nsIHTMLInlineTableEditor.h"   // for nsIHTMLInlineTableEditor
      21             : #include "nsIHTMLObjectResizer.h"       // for nsIHTMLObjectResizer
      22             : #include "nsIPlaintextEditor.h"         // for nsIPlaintextEditor, etc
      23             : #include "nsIPresShell.h"               // for nsIPresShell
      24             : #include "nsISelectionController.h"     // for nsISelectionController
      25             : #include "nsISupportsImpl.h"            // for nsPresContext::Release
      26             : #include "nsISupportsUtils.h"           // for NS_IF_ADDREF
      27             : #include "nsIURI.h"                     // for nsIURI
      28             : #include "nsPresContext.h"              // for nsPresContext
      29             : #include "nscore.h"                     // for NS_IMETHODIMP, nsresult, etc
      30             : 
      31             : using namespace mozilla;
      32             : 
      33             : class nsISupports;
      34             : 
      35             : //defines
      36             : #define STATE_ENABLED  "state_enabled"
      37             : #define STATE_ALL "state_all"
      38             : #define STATE_ATTRIBUTE "state_attribute"
      39             : #define STATE_DATA "state_data"
      40             : 
      41             : static
      42             : nsresult
      43           0 : GetPresContextFromEditor(nsIEditor *aEditor, nsPresContext **aResult)
      44             : {
      45           0 :   NS_ENSURE_ARG_POINTER(aResult);
      46           0 :   *aResult = nullptr;
      47           0 :   NS_ENSURE_ARG_POINTER(aEditor);
      48             : 
      49           0 :   nsCOMPtr<nsISelectionController> selCon;
      50           0 :   nsresult rv = aEditor->GetSelectionController(getter_AddRefs(selCon));
      51           0 :   NS_ENSURE_SUCCESS(rv, rv);
      52           0 :   NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
      53             : 
      54           0 :   nsCOMPtr<nsIPresShell> presShell = do_QueryInterface(selCon);
      55           0 :   NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
      56             : 
      57           0 :   NS_IF_ADDREF(*aResult = presShell->GetPresContext());
      58           0 :   return NS_OK;
      59             : }
      60             : 
      61             : NS_IMETHODIMP
      62           0 : nsSetDocumentOptionsCommand::IsCommandEnabled(const char * aCommandName,
      63             :                                               nsISupports *refCon,
      64             :                                               bool *outCmdEnabled)
      65             : {
      66           0 :   NS_ENSURE_ARG_POINTER(outCmdEnabled);
      67           0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
      68           0 :   if (editor) {
      69           0 :     return editor->GetIsSelectionEditable(outCmdEnabled);
      70             :   }
      71             : 
      72           0 :   *outCmdEnabled = false;
      73           0 :   return NS_OK;
      74             : }
      75             : 
      76             : NS_IMETHODIMP
      77           0 : nsSetDocumentOptionsCommand::DoCommand(const char *aCommandName,
      78             :                                        nsISupports *refCon)
      79             : {
      80           0 :   return NS_ERROR_NOT_IMPLEMENTED;
      81             : }
      82             : 
      83             : NS_IMETHODIMP
      84           0 : nsSetDocumentOptionsCommand::DoCommandParams(const char *aCommandName,
      85             :                                              nsICommandParams *aParams,
      86             :                                              nsISupports *refCon)
      87             : {
      88           0 :   NS_ENSURE_ARG_POINTER(aParams);
      89             : 
      90           0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
      91           0 :   NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
      92             : 
      93           0 :   RefPtr<nsPresContext> presContext;
      94           0 :   nsresult rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext));
      95           0 :   NS_ENSURE_SUCCESS(rv, rv);
      96           0 :   NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
      97             : 
      98             :   int32_t animationMode;
      99           0 :   rv = aParams->GetLongValue("imageAnimation", &animationMode);
     100           0 :   if (NS_SUCCEEDED(rv)) {
     101             :     // for possible values of animation mode, see:
     102             :     // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
     103           0 :     presContext->SetImageAnimationMode(animationMode);
     104             :   }
     105             : 
     106             :   bool allowPlugins;
     107           0 :   rv = aParams->GetBooleanValue("plugins", &allowPlugins);
     108           0 :   if (NS_SUCCEEDED(rv)) {
     109           0 :     nsCOMPtr<nsIDocShell> docShell(presContext->GetDocShell());
     110           0 :     NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
     111             : 
     112           0 :     rv = docShell->SetAllowPlugins(allowPlugins);
     113           0 :     NS_ENSURE_SUCCESS(rv, rv);
     114             :   }
     115             : 
     116           0 :   return NS_OK;
     117             : }
     118             : 
     119             : NS_IMETHODIMP
     120           0 : nsSetDocumentOptionsCommand::GetCommandStateParams(const char *aCommandName,
     121             :                                                    nsICommandParams *aParams,
     122             :                                                    nsISupports *refCon)
     123             : {
     124           0 :   NS_ENSURE_ARG_POINTER(aParams);
     125           0 :   NS_ENSURE_ARG_POINTER(refCon);
     126             : 
     127             :   // The base editor owns most state info
     128           0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     129           0 :   NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
     130             : 
     131             :   // Always get the enabled state
     132           0 :   bool outCmdEnabled = false;
     133           0 :   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
     134           0 :   nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
     135           0 :   NS_ENSURE_SUCCESS(rv, rv);
     136             : 
     137             :   // get pres context
     138           0 :   RefPtr<nsPresContext> presContext;
     139           0 :   rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext));
     140           0 :   NS_ENSURE_SUCCESS(rv, rv);
     141           0 :   NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
     142             : 
     143             :   int32_t animationMode;
     144           0 :   rv = aParams->GetLongValue("imageAnimation", &animationMode);
     145           0 :   if (NS_SUCCEEDED(rv)) {
     146             :     // for possible values of animation mode, see
     147             :     // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
     148           0 :     rv = aParams->SetLongValue("imageAnimation",
     149           0 :                                presContext->ImageAnimationMode());
     150           0 :     NS_ENSURE_SUCCESS(rv, rv);
     151             :   }
     152             : 
     153           0 :   bool allowPlugins = false;
     154           0 :   rv = aParams->GetBooleanValue("plugins", &allowPlugins);
     155           0 :   if (NS_SUCCEEDED(rv)) {
     156           0 :     nsCOMPtr<nsIDocShell> docShell(presContext->GetDocShell());
     157           0 :     NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
     158             : 
     159           0 :     allowPlugins = docShell->PluginsAllowedInCurrentDoc();
     160             : 
     161           0 :     rv = aParams->SetBooleanValue("plugins", allowPlugins);
     162           0 :     NS_ENSURE_SUCCESS(rv, rv);
     163             :   }
     164             : 
     165           0 :   return NS_OK;
     166             : }
     167             : 
     168             : 
     169             : /**
     170             :  *  Commands for document state that may be changed via doCommandParams
     171             :  *  As of 11/11/02, this is just "cmd_setDocumentModified"
     172             :  *  Note that you can use the same command class, nsSetDocumentStateCommand,
     173             :  *    for more than one of this type of command
     174             :  *    We check the input command param for different behavior
     175             :  */
     176             : 
     177             : NS_IMETHODIMP
     178           0 : nsSetDocumentStateCommand::IsCommandEnabled(const char * aCommandName,
     179             :                                             nsISupports *refCon,
     180             :                                             bool *outCmdEnabled)
     181             : {
     182             :   // These commands are always enabled
     183           0 :   NS_ENSURE_ARG_POINTER(outCmdEnabled);
     184           0 :   *outCmdEnabled = true;
     185           0 :   return NS_OK;
     186             : }
     187             : 
     188             : NS_IMETHODIMP
     189           0 : nsSetDocumentStateCommand::DoCommand(const char *aCommandName,
     190             :                                      nsISupports *refCon)
     191             : {
     192           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     193             : }
     194             : 
     195             : NS_IMETHODIMP
     196           0 : nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName,
     197             :                                            nsICommandParams *aParams,
     198             :                                            nsISupports *refCon)
     199             : {
     200           0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     201           0 :   NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
     202             : 
     203           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified")) {
     204           0 :     NS_ENSURE_ARG_POINTER(aParams);
     205             : 
     206             :     bool modified;
     207           0 :     nsresult rv = aParams->GetBooleanValue(STATE_ATTRIBUTE, &modified);
     208             : 
     209             :     // Should we fail if this param wasn't set?
     210             :     // I'm not sure we should be that strict
     211           0 :     NS_ENSURE_SUCCESS(rv, rv);
     212             : 
     213           0 :     if (modified) {
     214           0 :       return editor->IncrementModificationCount(1);
     215             :     }
     216             : 
     217           0 :     return editor->ResetModificationCount();
     218             :   }
     219             : 
     220           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly")) {
     221           0 :     NS_ENSURE_ARG_POINTER(aParams);
     222             :     bool isReadOnly;
     223           0 :     nsresult rvRO = aParams->GetBooleanValue(STATE_ATTRIBUTE, &isReadOnly);
     224           0 :     NS_ENSURE_SUCCESS(rvRO, rvRO);
     225             : 
     226             :     uint32_t flags;
     227           0 :     editor->GetFlags(&flags);
     228           0 :     if (isReadOnly) {
     229           0 :       flags |= nsIPlaintextEditor::eEditorReadonlyMask;
     230             :     } else {
     231           0 :       flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
     232             :     }
     233             : 
     234           0 :     return editor->SetFlags(flags);
     235             :   }
     236             : 
     237           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS")) {
     238           0 :     NS_ENSURE_ARG_POINTER(aParams);
     239           0 :     nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
     240           0 :     NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
     241             : 
     242             :     bool desireCSS;
     243           0 :     nsresult rvCSS = aParams->GetBooleanValue(STATE_ATTRIBUTE, &desireCSS);
     244           0 :     NS_ENSURE_SUCCESS(rvCSS, rvCSS);
     245             : 
     246           0 :     return htmleditor->SetIsCSSEnabled(desireCSS);
     247             :   }
     248             : 
     249           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn")) {
     250           0 :     NS_ENSURE_ARG_POINTER(aParams);
     251           0 :     nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
     252           0 :     NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
     253             : 
     254             :     bool insertBrOnReturn;
     255             :     nsresult rvBR = aParams->GetBooleanValue(STATE_ATTRIBUTE,
     256           0 :                                               &insertBrOnReturn);
     257           0 :     NS_ENSURE_SUCCESS(rvBR, rvBR);
     258             : 
     259           0 :     return htmleditor->SetReturnInParagraphCreatesNewParagraph(!insertBrOnReturn);
     260             :   }
     261             : 
     262           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
     263           0 :     if (NS_WARN_IF(!aParams)) {
     264           0 :       return NS_ERROR_NULL_POINTER;
     265             :     }
     266           0 :     nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
     267           0 :     if (NS_WARN_IF(!editor)) {
     268           0 :       return NS_ERROR_INVALID_ARG;
     269             :     }
     270           0 :     auto htmlEditor = static_cast<HTMLEditor*>(editor.get());
     271             : 
     272           0 :     nsXPIDLCString newValue;
     273           0 :     nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE,
     274           0 :                                            getter_Copies(newValue));
     275           0 :     if (NS_WARN_IF(NS_FAILED(rv))) {
     276           0 :       return rv;
     277             :     }
     278             : 
     279           0 :     if (newValue.LowerCaseEqualsLiteral("div")) {
     280           0 :       htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::div);
     281           0 :       return NS_OK;
     282             :     }
     283           0 :     if (newValue.LowerCaseEqualsLiteral("p")) {
     284           0 :       htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::p);
     285           0 :       return NS_OK;
     286             :     }
     287           0 :     if (newValue.LowerCaseEqualsLiteral("br")) {
     288             :       // Mozilla extension for backwards compatibility
     289           0 :       htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::br);
     290           0 :       return NS_OK;
     291             :     }
     292             : 
     293             :     // This should not be reachable from nsHTMLDocument::ExecCommand
     294           0 :     NS_WARNING("Invalid default paragraph separator");
     295           0 :     return NS_ERROR_UNEXPECTED;
     296             :   }
     297             : 
     298           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) {
     299           0 :     NS_ENSURE_ARG_POINTER(aParams);
     300           0 :     nsCOMPtr<nsIHTMLObjectResizer> resizer = do_QueryInterface(refCon);
     301           0 :     NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG);
     302             : 
     303             :     bool enabled;
     304           0 :     nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled);
     305           0 :     NS_ENSURE_SUCCESS(rvOR, rvOR);
     306             : 
     307           0 :     return resizer->SetObjectResizingEnabled(enabled);
     308             :   }
     309             : 
     310           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
     311           0 :     NS_ENSURE_ARG_POINTER(aParams);
     312           0 :     nsCOMPtr<nsIHTMLInlineTableEditor> editor = do_QueryInterface(refCon);
     313           0 :     NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
     314             : 
     315             :     bool enabled;
     316           0 :     nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled);
     317           0 :     NS_ENSURE_SUCCESS(rvOR, rvOR);
     318             : 
     319           0 :     return editor->SetInlineTableEditingEnabled(enabled);
     320             :   }
     321             : 
     322           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     323             : }
     324             : 
     325             : NS_IMETHODIMP
     326           0 : nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
     327             :                                                  nsICommandParams *aParams,
     328             :                                                  nsISupports *refCon)
     329             : {
     330           0 :   NS_ENSURE_ARG_POINTER(aParams);
     331           0 :   NS_ENSURE_ARG_POINTER(refCon);
     332             : 
     333             :   // The base editor owns most state info
     334           0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     335           0 :   NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
     336             : 
     337             :   // Always get the enabled state
     338           0 :   bool outCmdEnabled = false;
     339           0 :   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
     340           0 :   nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
     341           0 :   NS_ENSURE_SUCCESS(rv, rv);
     342             : 
     343           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified")) {
     344             :     bool modified;
     345           0 :     rv = editor->GetDocumentModified(&modified);
     346           0 :     NS_ENSURE_SUCCESS(rv, rv);
     347             : 
     348           0 :     return aParams->SetBooleanValue(STATE_ATTRIBUTE, modified);
     349             :   }
     350             : 
     351           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly")) {
     352           0 :     NS_ENSURE_ARG_POINTER(aParams);
     353             : 
     354             :     uint32_t flags;
     355           0 :     editor->GetFlags(&flags);
     356           0 :     bool isReadOnly = flags & nsIPlaintextEditor::eEditorReadonlyMask;
     357           0 :     return aParams->SetBooleanValue(STATE_ATTRIBUTE, isReadOnly);
     358             :   }
     359             : 
     360           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS")) {
     361           0 :     NS_ENSURE_ARG_POINTER(aParams);
     362           0 :     nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
     363           0 :     NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
     364             : 
     365             :     bool isCSS;
     366           0 :     htmleditor->GetIsCSSEnabled(&isCSS);
     367           0 :     return aParams->SetBooleanValue(STATE_ALL, isCSS);
     368             :   }
     369             : 
     370           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn")) {
     371           0 :     NS_ENSURE_ARG_POINTER(aParams);
     372           0 :     nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
     373           0 :     NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
     374             : 
     375             :     bool createPOnReturn;
     376           0 :     htmleditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn);
     377           0 :     return aParams->SetBooleanValue(STATE_ATTRIBUTE, !createPOnReturn);
     378             :   }
     379             : 
     380           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
     381           0 :     if (NS_WARN_IF(!aParams)) {
     382           0 :       return NS_ERROR_NULL_POINTER;
     383             :     }
     384           0 :     nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
     385           0 :     if (NS_WARN_IF(!editor)) {
     386           0 :       return NS_ERROR_INVALID_ARG;
     387             :     }
     388           0 :     auto htmlEditor = static_cast<HTMLEditor*>(editor.get());
     389             : 
     390           0 :     switch (htmlEditor->GetDefaultParagraphSeparator()) {
     391             :       case ParagraphSeparator::div:
     392           0 :         aParams->SetCStringValue(STATE_ATTRIBUTE, "div");
     393           0 :         return NS_OK;
     394             : 
     395             :       case ParagraphSeparator::p:
     396           0 :         aParams->SetCStringValue(STATE_ATTRIBUTE, "p");
     397           0 :         return NS_OK;
     398             : 
     399             :       case ParagraphSeparator::br:
     400           0 :         aParams->SetCStringValue(STATE_ATTRIBUTE, "br");
     401           0 :         return NS_OK;
     402             : 
     403             :       default:
     404           0 :         MOZ_ASSERT_UNREACHABLE("Invalid paragraph separator value");
     405             :         return NS_ERROR_UNEXPECTED;
     406             :     }
     407             :   }
     408             : 
     409           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) {
     410           0 :     NS_ENSURE_ARG_POINTER(aParams);
     411           0 :     nsCOMPtr<nsIHTMLObjectResizer> resizer = do_QueryInterface(refCon);
     412           0 :     NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG);
     413             : 
     414             :     bool enabled;
     415           0 :     resizer->GetObjectResizingEnabled(&enabled);
     416           0 :     return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled);
     417             :   }
     418             : 
     419           0 :   if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
     420           0 :     NS_ENSURE_ARG_POINTER(aParams);
     421           0 :     nsCOMPtr<nsIHTMLInlineTableEditor> editor = do_QueryInterface(refCon);
     422           0 :     NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
     423             : 
     424             :     bool enabled;
     425           0 :     editor->GetInlineTableEditingEnabled(&enabled);
     426           0 :     return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled);
     427             :   }
     428             : 
     429           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     430             : }
     431             : 
     432             : /**
     433             :  * Commands just for state notification
     434             :  *  As of 11/21/02, possible commands are:
     435             :  *    "obs_documentCreated"
     436             :  *    "obs_documentWillBeDestroyed"
     437             :  *    "obs_documentLocationChanged"
     438             :  *  Note that you can use the same command class, nsDocumentStateCommand
     439             :  *    for these or future observer commands.
     440             :  *    We check the input command param for different behavior
     441             :  *
     442             :  *  How to use:
     443             :  *  1. Get the nsICommandManager for the current editor
     444             :  *  2. Implement an nsIObserve object, e.g:
     445             :  *
     446             :  *    void Observe(
     447             :  *        in nsISupports aSubject, // The nsICommandManager calling this Observer
     448             :  *        in string      aTopic,   // command name, e.g.:"obs_documentCreated"
     449             :  *                                 //    or "obs_documentWillBeDestroyed"
     450             :           in wstring     aData );  // ignored (set to "command_status_changed")
     451             :  *
     452             :  *  3. Add the observer by:
     453             :  *       commandManager.addObserver(observeobject, obs_documentCreated);
     454             :  *  4. In the appropriate location in editorSession, editor, or commands code,
     455             :  *     trigger the notification of this observer by something like:
     456             :  *
     457             :  *  nsCOMPtr<nsICommandManager> commandManager = mDocShell->GetCommandManager();
     458             :  *  nsCOMPtr<nsPICommandUpdater> commandUpdater = do_QueryInterface(commandManager);
     459             :  *  NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
     460             :  *    commandUpdater->CommandStatusChanged(obs_documentCreated);
     461             :  *
     462             :  *  5. Use GetCommandStateParams() to obtain state information
     463             :  *     e.g., any creation state codes when creating an editor are
     464             :  *     supplied for "obs_documentCreated" command in the
     465             :  *     "state_data" param's value
     466             :  *
     467             :  */
     468             : 
     469             : NS_IMETHODIMP
     470           0 : nsDocumentStateCommand::IsCommandEnabled(const char* aCommandName,
     471             :                                          nsISupports *refCon,
     472             :                                          bool *outCmdEnabled)
     473             : {
     474           0 :   NS_ENSURE_ARG_POINTER(outCmdEnabled);
     475             :   // Always return false to discourage callers from using DoCommand()
     476           0 :   *outCmdEnabled = false;
     477           0 :   return NS_OK;
     478             : }
     479             : 
     480             : NS_IMETHODIMP
     481           0 : nsDocumentStateCommand::DoCommand(const char *aCommandName,
     482             :                                   nsISupports *refCon)
     483             : {
     484           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     485             : }
     486             : 
     487             : NS_IMETHODIMP
     488           0 : nsDocumentStateCommand::DoCommandParams(const char *aCommandName,
     489             :                                         nsICommandParams *aParams,
     490             :                                         nsISupports *refCon)
     491             : {
     492           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     493             : }
     494             : 
     495             : NS_IMETHODIMP
     496           0 : nsDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
     497             :                                               nsICommandParams *aParams,
     498             :                                               nsISupports *refCon)
     499             : {
     500           0 :   NS_ENSURE_ARG_POINTER(aParams);
     501           0 :   NS_ENSURE_ARG_POINTER(aCommandName);
     502             :   nsresult rv;
     503             : 
     504           0 :   if (!nsCRT::strcmp(aCommandName, "obs_documentCreated")) {
     505           0 :     uint32_t editorStatus = nsIEditingSession::eEditorErrorUnknown;
     506             : 
     507           0 :     nsCOMPtr<nsIEditingSession> editingSession = do_QueryInterface(refCon);
     508           0 :     if (editingSession) {
     509             :       // refCon is initially set to nsIEditingSession until editor
     510             :       //  is successfully created and source doc is loaded
     511             :       // Embedder gets error status if this fails
     512             :       // If called before startup is finished,
     513             :       //    status = eEditorCreationInProgress
     514           0 :       rv = editingSession->GetEditorStatus(&editorStatus);
     515           0 :       NS_ENSURE_SUCCESS(rv, rv);
     516             :     } else {
     517             :       // If refCon is an editor, then everything started up OK!
     518           0 :       nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     519           0 :       if (editor) {
     520           0 :         editorStatus = nsIEditingSession::eEditorOK;
     521             :       }
     522             :     }
     523             : 
     524             :     // Note that if refCon is not-null, but is neither
     525             :     // an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
     526           0 :     aParams->SetLongValue(STATE_DATA, editorStatus);
     527           0 :     return NS_OK;
     528             :   }
     529             : 
     530           0 :   if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged")) {
     531           0 :     nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     532           0 :     if (!editor) {
     533           0 :       return NS_OK;
     534             :     }
     535             : 
     536           0 :     nsCOMPtr<nsIDOMDocument> domDoc;
     537           0 :     editor->GetDocument(getter_AddRefs(domDoc));
     538           0 :     nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
     539           0 :     NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
     540             : 
     541           0 :     nsIURI *uri = doc->GetDocumentURI();
     542           0 :     NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
     543             : 
     544           0 :     return aParams->SetISupportsValue(STATE_DATA, (nsISupports*)uri);
     545             :   }
     546             : 
     547           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     548             : }

Generated by: LCOV version 1.13