コード例 #1
0
ファイル: cefclient.cpp プロジェクト: amate/unChromium
void RunDOMAccessTest(CefRefPtr<CefBrowser> browser)
{
  class Listener : public CefDOMEventListener
  {
  public:
    Listener() {}
    virtual void HandleEvent(CefRefPtr<CefDOMEvent> event)
    {
      CefRefPtr<CefDOMDocument> document = event->GetDocument();
      ASSERT(document.get());
      
      std::stringstream ss;

      CefRefPtr<CefDOMNode> button = event->GetTarget();
      ASSERT(button.get());
      std::string buttonValue = button->GetElementAttribute("value");
      ss << "You clicked the " << buttonValue.c_str() << " button. ";
      
      if (document->HasSelection()) {
        std::string startName, endName;
      
        // Determine the start name by first trying to locate the "id" attribute
        // and then defaulting to the tag name.
        {
          CefRefPtr<CefDOMNode> node = document->GetSelectionStartNode();
          if (!node->IsElement())
            node = node->GetParent();
          if (node->IsElement() && node->HasElementAttribute("id"))
            startName = node->GetElementAttribute("id");
          else
            startName = node->GetName();
        }

        // Determine the end name by first trying to locate the "id" attribute
        // and then defaulting to the tag name.
        {
          CefRefPtr<CefDOMNode> node = document->GetSelectionEndNode();
          if (!node->IsElement())
            node = node->GetParent();
          if (node->IsElement() && node->HasElementAttribute("id"))
            endName = node->GetElementAttribute("id");
          else
            endName = node->GetName();
        }

        ss << "The selection is from " <<
            startName.c_str() << ":" << document->GetSelectionStartOffset() <<
            " to " <<
            endName.c_str() << ":" << document->GetSelectionEndOffset();
      } else {
        ss << "Nothing is selected.";
      }
      
      // Update the description.
      CefRefPtr<CefDOMNode> desc = document->GetElementById("description");
      ASSERT(desc.get());
      CefRefPtr<CefDOMNode> text = desc->GetFirstChild();
      ASSERT(text.get());
      ASSERT(text->IsText());
      text->SetValue(ss.str());
    }

    IMPLEMENT_REFCOUNTING(Listener);
  };

  class Visitor : public CefDOMVisitor
  {
  public:
    Visitor() {}
    virtual void Visit(CefRefPtr<CefDOMDocument> document)
    {
      // Register an click listener for the button.
      CefRefPtr<CefDOMNode> button = document->GetElementById("button");
      ASSERT(button.get());
      button->AddEventListener("click", new Listener(), false);
    }

    IMPLEMENT_REFCOUNTING(Visitor);
  };

  // The DOM visitor will be called after the path is loaded.
  CefRefPtr<CefClient> client = browser->GetClient();
  static_cast<ClientHandler*>(client.get())->AddDOMVisitor(
      "http://tests/domaccess", new Visitor());

  browser->GetMainFrame()->LoadURL("http://tests/domaccess");
}
コード例 #2
0
void RunOSRPluginTest(CefRefPtr<CefBrowser> browser, bool transparent)
{
  class Listener : public CefDOMEventListener
  {
  public:
    Listener() {}
    virtual void HandleEvent(CefRefPtr<CefDOMEvent> event)
    {
      CefRefPtr<CefBrowser> browser = GetOffScreenBrowser();
      
      CefRefPtr<CefDOMNode> element = event->GetTarget();
      ASSERT(element.get());
      std::string elementId = element->GetElementAttribute("id");

      if (elementId == "back") {
        browser->GoBack();
      } else if(elementId == "forward") {
        browser->GoForward();
      } else if(elementId == "stop") {
        browser->Reload();
      } else if(elementId == "reload") {
        browser->StopLoad();
      } else if (elementId == "go") {
        // Retrieve the value of the "url" field and load it in the off-screen
        // browser window.
        CefRefPtr<CefDOMDocument> document = event->GetDocument();
        ASSERT(document.get());
        CefRefPtr<CefDOMNode> url = document->GetElementById("url");
        ASSERT(url.get());
        CefString value = url->GetValue();
        if (!value.empty())
          browser->GetMainFrame()->LoadURL(value);
      } else if(elementId == "testTransparency") {
        // Transparency test.
        browser->GetMainFrame()->LoadURL(
            "http://tests/transparency");
      } else if(elementId == "testWindowlessPlugin") {
        // Load flash, which is a windowless plugin.
        browser->GetMainFrame()->LoadURL(
            "http://www.adobe.com/software/flash/about/");
      } else if(elementId == "viewSource") {
        // View the page source for the host browser window.
        AppGetBrowser()->GetMainFrame()->ViewSource();
      } else {
        // Not reached.
        ASSERT(false);
      }
    }

    IMPLEMENT_REFCOUNTING(Listener);
  };

  class Visitor : public CefDOMVisitor
  {
  public:
    Visitor() {}

    void RegisterClickListener(CefRefPtr<CefDOMDocument> document,
                               CefRefPtr<CefDOMEventListener> listener,
                               const std::string& elementId)
    {
      CefRefPtr<CefDOMNode> element = document->GetElementById(elementId);
      ASSERT(element.get());
      element->AddEventListener("click", listener, false);
    }

    virtual void Visit(CefRefPtr<CefDOMDocument> document)
    {
      CefRefPtr<CefDOMEventListener> listener(new Listener());
      
      // Register click listeners for the various HTML elements.
      RegisterClickListener(document, listener, "back");
      RegisterClickListener(document, listener, "forward");
      RegisterClickListener(document, listener, "stop");
      RegisterClickListener(document, listener, "reload");
      RegisterClickListener(document, listener, "go");
      RegisterClickListener(document, listener, "testTransparency");
      RegisterClickListener(document, listener, "testWindowlessPlugin");
      RegisterClickListener(document, listener, "viewSource");
    }

    IMPLEMENT_REFCOUNTING(Visitor);
  };

  // Center the window on the screen.
  int screenX = GetSystemMetrics(SM_CXFULLSCREEN);
  int screenY = GetSystemMetrics(SM_CYFULLSCREEN);
  int width = 1000, height = 760;
  int x = (screenX - width) / 2;
  int y = (screenY - height) / 2;

  SetWindowPos(AppGetMainHwnd(), NULL, x, y, width, height,
      SWP_NOZORDER | SWP_SHOWWINDOW);

  // The DOM visitor will be called after the path is loaded.
  CefRefPtr<CefClient> client = browser->GetClient();
  static_cast<ClientHandler*>(client.get())->AddDOMVisitor(
      "http://tests/osrapp", new Visitor());

  SetOffScreenTransparent(transparent);
  browser->GetMainFrame()->LoadURL("http://tests/osrapp");
}