Web Sprocket(s) | Lesson 3 - Crafting a window (Part II)

In the previous lesson we have discussed how to create a platform independent Window class and expand it with platform specific (Linux) behaviors. If you missed it, you can read it here.

In this lesson we will connect our window to the application.

First of all we should create our own BrowserMainParts class which will have an important role in the whole browser loop. This will handle all main functionalities for initialization part of the browser. There are several essential methods in this class which have to be implemented. At certain steps - before and after some events - these methods are called from the BrowserMainLoop class. The steps are the followings:

  • EarlyInitialization
    • tasks that should be done as soon as possible after the program startup
    • tasks that should be done before the main message loop is executed
  • MainMessageLoopStart
    • execute those tasks which begin with the start of the main message loop and end with the initialization of the main thread
    • platform-specific things which should be done immediately before the start of the main message loop
  • RunMainMessageLoopParts
    • tasks to be done before and after invoking the main message loop run method

According to these three steps, we will override the following methods in our BrowserMainParts subclass (browser/browser_main_parts.cc).

In PreEarlyInitialization, we initialize the UI input method.

void SprocketBrowserMainParts::PreEarlyInitialization() {
  ui::InitializeInputMethodForTesting();
}

In PreMainMessageLoopRun we initialize and create the window. The window needs a running message-loop to work properly and to be able to deal with the interactions.

void SprocketBrowserMainParts::PreMainMessageLoopRun() {
  SprocketWindow::Initialize();
  SprocketWindow::CreateNewWindow(gfx::Size());
}

In PostMainMessageLoopRun we deinitialize and destroy the window.

void SprocketBrowserMainParts::PostMainMessageLoopRun() {
  SprocketWindow::Deinitialize();
}

At this point we haven’t finished yet. The BrowserMainParts should be connected to the rest of our application.

The next thing what we have to do is to add our own ContentBrowserClient class. The purpose of this class is to provide an API to the embedder in order to participate in the browser logic.

For now we can have only one method which instantiates the browser main parts with specific parameters.

content::BrowserMainParts* 
SprocketContentBrowserClient::CreateBrowserMainParts(
    const content::MainFunctionParams& parameters) {
  browser_main_parts_ = new SprocketBrowserMainParts(parameters);
  return browser_main_parts_;
}

Next, we will add our MainDelegate implementation. You should know that the ContentMain needs a MainDelegate instance, which is passed through its constructor. In the previous tutorial this parameter was set to NULL. Now after implementing MainDelegate we will be able to use an instance of our class instead.

SprocketMainDelegate delegate;
content::ContentMainParams params(&delegate);

With this code you make MainDelegate responsible for creating the ContentBrowserClient.

content::ContentBrowserClient*
SprocketMainDelegate::CreateContentBrowserClient() {
  browser_client_.reset(new SprocketContentBrowserClient);
  return browser_client_.get();
}

Only one thing is remaining. Let’s implement the BasicStartupComplete which is called after the basic startup actions have been completed.

bool SprocketMainDelegate::BasicStartupComplete(int* exit_code) {
  base::FilePath pak_file;
  ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);

  return false;
}

In this method we should initialize the resource bundle. For now a dummy path is used here, because we don’t have any resource files yet. We will replace this in a later lesson.

That’s it! Now, you should be able to see a blank window.

Next time, we will look at some of the Content API’s Contexts.

Stay tuned for the upcoming lesson!


The lesson 3 repository can be found here.

Attachments: BrowserMainParts, ContentBrowserClient, MainDelegate.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Fill in the blank