Skip to content

Commit

Permalink
Upstream ios/web_view source code.
Browse files Browse the repository at this point in the history
ios/web_view will provide a view similar to WKWebView while additionally exposing Chromium features.

BUG= 622967

Review-Url: https://codereview.chromium.org/2643773005
Cr-Commit-Position: refs/heads/master@{#446056}
  • Loading branch information
michaeldo1 authored and Commit bot committed Jan 25, 2017
1 parent cb0c494 commit 05089d2
Show file tree
Hide file tree
Showing 55 changed files with 2,462 additions and 0 deletions.
1 change: 1 addition & 0 deletions ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ group("all") {
"//ios/clean/chrome/app:chrome_clean_skeleton",
"//ios/showcase",
"//ios/web/shell:ios_web_shell",
"//ios/web_view/shell:ios_web_view_shell",

# List all the test targets that need to be build on iOS by default.
"//ios/chrome/test:all_tests",
Expand Down
14 changes: 14 additions & 0 deletions ios/web_view/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2016 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.

# Public target that should be used to depend on web_view. Only give access
# to the web_view public headers while still linking with the implementation.
group("web_view") {
public_deps = [
"//ios/web_view/public",
]
deps = [
"//ios/web_view/internal",
]
}
5 changes: 5 additions & 0 deletions ios/web_view/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_rules = [
# The subdirectories in ios/web_view/ will manually allow their own include
# directories in ios/web_view/ so we disallow all of them.
"-ios/web_view",
]
6 changes: 6 additions & 0 deletions ios/web_view/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eugenebut@chromium.org
rohitrao@chromium.org

# These are for the common case of adding or renaming files. If you're doing
# structural changes, please get a review from an OWNER.
per-file BUILD.gn=*
48 changes: 48 additions & 0 deletions ios/web_view/internal/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2016 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.

source_set("internal") {
visibility = [
"//ios/web_view",
"//ios/web_view/internal/*",
]

sources = [
"criwv.mm",
"criwv_browser_state.h",
"criwv_browser_state.mm",
"criwv_network_delegate.cc",
"criwv_network_delegate.h",
"criwv_url_request_context_getter.h",
"criwv_url_request_context_getter.mm",
"criwv_web_client.h",
"criwv_web_client.mm",
"criwv_web_main_delegate.h",
"criwv_web_main_delegate.mm",
"criwv_web_main_parts.h",
"criwv_web_main_parts.mm",
"criwv_web_view_impl.h",
"criwv_web_view_impl.mm",
"pref_names.cc",
"pref_names.h",
]
deps = [
"//base",
"//components/pref_registry",
"//components/prefs",
"//components/translate/core/browser",
"//components/translate/core/common",
"//ios/net",
"//ios/web",
"//ios/web:user_agent",
"//ios/web/public/app",
"//ios/web_view/internal/translate",
"//ios/web_view/public",
"//net",
"//net:extras",
"//ui/base",
"//url",
]
allow_circular_includes_from = [ "//ios/web_view/internal/translate" ]
}
20 changes: 20 additions & 0 deletions ios/web_view/internal/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
include_rules = [
"+base",
"+components/infobars/core",
"+components/keyed_service/core",
"+components/keyed_service/ios",
"+components/pref_registry",
"+components/prefs",
"+components/translate/core",
"+components/translate/ios",
"+ios/net",
"+ios/web/public",
"+ios/web_view",
"+net",
"+ui/base",

# TODO(crbug.com/622967): Remove these exceptions.
"+ios/web/navigation/crw_session_controller.h",
"+ios/web/web_state/ui/crw_web_controller.h",
"+ios/web/web_state/web_state_impl.h",
]
73 changes: 73 additions & 0 deletions ios/web_view/internal/criwv.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2014 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.

#import "ios/web_view/public/criwv.h"

#include <memory>

#include "base/location.h"
#import "base/mac/bind_objc_block.h"
#include "base/single_thread_task_runner.h"
#include "ios/web/public/app/web_main.h"
#include "ios/web/public/web_thread.h"
#include "ios/web_view/internal/criwv_browser_state.h"
#import "ios/web_view/internal/criwv_web_client.h"
#import "ios/web_view/internal/criwv_web_main_delegate.h"
#import "ios/web_view/internal/criwv_web_view_impl.h"
#import "ios/web_view/public/criwv_delegate.h"

namespace {
CRIWV* g_criwv = nil;
}

@interface CRIWV () {
id<CRIWVDelegate> _delegate;
std::unique_ptr<ios_web_view::CRIWVWebMainDelegate> _webMainDelegate;
std::unique_ptr<web::WebMain> _webMain;
}

- (instancetype)initWithDelegate:(id<CRIWVDelegate>)delegate;
- (ios_web_view::CRIWVBrowserState*)browserState;
@end

@implementation CRIWV

+ (void)configureWithDelegate:(id<CRIWVDelegate>)delegate {
g_criwv = [[CRIWV alloc] initWithDelegate:delegate];
}

+ (void)shutDown {
[g_criwv release];
g_criwv = nil;
}

+ (id<CRIWVWebView>)webView {
return [[[CRIWVWebViewImpl alloc] initWithBrowserState:[g_criwv browserState]]
autorelease];
}

- (instancetype)initWithDelegate:(id<CRIWVDelegate>)delegate {
self = [super init];
if (self) {
_delegate = delegate;
_webMainDelegate.reset(new ios_web_view::CRIWVWebMainDelegate(_delegate));
web::WebMainParams params(_webMainDelegate.get());
_webMain.reset(new web::WebMain(params));
}
return self;
}

- (void)dealloc {
_webMain.reset();
_webMainDelegate.reset();
[super dealloc];
}

- (ios_web_view::CRIWVBrowserState*)browserState {
ios_web_view::CRIWVWebClient* client =
static_cast<ios_web_view::CRIWVWebClient*>(web::GetWebClient());
return client->browser_state();
}

@end
59 changes: 59 additions & 0 deletions ios/web_view/internal/criwv_browser_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2014 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.

#ifndef IOS_WEB_VIEW_INTERNAL_CRIWV_BROWSER_STATE_H_
#define IOS_WEB_VIEW_INTERNAL_CRIWV_BROWSER_STATE_H_

#include <memory>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "components/prefs/pref_service.h"
#include "ios/web/public/browser_state.h"

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace ios_web_view {

class CRIWVURLRequestContextGetter;

// CRIWV implementation of BrowserState. Can only be called from the UI thread.
class CRIWVBrowserState : public web::BrowserState {
public:
CRIWVBrowserState();
~CRIWVBrowserState() override;

// web::BrowserState implementation.
bool IsOffTheRecord() const override;
base::FilePath GetStatePath() const override;
net::URLRequestContextGetter* GetRequestContext() override;

// Returns the associated PrefService.
PrefService* GetPrefs();

// Converts from web::BrowserState to CRIWVBrowserState.
static CRIWVBrowserState* FromBrowserState(web::BrowserState* browser_state);

private:
// Registers the preferences for this BrowserState.
void RegisterPrefs(user_prefs::PrefRegistrySyncable* pref_registry);

// The path associated with this BrowserState object.
base::FilePath path_;

// The request context getter for this BrowserState object.
scoped_refptr<CRIWVURLRequestContextGetter> request_context_getter_;

// The PrefService associated with this BrowserState.
std::unique_ptr<PrefService> prefs_;

DISALLOW_COPY_AND_ASSIGN(CRIWVBrowserState);
};

} // namespace ios_web_view

#endif // IOS_WEB_VIEW_INTERNAL_CRIWV_BROWSER_STATE_H_
92 changes: 92 additions & 0 deletions ios/web_view/internal/criwv_browser_state.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2014 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 "ios/web_view/internal/criwv_browser_state.h"

#include <memory>

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/threading/thread_restrictions.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_filter.h"
#include "components/prefs/pref_service_factory.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/translate/core/common/translate_pref_names.h"
#include "ios/web/public/web_thread.h"
#include "ios/web_view/internal/criwv_url_request_context_getter.h"
#include "ios/web_view/internal/pref_names.h"
#include "ui/base/l10n/l10n_util_mac.h"

namespace {
const char kPreferencesFilename[] = "Preferences";
}

namespace ios_web_view {

CRIWVBrowserState::CRIWVBrowserState() : web::BrowserState() {
CHECK(PathService::Get(base::DIR_APP_DATA, &path_));

request_context_getter_ = new CRIWVURLRequestContextGetter(
GetStatePath(),
web::WebThread::GetTaskRunnerForThread(web::WebThread::IO),
web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE),
web::WebThread::GetTaskRunnerForThread(web::WebThread::CACHE));

// Initialize prefs.
scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry =
new user_prefs::PrefRegistrySyncable;
RegisterPrefs(pref_registry.get());
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner =
JsonPrefStore::GetTaskRunnerForFile(path_,
web::WebThread::GetBlockingPool());

scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore(
path_.Append(kPreferencesFilename), sequenced_task_runner, nullptr);

PrefServiceFactory factory;
factory.set_user_prefs(user_pref_store);
prefs_ = factory.Create(pref_registry.get());
}

CRIWVBrowserState::~CRIWVBrowserState() {}

PrefService* CRIWVBrowserState::GetPrefs() {
DCHECK(prefs_);
return prefs_.get();
}

// static
CRIWVBrowserState* CRIWVBrowserState::FromBrowserState(
web::BrowserState* browser_state) {
return static_cast<CRIWVBrowserState*>(browser_state);
}

bool CRIWVBrowserState::IsOffTheRecord() const {
return false;
}

base::FilePath CRIWVBrowserState::GetStatePath() const {
return path_;
}

net::URLRequestContextGetter* CRIWVBrowserState::GetRequestContext() {
return request_context_getter_.get();
}

void CRIWVBrowserState::RegisterPrefs(
user_prefs::PrefRegistrySyncable* pref_registry) {
// TODO(crbug.com/679895): Find a good value for the kAcceptLanguages pref.
// TODO(crbug.com/679895): Pass this value to the network stack somehow, for
// the HTTP header.
pref_registry->RegisterStringPref(prefs::kAcceptLanguages,
l10n_util::GetLocaleOverride());
pref_registry->RegisterBooleanPref(prefs::kEnableTranslate, true);
translate::TranslatePrefs::RegisterProfilePrefs(pref_registry);
}

} // namespace ios_web_view
Loading

0 comments on commit 05089d2

Please sign in to comment.