summaryrefslogtreecommitdiffstats
path: root/android_webview/lib/aw_browser_dependency_factory_impl.cc
blob: f31bb64e7971921c0621145bfd3b64042f4b2cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/lib/aw_browser_dependency_factory_impl.h"

// TODO(joth): Componentize or remove chrome/... dependencies.
#include "android_webview/browser/net/aw_network_delegate.h"
#include "android_webview/native/aw_contents_container.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "content/public/browser/web_contents.h"
#include "ipc/ipc_message.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"

using content::BrowserContext;
using content::WebContents;

namespace android_webview {

namespace {

base::LazyInstance<AwBrowserDependencyFactoryImpl>::Leaky g_lazy_instance;

class TabContentsWrapper : public AwContentsContainer {
 public:
  TabContentsWrapper(TabContents* tab_contents) {
    tab_contents_.reset(tab_contents);
  }

  virtual ~TabContentsWrapper() {}

  // AwContentsContainer
  virtual content::WebContents* GetWebContents() OVERRIDE {
    return tab_contents_->web_contents();
  }

 private:
  scoped_ptr<TabContents> tab_contents_;
};

}  // namespace

AwBrowserDependencyFactoryImpl::AwBrowserDependencyFactoryImpl() {}

AwBrowserDependencyFactoryImpl::~AwBrowserDependencyFactoryImpl() {}

// static
void AwBrowserDependencyFactoryImpl::InstallInstance() {
  SetInstance(g_lazy_instance.Pointer());
}

// Initializing the Network Delegate here is only a temporary solution until we
// build an Android WebView specific BrowserContext that can handle building
// this internally.
void AwBrowserDependencyFactoryImpl::InitializeNetworkDelegateOnIOThread(
    net::URLRequestContextGetter* normal_context,
    net::URLRequestContextGetter* incognito_context) {
  network_delegate_.reset(new AwNetworkDelegate());
  normal_context->GetURLRequestContext()->set_network_delegate(
      network_delegate_.get());
  incognito_context->GetURLRequestContext()->set_network_delegate(
      network_delegate_.get());
}

void AwBrowserDependencyFactoryImpl::EnsureNetworkDelegateInitialized() {
  if (initialized_network_delegate_)
    return;
  initialized_network_delegate_ = true;
  Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile();
  profile->GetRequestContext()->GetNetworkTaskRunner()->PostTask(
      FROM_HERE,
      base::Bind(
          &AwBrowserDependencyFactoryImpl::InitializeNetworkDelegateOnIOThread,
          base::Unretained(this),
          make_scoped_refptr(profile->GetRequestContext()),
          make_scoped_refptr(
              profile->GetOffTheRecordProfile()->GetRequestContext())));
}

content::BrowserContext* AwBrowserDependencyFactoryImpl::GetBrowserContext(
    bool incognito) {
  EnsureNetworkDelegateInitialized();
  Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile();
  return incognito ? profile->GetOffTheRecordProfile() : profile;
}

WebContents* AwBrowserDependencyFactoryImpl::CreateWebContents(bool incognito) {
  return content::WebContents::Create(
      GetBrowserContext(incognito), 0, MSG_ROUTING_NONE, 0);
}

AwContentsContainer* AwBrowserDependencyFactoryImpl::CreateContentsContainer(
    content::WebContents* contents) {
  return new TabContentsWrapper(
      TabContents::Factory::CreateTabContents(contents));
}

content::JavaScriptDialogCreator*
    AwBrowserDependencyFactoryImpl::GetJavaScriptDialogCreator() {
  return GetJavaScriptDialogCreatorInstance();
}

}  // namespace android_webview