summaryrefslogtreecommitdiffstats
path: root/ios
diff options
context:
space:
mode:
authordroger <droger@chromium.org>2014-12-19 04:31:38 -0800
committerCommit bot <commit-bot@chromium.org>2014-12-19 12:32:27 +0000
commit33cad731a0aa5bdf8103dd961974f05568135489 (patch)
tree81fc951160820449714b343723177ebb115aa26b /ios
parent8261921db56785b00c0db30e796323cf66fcbcad (diff)
downloadchromium_src-33cad731a0aa5bdf8103dd961974f05568135489.zip
chromium_src-33cad731a0aa5bdf8103dd961974f05568135489.tar.gz
chromium_src-33cad731a0aa5bdf8103dd961974f05568135489.tar.bz2
Upstream ApplicationContext on iOS
Review URL: https://codereview.chromium.org/812833003 Cr-Commit-Position: refs/heads/master@{#309185}
Diffstat (limited to 'ios')
-rw-r--r--ios/chrome/browser/application_context.cc27
-rw-r--r--ios/chrome/browser/application_context.h44
-rw-r--r--ios/chrome/browser/application_context_impl.cc43
-rw-r--r--ios/chrome/browser/application_context_impl.h34
-rw-r--r--ios/chrome/ios_chrome.gyp4
-rw-r--r--ios/chrome/ios_chrome_tests.gyp13
-rw-r--r--ios/chrome/test/testing_application_context.cc46
-rw-r--r--ios/chrome/test/testing_application_context.h37
8 files changed, 248 insertions, 0 deletions
diff --git a/ios/chrome/browser/application_context.cc b/ios/chrome/browser/application_context.cc
new file mode 100644
index 0000000..8cdff1c
--- /dev/null
+++ b/ios/chrome/browser/application_context.cc
@@ -0,0 +1,27 @@
+// 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/chrome/browser/application_context.h"
+
+#include "base/logging.h"
+
+namespace {
+// Global ApplicationContext instance.
+ApplicationContext* g_application_context = nullptr;
+}
+
+ApplicationContext* GetApplicationContext() {
+ return g_application_context;
+}
+
+ApplicationContext::ApplicationContext() {
+}
+
+ApplicationContext::~ApplicationContext() {
+}
+
+// static
+void ApplicationContext::SetApplicationContext(ApplicationContext* context) {
+ g_application_context = context;
+}
diff --git a/ios/chrome/browser/application_context.h b/ios/chrome/browser/application_context.h
new file mode 100644
index 0000000..c10e8f8
--- /dev/null
+++ b/ios/chrome/browser/application_context.h
@@ -0,0 +1,44 @@
+// 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_CHROME_BROWSER_APPLICATION_CONTEXT_H_
+#define IOS_CHROME_BROWSER_APPLICATION_CONTEXT_H_
+
+#include <string>
+
+#include "base/macros.h"
+
+namespace net {
+class URLRequestContextGetter;
+}
+
+class ApplicationContext;
+class PrefService;
+
+// Gets the global application context. Cannot return null.
+ApplicationContext* GetApplicationContext();
+
+class ApplicationContext {
+ public:
+ ApplicationContext();
+ virtual ~ApplicationContext();
+
+ // Gets the local state associated with this application.
+ virtual PrefService* GetLocalState() = 0;
+
+ // Gets the URL request context associated with this application.
+ virtual net::URLRequestContextGetter* GetSystemURLRequestContext() = 0;
+
+ // Gets the locale used by the application.
+ virtual const std::string& GetApplicationLocale() = 0;
+
+ protected:
+ // Sets the global ApplicationContext instance.
+ static void SetApplicationContext(ApplicationContext* context);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ApplicationContext);
+};
+
+#endif // IOS_CHROME_BROWSER_APPLICATION_CONTEXT_H_
diff --git a/ios/chrome/browser/application_context_impl.cc b/ios/chrome/browser/application_context_impl.cc
new file mode 100644
index 0000000..b749899
--- /dev/null
+++ b/ios/chrome/browser/application_context_impl.cc
@@ -0,0 +1,43 @@
+// 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/chrome/browser/application_context_impl.h"
+
+#include "base/logging.h"
+#include "components/translate/core/browser/translate_download_manager.h"
+#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
+
+ApplicationContextImpl::ApplicationContextImpl() {
+ DCHECK(!GetApplicationContext());
+ SetApplicationContext(this);
+}
+
+ApplicationContextImpl::~ApplicationContextImpl() {
+ DCHECK_EQ(this, GetApplicationContext());
+ SetApplicationContext(nullptr);
+}
+
+PrefService* ApplicationContextImpl::GetLocalState() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return ios::GetChromeBrowserProvider()->GetLocalState();
+}
+
+net::URLRequestContextGetter*
+ApplicationContextImpl::GetSystemURLRequestContext() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return ios::GetChromeBrowserProvider()->GetSystemURLRequestContext();
+}
+
+const std::string& ApplicationContextImpl::GetApplicationLocale() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!application_locale_.empty());
+ return application_locale_;
+}
+
+void ApplicationContextImpl::SetApplicationLocale(const std::string& locale) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ application_locale_ = locale;
+ translate::TranslateDownloadManager::GetInstance()->set_application_locale(
+ application_locale_);
+}
diff --git a/ios/chrome/browser/application_context_impl.h b/ios/chrome/browser/application_context_impl.h
new file mode 100644
index 0000000..c44a7ef
--- /dev/null
+++ b/ios/chrome/browser/application_context_impl.h
@@ -0,0 +1,34 @@
+// 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_CHROME_BROWSER_APPLICATION_CONTEXT_IMPL_H_
+#define IOS_CHROME_BROWSER_APPLICATION_CONTEXT_IMPL_H_
+
+#include <string>
+
+#include "base/macros.h"
+#include "base/threading/thread_checker.h"
+#include "ios/chrome/browser/application_context.h"
+
+class ApplicationContextImpl : public ApplicationContext {
+ public:
+ ApplicationContextImpl();
+ ~ApplicationContextImpl() override;
+
+ // Sets the locale used by the application.
+ void SetApplicationLocale(const std::string& locale);
+
+ private:
+ // ApplicationContext implementation.
+ PrefService* GetLocalState() override;
+ net::URLRequestContextGetter* GetSystemURLRequestContext() override;
+ const std::string& GetApplicationLocale() override;
+
+ base::ThreadChecker thread_checker_;
+ std::string application_locale_;
+
+ DISALLOW_COPY_AND_ASSIGN(ApplicationContextImpl);
+};
+
+#endif // IOS_CHROME_BROWSER_APPLICATION_CONTEXT_IMPL_H_
diff --git a/ios/chrome/ios_chrome.gyp b/ios/chrome/ios_chrome.gyp
index fe3e2db..04c3c2a 100644
--- a/ios/chrome/ios_chrome.gyp
+++ b/ios/chrome/ios_chrome.gyp
@@ -29,6 +29,10 @@
'../web/ios_web.gyp:ios_web',
],
'sources': [
+ 'browser/application_context.cc',
+ 'browser/application_context.h',
+ 'browser/application_context_impl.cc',
+ 'browser/application_context_impl.h',
'browser/browser_state/browser_state_otr_helper.cc',
'browser/browser_state/browser_state_otr_helper.h',
'browser/infobars/confirm_infobar_controller.h',
diff --git a/ios/chrome/ios_chrome_tests.gyp b/ios/chrome/ios_chrome_tests.gyp
index 871fc20..064638c 100644
--- a/ios/chrome/ios_chrome_tests.gyp
+++ b/ios/chrome/ios_chrome_tests.gyp
@@ -23,5 +23,18 @@
'browser/net/image_fetcher_unittest.mm',
],
},
+ {
+ 'target_name': 'ios_chrome_test_support',
+ 'type': 'static_library',
+ 'dependencies': [
+ '../../base/base.gyp:base',
+ '../provider/ios_provider_chrome.gyp:ios_provider_chrome_browser',
+ 'ios_chrome.gyp:ios_chrome_browser',
+ ],
+ 'sources': [
+ 'test/testing_application_context.cc',
+ 'test/testing_application_context.h',
+ ],
+ },
],
}
diff --git a/ios/chrome/test/testing_application_context.cc b/ios/chrome/test/testing_application_context.cc
new file mode 100644
index 0000000..5c0edc7
--- /dev/null
+++ b/ios/chrome/test/testing_application_context.cc
@@ -0,0 +1,46 @@
+// 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/chrome/test/testing_application_context.h"
+
+#include "base/logging.h"
+#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
+
+TestingApplicationContext::TestingApplicationContext()
+ : application_locale_("en"), local_state_(nullptr) {
+ DCHECK(!GetApplicationContext());
+ SetApplicationContext(this);
+}
+
+TestingApplicationContext::~TestingApplicationContext() {
+ DCHECK_EQ(this, GetApplicationContext());
+ SetApplicationContext(nullptr);
+}
+
+void TestingApplicationContext::SetLocalState(PrefService* local_state) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ local_state_ = local_state;
+}
+
+// static
+TestingApplicationContext* TestingApplicationContext::GetGlobal() {
+ return static_cast<TestingApplicationContext*>(GetApplicationContext());
+}
+
+PrefService* TestingApplicationContext::GetLocalState() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return local_state_;
+}
+
+net::URLRequestContextGetter*
+TestingApplicationContext::GetSystemURLRequestContext() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return ios::GetChromeBrowserProvider()->GetSystemURLRequestContext();
+}
+
+const std::string& TestingApplicationContext::GetApplicationLocale() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!application_locale_.empty());
+ return application_locale_;
+}
diff --git a/ios/chrome/test/testing_application_context.h b/ios/chrome/test/testing_application_context.h
new file mode 100644
index 0000000..99e4cc9
--- /dev/null
+++ b/ios/chrome/test/testing_application_context.h
@@ -0,0 +1,37 @@
+// 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_CHROME_TEST_TESTING_APPLICATION_CONTEXT_H_
+#define IOS_CHROME_TEST_TESTING_APPLICATION_CONTEXT_H_
+
+#include "base/macros.h"
+#include "base/threading/thread_checker.h"
+#include "ios/chrome/browser/application_context.h"
+
+class TestingApplicationContext : public ApplicationContext {
+ public:
+ TestingApplicationContext();
+ ~TestingApplicationContext() override;
+
+ // Convenience method to get the current application context as a
+ // TestingApplicationContext.
+ static TestingApplicationContext* GetGlobal();
+
+ // Sets the local state.
+ void SetLocalState(PrefService* local_state);
+
+ // ApplicationContext implementation.
+ PrefService* GetLocalState() override;
+ net::URLRequestContextGetter* GetSystemURLRequestContext() override;
+ const std::string& GetApplicationLocale() override;
+
+ private:
+ base::ThreadChecker thread_checker_;
+ std::string application_locale_;
+ PrefService* local_state_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestingApplicationContext);
+};
+
+#endif // IOS_CHROME_TEST_TESTING_APPLICATION_CONTEXT_H_