webview创建过程分析(四)

在前面webview创建过程分析(三)中我们了解了AwContents的初始化过程,其中还有一个点也就是native的初始化部分没有将,今天主要来看下AwContents的native是如何实现的。

private static native long nativeInit(AwBrowserContext browserContext);

AwContents会调用nativeInit方法来进行native层的初始化工作,而AwContents对应的jni层代码在AwContents_jni.h文件中。natveInit对应的jni代码为

JNI_GENERATOR_EXPORT jlong Java_org_chromium_android_1webview_AwContents_nativeInit(
    JNIEnv* env,
    jclass jcaller,
    jobject browserContext) {
  return JNI_AwContents_Init(env, base::android::JavaParamRef<jobject>(env, browserContext));
}

我们来看下JNI_AwContents_Init的实现,实现代码在aw_contents.cc

static jlong JNI_AwContents_Init(JNIEnv* env,
                                 const JavaParamRef<jobject>& browser_context) {
  // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
  // than hard-code the default instance lookup here.
  std::unique_ptr<WebContents> web_contents(content::WebContents::Create(
      content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
  // Return an 'uninitialized' instance; most work is deferred until the
  // subsequent SetJavaPeers() call.
  return reinterpret_cast<intptr_t>(new AwContents(std::move(web_contents)));
}

这里最主要的逻辑就是创建了WebContents对象和AwContents对象,接下来我们看下这两个对象创建过程中都涉及到哪些逻辑

WebContents创建过程

1.构建WebContents::CreateParams参数

WebContents::CreateParams::CreateParams(BrowserContext* context)
    : CreateParams(context, nullptr) {}

WebContents::CreateParams::CreateParams(BrowserContext* context,
                                        scoped_refptr<SiteInstance> site)
    : browser_context(context),
      site_instance(std::move(site)),
      opener_render_process_id(content::ChildProcessHost::kInvalidUniqueID),
      opener_render_frame_id(MSG_ROUTING_NONE),
      opener_suppressed(false),
      created_with_opener(false),
      routing_id(MSG_ROUTING_NONE),
      main_frame_routing_id(MSG_ROUTING_NONE),
      main_frame_widget_routing_id(MSG_ROUTING_NONE),
      initially_hidden(false),
      guest_delegate(nullptr),
      context(nullptr),
      renderer_initiated_creation(false),
      desired_renderer_state(kOkayToHaveRendererProcess),
      starting_sandbox_flags(blink::WebSandboxFlags::kNone) {}

这个参数的创建过程主要是进行一些变量的初始化。

2.WebContents::Create方法分析

std::unique_ptr<WebContents> WebContents::Create(
    const WebContents::CreateParams& params) {
  return WebContentsImpl::CreateWithOpener(params, FindOpenerRFH(params));
}

这里主要是调用WebContentsImpl的CreateWithOpener方法

std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
    const WebContents::CreateParams& params,
    RenderFrameHostImpl* opener_rfh) {
  TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
  FrameTreeNode* opener = nullptr;
  if (opener_rfh)
    opener = opener_rfh->frame_tree_node();
  std::unique_ptr<WebContentsImpl> new_contents(
      new WebContentsImpl(params.browser_context));

  ........
  new_contents->Init(params);
  return new_contents;

从这里可以知道前面需要的WebContents对象实际上是WebContentsImpl

这里主要分为两步:

1.创建WebContentsImpl

2.调用WebContentsImpl的Init方法

创建WebContentsImpl

WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
    : delegate_(nullptr),
      controller_(this, browser_context),
      render_view_host_delegate_view_(nullptr),
      created_with_opener_(false),
      node_(this),
      frame_tree_(new NavigatorImpl(&controller_, this),
                  this,
                  this,
                  this,
                  this),
                  ......

在WebContentsImpl的创建中主要会创建一个controller_和frame_tree_

cotroller_的创建过程

NavigationControllerImpl::NavigationControllerImpl(
    NavigationControllerDelegate* delegate,
    BrowserContext* browser_context)
    : browser_context_(browser_context),
      pending_entry_(nullptr),
      failed_pending_entry_id_(0),
      last_committed_entry_index_(-1),
      pending_entry_index_(-1),
      transient_entry_index_(-1),
      delegate_(delegate),
      ssl_manager_(this),
      needs_reload_(false),
      is_initial_navigation_(true),
      in_navigate_to_pending_entry_(false),
      pending_reload_(ReloadType::NONE),
      get_timestamp_callback_(base::Bind(&base::Time::Now)),
      last_committed_reload_type_(ReloadType::NONE) {
  DCHECK(browser_context_);
}

这个controller_是NavigationControllerImpl的一个实例,NavigationControllerImpl是继承NavigationController,它包含了LoadUrl,CanGoBack等方法,这个类的作用在我们研究webview加载页面的过程中会又更深入的研究。在这里只是创建了一个NavigationControllerImpl对象,为其中的browser_context_和delegate_进行赋值,这里的dele

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值