1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/lib/aw_browser_dependency_factory_impl.h"
7 // TODO(joth): Componentize or remove chrome/... dependencies.
8 #include "android_webview/browser/net/aw_network_delegate.h"
9 #include "android_webview/native/aw_contents_container.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h"
18 #include "chrome/browser/ui/tab_contents/tab_contents.h"
19 #include "content/public/browser/web_contents.h"
20 #include "ipc/ipc_message.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
24 using content::BrowserContext
;
25 using content::WebContents
;
27 namespace android_webview
{
31 base::LazyInstance
<AwBrowserDependencyFactoryImpl
>::Leaky g_lazy_instance
;
33 class TabContentsWrapper
: public AwContentsContainer
{
35 TabContentsWrapper(TabContents
* tab_contents
) {
36 tab_contents_
.reset(tab_contents
);
39 virtual ~TabContentsWrapper() {}
41 // AwContentsContainer
42 virtual content::WebContents
* GetWebContents() OVERRIDE
{
43 return tab_contents_
->web_contents();
47 scoped_ptr
<TabContents
> tab_contents_
;
52 AwBrowserDependencyFactoryImpl::AwBrowserDependencyFactoryImpl() {}
54 AwBrowserDependencyFactoryImpl::~AwBrowserDependencyFactoryImpl() {}
57 void AwBrowserDependencyFactoryImpl::InstallInstance() {
58 SetInstance(g_lazy_instance
.Pointer());
61 // Initializing the Network Delegate here is only a temporary solution until we
62 // build an Android WebView specific BrowserContext that can handle building
64 void AwBrowserDependencyFactoryImpl::InitializeNetworkDelegateOnIOThread(
65 net::URLRequestContextGetter
* normal_context
,
66 net::URLRequestContextGetter
* incognito_context
) {
67 network_delegate_
.reset(new AwNetworkDelegate());
68 normal_context
->GetURLRequestContext()->set_network_delegate(
69 network_delegate_
.get());
70 incognito_context
->GetURLRequestContext()->set_network_delegate(
71 network_delegate_
.get());
74 void AwBrowserDependencyFactoryImpl::EnsureNetworkDelegateInitialized() {
75 if (initialized_network_delegate_
)
77 initialized_network_delegate_
= true;
78 Profile
* profile
= g_browser_process
->profile_manager()->GetDefaultProfile();
79 profile
->GetRequestContext()->GetNetworkTaskRunner()->PostTask(
82 &AwBrowserDependencyFactoryImpl::InitializeNetworkDelegateOnIOThread
,
83 base::Unretained(this),
84 make_scoped_refptr(profile
->GetRequestContext()),
86 profile
->GetOffTheRecordProfile()->GetRequestContext())));
89 content::BrowserContext
* AwBrowserDependencyFactoryImpl::GetBrowserContext(
91 EnsureNetworkDelegateInitialized();
92 Profile
* profile
= g_browser_process
->profile_manager()->GetDefaultProfile();
93 return incognito
? profile
->GetOffTheRecordProfile() : profile
;
96 WebContents
* AwBrowserDependencyFactoryImpl::CreateWebContents(bool incognito
) {
97 return content::WebContents::Create(
98 GetBrowserContext(incognito
), 0, MSG_ROUTING_NONE
, 0);
101 AwContentsContainer
* AwBrowserDependencyFactoryImpl::CreateContentsContainer(
102 content::WebContents
* contents
) {
103 return new TabContentsWrapper(
104 TabContents::Factory::CreateTabContents(contents
));
107 content::JavaScriptDialogCreator
*
108 AwBrowserDependencyFactoryImpl::GetJavaScriptDialogCreator() {
109 return GetJavaScriptDialogCreatorInstance();
112 } // namespace android_webview