summaryrefslogtreecommitdiffstats
path: root/components/translate
diff options
context:
space:
mode:
authorandrewhayden <andrewhayden@chromium.org>2014-11-11 09:47:16 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-11 17:47:36 +0000
commit14d91355848a733b18396d74c97d6b945260b899 (patch)
tree28bdfe9f0561e72b95a08552fc69e5a51af8224c /components/translate
parent7742fb4516ba94d8c0b6cdb8ddbb25839065642f (diff)
downloadchromium_src-14d91355848a733b18396d74c97d6b945260b899.zip
chromium_src-14d91355848a733b18396d74c97d6b945260b899.tar.gz
chromium_src-14d91355848a733b18396d74c97d6b945260b899.tar.bz2
Refactor language detection logic to allow non-static CLD data sources.
This change further refines the approach taken in setting the implementation used for accessing compact language detector at runtime. It is based upon the pattern used in content_client.h, though adapted for this specific use case. Fundamentally, there is a switch from having exactly one linked-in class define a global method to instead having the high-level targets (like the content shell, chrome shell, and so on) be responsible for setting the implementation that is desired. This eliminates a lot of the ugly GYP/GN logic, but requires work for each high-level target. BUG=367239 Review URL: https://codereview.chromium.org/461633002 Cr-Commit-Position: refs/heads/master@{#303669}
Diffstat (limited to 'components/translate')
-rw-r--r--components/translate/content/browser/BUILD.gn18
-rw-r--r--components/translate/content/browser/browser_cld_data_provider.cc13
-rw-r--r--components/translate/content/browser/browser_cld_data_provider.h52
-rw-r--r--components/translate/content/browser/browser_cld_data_provider_factory.cc54
-rw-r--r--components/translate/content/browser/browser_cld_data_provider_factory.h68
-rw-r--r--components/translate/content/browser/browser_cld_utils.cc39
-rw-r--r--components/translate/content/browser/browser_cld_utils.h38
-rw-r--r--components/translate/content/browser/data_file_browser_cld_data_provider.cc26
-rw-r--r--components/translate/content/browser/data_file_browser_cld_data_provider.h13
-rw-r--r--components/translate/content/browser/static_browser_cld_data_provider.cc56
-rw-r--r--components/translate/content/browser/static_browser_cld_data_provider.h28
-rw-r--r--components/translate/content/common/BUILD.gn20
-rw-r--r--components/translate/content/common/cld_data_source.cc112
-rw-r--r--components/translate/content/common/cld_data_source.h123
-rw-r--r--components/translate/content/common/component_cld_data_source.cc21
-rw-r--r--components/translate/content/common/standalone_cld_data_source.cc21
-rw-r--r--components/translate/content/common/static_cld_data_source.cc21
-rw-r--r--components/translate/content/renderer/BUILD.gn16
-rw-r--r--components/translate/content/renderer/data_file_renderer_cld_data_provider.cc10
-rw-r--r--components/translate/content/renderer/data_file_renderer_cld_data_provider.h15
-rw-r--r--components/translate/content/renderer/renderer_cld_data_provider.cc59
-rw-r--r--components/translate/content/renderer/renderer_cld_data_provider.h67
-rw-r--r--components/translate/content/renderer/renderer_cld_utils.cc39
-rw-r--r--components/translate/content/renderer/renderer_cld_utils.h41
-rw-r--r--components/translate/content/renderer/static_renderer_cld_data_provider.cc45
-rw-r--r--components/translate/content/renderer/static_renderer_cld_data_provider.h34
-rw-r--r--components/translate/content/renderer/translate_helper.cc7
-rw-r--r--components/translate/content/renderer/translate_helper.h5
28 files changed, 692 insertions, 369 deletions
diff --git a/components/translate/content/browser/BUILD.gn b/components/translate/content/browser/BUILD.gn
index d94c338..1cc1923 100644
--- a/components/translate/content/browser/BUILD.gn
+++ b/components/translate/content/browser/BUILD.gn
@@ -6,9 +6,16 @@ import("//build/config/features.gni")
static_library("browser") {
sources = [
+ "browser_cld_data_provider_factory.cc",
+ "browser_cld_data_provider_factory.h",
+ "browser_cld_data_provider.cc",
"browser_cld_data_provider.h",
+ "browser_cld_utils.cc",
+ "browser_cld_utils.h",
"content_translate_driver.cc",
"content_translate_driver.h",
+ "data_file_browser_cld_data_provider.cc",
+ "data_file_browser_cld_data_provider.h",
]
deps = [
@@ -17,15 +24,4 @@ static_library("browser") {
"//content/public/browser",
]
- if (cld2_data_source == "standalone" || cld2_data_source == "component") {
- sources += [
- "data_file_browser_cld_data_provider.cc",
- "data_file_browser_cld_data_provider.h",
- ]
- } else if (cld2_data_source == "static") {
- sources += [
- "static_browser_cld_data_provider.cc",
- "static_browser_cld_data_provider.h",
- ]
- }
}
diff --git a/components/translate/content/browser/browser_cld_data_provider.cc b/components/translate/content/browser/browser_cld_data_provider.cc
new file mode 100644
index 0000000..a73e494
--- /dev/null
+++ b/components/translate/content/browser/browser_cld_data_provider.cc
@@ -0,0 +1,13 @@
+// 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 "components/translate/content/browser/browser_cld_data_provider.h"
+
+namespace translate {
+
+bool BrowserCldDataProvider::OnMessageReceived(const IPC::Message& message) {
+ return false; // Message not handled
+}
+
+} // namespace translate
diff --git a/components/translate/content/browser/browser_cld_data_provider.h b/components/translate/content/browser/browser_cld_data_provider.h
index da99c06..918912a 100644
--- a/components/translate/content/browser/browser_cld_data_provider.h
+++ b/components/translate/content/browser/browser_cld_data_provider.h
@@ -7,30 +7,31 @@
#include <string>
-#include "base/files/file_path.h"
+#include "base/compiler_specific.h"
+#include "base/macros.h"
#include "ipc/ipc_listener.h"
namespace IPC {
class Message;
}
-namespace content {
-class WebContents;
-}
-
namespace translate {
// Browser-side interface responsible for providing CLD data.
// The implementation must be paired with a renderer-side implementation of
// the RendererCldDataProvider class:
+// ../renderer/renderer_cld_data_provider.h
//
-// components/translate/content/renderer/renderer_cld_data_provider.h
+// The glue between them is typically a pair of request/response IPC messages
+// using the "CldDataProviderMsgStart" IPCMessageStart enumerated constant from
+// ipc_message_start.h
//
-// ... and the glue between them is typically a pair of request/response IPC
-// messages using the CldDataProviderMsgStart IPCMessageStart enumerated
-// constant from ipc_message_start.h
+// In general, instances of this class should be obtained by using the
+// BrowserCldDataProviderFactory::CreateBrowserCldProvider(...). For more
+// information, see browser_cld_data_provider_factory.h.
class BrowserCldDataProvider : public IPC::Listener {
public:
+ BrowserCldDataProvider() {}
~BrowserCldDataProvider() override {}
// IPC::Listener implementation:
@@ -38,44 +39,29 @@ class BrowserCldDataProvider : public IPC::Listener {
// OnCldDataRequest() and returns true. In all other cases, this method does
// nothing. This method is defined as virtual in order to force the
// implementation to define the specific IPC message(s) that it handles.
- virtual bool OnMessageReceived(const IPC::Message&) override = 0;
+ // The default implementation does nothing and returns false.
+ bool OnMessageReceived(const IPC::Message&) override;
// Called when the browser process receives an appropriate message in
// OnMessageReceived, above. The implementation should attempt to locate
// the CLD data, cache any metadata required for accessing that data, and
// ultimately trigger a response by invoking SendCldDataResponse.
- //
// The renderer process may poll for data, in which case this method may be
// repeatedly invoked. The implementation must be safe to call any number
// of times.
- virtual void OnCldDataRequest() = 0;
+ // The default implementation does nothing.
+ virtual void OnCldDataRequest() {}
// Invoked when OnCldDataRequest, above, results in a successful lookup or
// the data is already cached and ready to respond to. The implementation
// should take whatever action is appropriate for responding to the paired
// RendererCldDataProvider, typically by sending an IPC response.
- virtual void SendCldDataResponse() = 0;
-};
-
-// Static factory function defined by the implementation that produces a new
-// provider for the specified WebContents.
-BrowserCldDataProvider* CreateBrowserCldDataProviderFor(
- content::WebContents*);
+ // The default implementation does nothing.
+ virtual void SendCldDataResponse() {}
-// For data sources that support a separate CLD data file, configures the path
-// of that data file.
-//
-// The 'component' and 'standalone' data sources need this method to be called
-// in order to locate the CLD data on disk.
-// If the data source doesn't need or doesn't support such configuration, this
-// function should do nothing. This is the case for, e.g., the static data
-// source.
-void SetCldDataFilePath(const base::FilePath& path);
-
-// Returns the path most recently set by SetCldDataFilePath. The initial value
-// prior to any such call is the empty path. If the data source doesn't support
-// a data file, returns the empty path.
-base::FilePath GetCldDataFilePath();
+ private:
+ DISALLOW_COPY_AND_ASSIGN(BrowserCldDataProvider);
+};
} // namespace translate
diff --git a/components/translate/content/browser/browser_cld_data_provider_factory.cc b/components/translate/content/browser/browser_cld_data_provider_factory.cc
new file mode 100644
index 0000000..db72ac1
--- /dev/null
+++ b/components/translate/content/browser/browser_cld_data_provider_factory.cc
@@ -0,0 +1,54 @@
+// 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 "components/translate/content/browser/browser_cld_data_provider_factory.h"
+
+#include "base/lazy_instance.h"
+#include "components/translate/content/browser/browser_cld_data_provider.h"
+
+namespace {
+
+// The global instance, alive for the entire lifetime of the process.
+translate::BrowserCldDataProviderFactory* g_instance = NULL;
+
+// The default factory, which produces no-op instances of BrowserCldDataProvider
+// suitable for use when CLD data is statically-linked.
+base::LazyInstance<translate::BrowserCldDataProviderFactory>::Leaky
+ g_wrapped_default = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+
+namespace translate {
+
+scoped_ptr<BrowserCldDataProvider>
+BrowserCldDataProviderFactory::CreateBrowserCldDataProvider(
+ content::WebContents* web_contents) {
+ return scoped_ptr<BrowserCldDataProvider>(new BrowserCldDataProvider());
+}
+
+// static
+bool BrowserCldDataProviderFactory::IsInitialized() {
+ return g_instance != NULL;
+}
+
+// static
+void BrowserCldDataProviderFactory::SetDefault(
+ BrowserCldDataProviderFactory* instance) {
+ if (!IsInitialized()) Set(instance);
+}
+
+// static
+void BrowserCldDataProviderFactory::Set(
+ BrowserCldDataProviderFactory* instance) {
+ g_instance = instance;
+}
+
+// static
+BrowserCldDataProviderFactory* BrowserCldDataProviderFactory::Get() {
+ if (IsInitialized()) return g_instance;
+ return &g_wrapped_default.Get();
+}
+
+} // namespace translate
diff --git a/components/translate/content/browser/browser_cld_data_provider_factory.h b/components/translate/content/browser/browser_cld_data_provider_factory.h
new file mode 100644
index 0000000..bae2250
--- /dev/null
+++ b/components/translate/content/browser/browser_cld_data_provider_factory.h
@@ -0,0 +1,68 @@
+// 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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_DATA_PROVIDER_FACTORY_H_
+#define COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_DATA_PROVIDER_FACTORY_H_
+
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "components/translate/content/browser/browser_cld_data_provider.h"
+
+namespace content {
+class WebContents;
+}
+
+namespace translate {
+
+// A factory for the Browser side of CLD Data Providers. The embedder should
+// set an instance as soon as feasible during startup. The browser process will
+// use this factory to create the one BrowserCldDataProvider for each render
+// view host in order to communicate with a RendererCldDataProvider in the
+// renderer process. For more information on the RendererCldDataProvider, see:
+// ../renderer/renderer_cld_data_provider.h
+class BrowserCldDataProviderFactory {
+ public:
+ BrowserCldDataProviderFactory() {}
+ virtual ~BrowserCldDataProviderFactory() {}
+
+ // Create and return a new instance of a BrowserCldDataProvider. The default
+ // implementation of this method produces a no-op BrowserCldDataProvider that
+ // is suitable only when CLD data has been statically linked.
+ // Every invocation creates a new provider; the caller is responsible for
+ // deleting the object when it is no longer needed.
+ virtual scoped_ptr<BrowserCldDataProvider> CreateBrowserCldDataProvider(
+ content::WebContents* web_contents);
+
+ // Returns true if and only if the current instance for this process is not
+ // NULL.
+ static bool IsInitialized();
+
+ // Sets the default factory for this process, i.e. the factory to be used
+ // unless the embedder calls Set(BrowserCldDataProviderFactory*). This is the
+ // method that normal (i.e., non-test) Chromium code should use; embedders can
+ // and should use the unconditional Set(BrowserCldDataProviderFactory*) method
+ // instead. If a default factory has already been set, this method does
+ // nothing.
+ static void SetDefault(BrowserCldDataProviderFactory* instance);
+
+ // Unconditionally sets the factory for this process, overwriting any
+ // previously-configured default. Normal Chromium code should never use this
+ // method; it is provided for embedders to inject a factory from outside of
+ // the Chromium code base. Test code can also use this method to force the
+ // runtime to have a desired behavior.
+ static void Set(BrowserCldDataProviderFactory* instance);
+
+ // Returns the instance of the factory previously set by Set()/SetDefault().
+ // If no instance has been set, a default factory will be returned that
+ // produces no-op BrowserCldDataProviders as described by NewProvider(...)
+ // above.
+ static BrowserCldDataProviderFactory* Get();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(BrowserCldDataProviderFactory);
+};
+
+} // namespace translate
+
+#endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_DATA_PROVIDER_FACTORY_H_
diff --git a/components/translate/content/browser/browser_cld_utils.cc b/components/translate/content/browser/browser_cld_utils.cc
new file mode 100644
index 0000000..4e65c05
--- /dev/null
+++ b/components/translate/content/browser/browser_cld_utils.cc
@@ -0,0 +1,39 @@
+// 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 "components/translate/content/browser/browser_cld_utils.h"
+
+#include "components/translate/content/browser/browser_cld_data_provider_factory.h"
+#include "components/translate/content/common/cld_data_source.h"
+
+namespace translate {
+
+// static
+void BrowserCldUtils::ConfigureDefaultDataProvider() {
+ if (!BrowserCldDataProviderFactory::IsInitialized()) {
+ BrowserCldDataProviderFactory* factory = NULL;
+ CldDataSource* data_source = NULL;
+
+ // Maintainers: Customize platform defaults here as necessary.
+ // It is appropriate to ifdef this code and customize per-platform.
+ //
+ // Remember to update GYP/GN dependencies of high-level targets as well:
+ // - For the NONE or STATIC data sources, depend upon
+ // third_party/cld_2/cld_2.gyp:cld2_static.
+ // - For the COMPONENT or STANDALONE data sources, depend upon
+ // third_party/cld_2/cld_2.gyp:cld2_dynamic.
+ // - For any other sources, the embedder should already have set a factory
+ // and so this code should never be invoked.
+ // -----------------------------------------------------------------------
+ factory = new BrowserCldDataProviderFactory();
+ data_source = CldDataSource::GetStaticDataSource();
+ // -----------------------------------------------------------------------
+
+ // Apply the values defined above
+ BrowserCldDataProviderFactory::SetDefault(factory);
+ CldDataSource::SetDefault(data_source);
+ }
+}
+
+} // namespace translate
diff --git a/components/translate/content/browser/browser_cld_utils.h b/components/translate/content/browser/browser_cld_utils.h
new file mode 100644
index 0000000..f3fab95
--- /dev/null
+++ b/components/translate/content/browser/browser_cld_utils.h
@@ -0,0 +1,38 @@
+// 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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_UTILS_H_
+#define COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_UTILS_H_
+
+#include "base/macros.h"
+
+namespace translate {
+
+// Browser-side utilities for dealing with CLD data source configuration.
+// This class exists primarily to avoid duplicating code in high-level targets
+// such as the Chrome browser, Chrome shell, and so on.
+class BrowserCldUtils {
+ public:
+ // Perform conditional configuration of the CLD data provider.
+ // If no specific BrowserCldDataProviderFactory has yet been set, an instance
+ // is created and assigned as appropriate for each platform. If a specific
+ // BrowserCldDataProviderFactory *has* been set, this method does nothing
+ // (as a side effect of setting the default factory, subsequent invocations
+ // do nothing - making this method reentrant).
+ //
+ // This method will not overwrite a data provider configured by an embedder.
+ // This method must be invoked prior to registering components with the
+ // component updater if the component updater implementation is to be used;
+ // it is best to ensure that this method is called as early as possible.
+ static void ConfigureDefaultDataProvider();
+
+ private:
+ BrowserCldUtils() {}
+ ~BrowserCldUtils() {}
+ DISALLOW_COPY_AND_ASSIGN(BrowserCldUtils);
+};
+
+} // namespace translate
+
+#endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_BROWSER_CLD_UTILS_H_
diff --git a/components/translate/content/browser/data_file_browser_cld_data_provider.cc b/components/translate/content/browser/data_file_browser_cld_data_provider.cc
index de283b4..18e7b8e 100644
--- a/components/translate/content/browser/data_file_browser_cld_data_provider.cc
+++ b/components/translate/content/browser/data_file_browser_cld_data_provider.cc
@@ -2,16 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "data_file_browser_cld_data_provider.h"
+#include "components/translate/content/browser/data_file_browser_cld_data_provider.h"
#include "base/basictypes.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
+#include "base/files/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
+#include "components/translate/content/common/cld_data_source.h"
#include "components/translate/content/common/data_file_cld_data_provider_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
@@ -26,8 +28,8 @@ namespace {
// We also track the offset at which the data starts, and its length.
base::FilePath g_cached_filepath; // guarded by g_file_lock_
base::File* g_cached_file = NULL; // guarded by g_file_lock_
-uint64 g_cached_data_offset = -1; // guarded by g_file_lock_
-uint64 g_cached_data_length = -1; // guarded by g_file_lock_
+uint64 g_cached_data_offset = 0; // guarded by g_file_lock_
+uint64 g_cached_data_length = 0; // guarded by g_file_lock_
// Guards g_cached_filepath
base::LazyInstance<base::Lock> g_file_lock_;
@@ -35,14 +37,6 @@ base::LazyInstance<base::Lock> g_file_lock_;
namespace translate {
-// Implementation of the static factory method from BrowserCldDataProvider,
-// hooking up this specific implementation for all of Chromium.
-BrowserCldDataProvider* CreateBrowserCldDataProviderFor(
- content::WebContents* web_contents) {
- VLOG(1) << "Creating DataFileBrowserCldDataProvider";
- return new DataFileBrowserCldDataProvider(web_contents);
-}
-
void SetCldDataFilePath(const base::FilePath& path) {
VLOG(1) << "Setting CLD data file path to: " << path.value();
base::AutoLock lock(g_file_lock_.Get());
@@ -51,12 +45,15 @@ void SetCldDataFilePath(const base::FilePath& path) {
g_cached_filepath = path;
// For sanity, clean these other values up just in case.
g_cached_file = NULL;
- g_cached_data_length = -1;
- g_cached_data_offset = -1;
+ g_cached_data_length = 0;
+ g_cached_data_offset = 0;
}
base::FilePath GetCldDataFilePath() {
base::AutoLock lock(g_file_lock_.Get());
+ if (g_cached_filepath.empty()) {
+ g_cached_filepath = translate::CldDataSource::Get()->GetCldDataFilePath();
+ }
return g_cached_filepath;
}
@@ -66,6 +63,7 @@ DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider(
}
DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() {
+ // web_contents_ outlives this object
}
bool DataFileBrowserCldDataProvider::OnMessageReceived(
@@ -158,7 +156,7 @@ void DataFileBrowserCldDataProvider::SendCldDataResponseInternal(
}
// Data available, respond to the request.
- const int render_process_handle = render_process_host->GetHandle();
+ base::ProcessHandle render_process_handle = render_process_host->GetHandle();
IPC::PlatformFileForTransit ipc_platform_file =
IPC::GetFileHandleForProcess(handle->GetPlatformFile(),
render_process_handle, false);
diff --git a/components/translate/content/browser/data_file_browser_cld_data_provider.h b/components/translate/content/browser/data_file_browser_cld_data_provider.h
index 7b4f17e..687d2b5 100644
--- a/components/translate/content/browser/data_file_browser_cld_data_provider.h
+++ b/components/translate/content/browser/data_file_browser_cld_data_provider.h
@@ -12,16 +12,21 @@
#include "base/memory/weak_ptr.h"
#include "components/translate/content/browser/browser_cld_data_provider.h"
+namespace content {
+class WebContents;
+}
+
namespace translate {
class DataFileBrowserCldDataProvider : public BrowserCldDataProvider {
public:
explicit DataFileBrowserCldDataProvider(content::WebContents*);
- virtual ~DataFileBrowserCldDataProvider();
+ ~DataFileBrowserCldDataProvider() override;
+
// BrowserCldDataProvider implementations:
- virtual bool OnMessageReceived(const IPC::Message&) override;
- virtual void OnCldDataRequest() override;
- virtual void SendCldDataResponse() override;
+ bool OnMessageReceived(const IPC::Message&) override;
+ void OnCldDataRequest() override;
+ void SendCldDataResponse() override;
private:
void SendCldDataResponseInternal(const base::File*,
diff --git a/components/translate/content/browser/static_browser_cld_data_provider.cc b/components/translate/content/browser/static_browser_cld_data_provider.cc
deleted file mode 100644
index 675db42..0000000
--- a/components/translate/content/browser/static_browser_cld_data_provider.cc
+++ /dev/null
@@ -1,56 +0,0 @@
-// 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 "static_browser_cld_data_provider.h"
-
-#include "base/logging.h"
-#include "content/public/browser/web_contents.h"
-#include "ipc/ipc_message.h"
-
-namespace translate {
-
-// Implementation of the static factory method from BrowserCldDataProvider,
-// hooking up this specific implementation for all of Chromium.
-BrowserCldDataProvider* CreateBrowserCldDataProviderFor(
- content::WebContents* web_contents) {
- // This log line is to help with determining which kind of provider has been
- // configured. See also: chrome://translate-internals
- VLOG(1) << "Creating StaticBrowserCldDataProvider";
- return new StaticBrowserCldDataProvider();
-}
-
-void SetCldDataFilePath(const base::FilePath& path) {
- LOG(WARNING) << "Not supported: SetCldDataFilePath";
- return;
-}
-
-base::FilePath GetCldDataFilePath() {
- return base::FilePath(); // empty path
-}
-
-void ConfigureBrowserCldDataProvider(const void* config) {
- // No-op: data is statically linked
-}
-
-StaticBrowserCldDataProvider::StaticBrowserCldDataProvider() {
-}
-
-StaticBrowserCldDataProvider::~StaticBrowserCldDataProvider() {
-}
-
-bool StaticBrowserCldDataProvider::OnMessageReceived(
- const IPC::Message& message) {
- // No-op: data is statically linked
- return false;
-}
-
-void StaticBrowserCldDataProvider::OnCldDataRequest() {
- // No-op: data is statically linked
-}
-
-void StaticBrowserCldDataProvider::SendCldDataResponse() {
- // No-op: data is statically linked
-}
-
-} // namespace translate
diff --git a/components/translate/content/browser/static_browser_cld_data_provider.h b/components/translate/content/browser/static_browser_cld_data_provider.h
deleted file mode 100644
index 04a4232..0000000
--- a/components/translate/content/browser/static_browser_cld_data_provider.h
+++ /dev/null
@@ -1,28 +0,0 @@
-// 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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_STATIC_BROWSER_CLD_DATA_PROVIDER_H_
-#define COMPONENTS_TRANSLATE_CONTENT_BROWSER_STATIC_BROWSER_CLD_DATA_PROVIDER_H_
-
-#include "base/macros.h"
-#include "components/translate/content/browser/browser_cld_data_provider.h"
-
-namespace translate {
-
-class StaticBrowserCldDataProvider : public BrowserCldDataProvider {
- public:
- explicit StaticBrowserCldDataProvider();
- ~StaticBrowserCldDataProvider() override;
- // BrowserCldDataProvider implementations:
- bool OnMessageReceived(const IPC::Message&) override;
- void OnCldDataRequest() override;
- void SendCldDataResponse() override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(StaticBrowserCldDataProvider);
-};
-
-} // namespace translate
-
-#endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_STATIC_BROWSER_CLD_DATA_PROVIDER_H_
diff --git a/components/translate/content/common/BUILD.gn b/components/translate/content/common/BUILD.gn
index 44affb3..bbcdb10 100644
--- a/components/translate/content/common/BUILD.gn
+++ b/components/translate/content/common/BUILD.gn
@@ -6,9 +6,12 @@ import("//build/config/features.gni")
static_library("common") {
sources = [
+ "cld_data_source.cc",
+ "cld_data_source.h",
+ "data_file_cld_data_provider_messages.cc",
+ "data_file_cld_data_provider_messages.h",
"translate_messages.cc",
"translate_messages.h",
- "cld_data_source.h",
]
deps = [
@@ -19,19 +22,4 @@ static_library("common") {
"//ipc",
]
- if (cld2_data_source == "standalone" || cld2_data_source == "component") {
- sources += [
- "data_file_cld_data_provider_messages.cc",
- "data_file_cld_data_provider_messages.h",
- ]
- }
- if (cld2_data_source == "standalone") {
- sources += [ "standalone_cld_data_source.cc" ]
- }
- if (cld2_data_source == "component") {
- sources += [ "component_cld_data_source.cc" ]
- }
- if (cld2_data_source == "static") {
- sources += [ "static_cld_data_source.cc" ]
- }
}
diff --git a/components/translate/content/common/cld_data_source.cc b/components/translate/content/common/cld_data_source.cc
new file mode 100644
index 0000000..9d6e009c
--- /dev/null
+++ b/components/translate/content/common/cld_data_source.cc
@@ -0,0 +1,112 @@
+// 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 "components/translate/content/common/cld_data_source.h"
+
+#include "base/lazy_instance.h"
+
+namespace {
+
+// This is the global instance managed by Get/Set/SetDefault
+translate::CldDataSource* g_instance = NULL;
+
+// Global instances for the builtin data source types
+base::LazyInstance<translate::CldDataSource>::Leaky g_wrapped_static =
+ LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<translate::CldDataSource>::Leaky g_wrapped_standalone =
+ LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<translate::CldDataSource>::Leaky g_wrapped_component =
+ LAZY_INSTANCE_INITIALIZER;
+
+bool g_disable_sanity_checks_for_test = false;
+
+} // namespace
+
+namespace translate {
+
+CldDataSource::CldDataSource() : m_cached_filepath(), m_file_lock() {
+}
+
+std::string CldDataSource::GetName() {
+ if (this == GetStaticDataSource()) return "static";
+ if (this == GetComponentDataSource()) return "component";
+ if (this == GetStandaloneDataSource()) return "standalone";
+ NOTREACHED() << "Subclass failed to override GetName()";
+ return "unknown";
+}
+
+// static
+void CldDataSource::DisableSanityChecksForTest() {
+ g_disable_sanity_checks_for_test = true;
+}
+
+// static
+void CldDataSource::EnableSanityChecksForTest() {
+ g_disable_sanity_checks_for_test = false;
+}
+
+void CldDataSource::SetCldDataFilePath(const base::FilePath& path) {
+ DCHECK(g_disable_sanity_checks_for_test ||
+ this == GetComponentDataSource() ||
+ this == GetStandaloneDataSource()) << "CLD Data Source misconfigured!";
+ base::AutoLock lock(m_file_lock);
+ m_cached_filepath = path;
+}
+
+base::FilePath CldDataSource::GetCldDataFilePath() {
+ DCHECK(g_disable_sanity_checks_for_test ||
+ this == GetComponentDataSource() ||
+ this == GetStandaloneDataSource()) << "CLD Data Source misconfigured!";
+ base::AutoLock lock(m_file_lock);
+ return m_cached_filepath;
+}
+
+// static
+void CldDataSource::SetDefault(CldDataSource* data_source) {
+ if (g_instance == NULL) Set(data_source);
+}
+
+// static
+void CldDataSource::Set(CldDataSource* data_source) {
+ g_instance = data_source;
+}
+
+// static
+CldDataSource* CldDataSource::Get() {
+ DCHECK(g_instance != NULL) << "No CLD data source was configured!";
+ if (g_instance != NULL) return g_instance;
+ return GetStaticDataSource();
+}
+
+// static
+CldDataSource* CldDataSource::GetStaticDataSource() {
+ return &g_wrapped_static.Get();
+}
+
+// static
+bool CldDataSource::IsUsingStaticDataSource() {
+ return Get()->GetName() == "static";
+}
+
+// static
+CldDataSource* CldDataSource::GetStandaloneDataSource() {
+ return &g_wrapped_standalone.Get();
+}
+
+// static
+bool CldDataSource::IsUsingStandaloneDataSource() {
+ return Get()->GetName() == "standalone";
+}
+
+// static
+CldDataSource* CldDataSource::GetComponentDataSource() {
+ return &g_wrapped_component.Get();
+}
+
+// static
+bool CldDataSource::IsUsingComponentDataSource() {
+ return Get()->GetName() == "component";
+}
+
+} // namespace translate
diff --git a/components/translate/content/common/cld_data_source.h b/components/translate/content/common/cld_data_source.h
index 07a56b0..577c415f 100644
--- a/components/translate/content/common/cld_data_source.h
+++ b/components/translate/content/common/cld_data_source.h
@@ -7,12 +7,29 @@
#include <string>
+#include "base/files/file_path.h"
+#include "base/macros.h"
+#include "base/synchronization/lock.h"
+
+namespace component_updater {
+ // For friend-class declaration, see private section at bottom of class.
+ class CldComponentInstallerTest;
+}
+
namespace translate {
// Provides high-level functionality related to a CLD Data Source.
class CldDataSource {
public:
+ // Generally not used by Chromium code, but available for embedders to
+ // configure additional data sources as subclasses.
+ // Chromium code should use the getters (GetStaticDataSource(),
+ // GetStandaloneDataSource(), and GetComponentDataSource()) and checkers
+ // (IsUsingStaticDataSource(), IsUsingStandaloneDataSource() and
+ // IsUsingComponentDataSource()) instead as appropriate.
+ CldDataSource();
+ virtual ~CldDataSource() {}
// Returns the symbolic name of the data source. In the Chromium
// open-source tree, the following data sources exist:
@@ -24,24 +41,94 @@ class CldDataSource {
// implementations.
//
// Other implementations based upon Chromium may provide CLD differently and
- // may have other names. This method is primarily provided for those
- // non-Chromium implementations; Chromium implementations should use the
- // boolean methods in this class instead:
- // ShouldRegisterForComponentUpdates()
- // ShouldUseStandaloneDataFile()
- static std::string GetName();
-
- // Returns true if the data source needs to receive updates from the
- // Component Updater.
- // This is only true if the data source name is "component", but makes caller
- // logic more generic.
- static bool ShouldRegisterForComponentUpdates();
-
- // Returns true if the data source needs to have the path to the CLD
- // data file configured immediately because it is bundled with Chromium.
- // This is only true if the data source name is "standalone", but makes
- // caller logic more generic.
- static bool ShouldUseStandaloneDataFile();
+ // may have other names.
+ // This method is threadsafe.
+ virtual std::string GetName();
+
+ // For data sources that support a separate CLD data file, configures the path
+ // of that data file.
+ //
+ // The 'component' and 'standalone' data sources need this method to be called
+ // in order to locate the CLD data on disk.
+ // If the data source doesn't need or doesn't support such configuration, this
+ // function is a no-op. This is the case for, e.g., the static data source.
+ // This method is threadsafe.
+ void SetCldDataFilePath(const base::FilePath& path);
+
+ // Returns the path most recently set by SetCldDataFilePath. The initial value
+ // prior to any such call is the empty path. If the data source doesn't
+ // support a data file, returns the empty path.
+ // This method is threadsafe.
+ base::FilePath GetCldDataFilePath();
+
+ // Sets the default data source for this process, i.e. the data source to be
+ // used unless the embedder calls Set(CldDatasource*). This is the method
+ // that normal (i.e., non-test) Chromium code should use; embedders can and
+ // should use the unconditional Set(CldDataSource*) method instead. If a
+ // default data source has already been set, this method does nothing.
+ static void SetDefault(CldDataSource* data_source);
+
+ // Unconditionally sets the data source for this process, overwriting any
+ // previously-configured default. Normal Chromium code should never use this
+ // method; it is provided for embedders to inject a data source from outside
+ // of the Chromium code base. Test code can also use this method to force the
+ // runtime to have a desired behavior.
+ static void Set(CldDataSource* data_source);
+
+ // Returns the data source for this process. Guaranteed to never be null.
+ // If no instance has been set, this returns the same object obtained by
+ // calling GetStaticDataSource(), which is always safe but may fail to
+ // function if the CLD data is not *actually* statically linked.
+ static CldDataSource* Get();
+
+ // Fetch the global instance of the "static" data source.
+ // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*).
+ static CldDataSource* GetStaticDataSource();
+
+ // Returns true if and only if the data source returned by Get() is the
+ // "static" data source.
+ static bool IsUsingStaticDataSource();
+
+ // Fetch the global instance of the "standalone" data source.
+ // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*).
+ static CldDataSource* GetStandaloneDataSource();
+
+ // Returns true if and only if the data source returned by Get() is the
+ // "static" data source.
+ static bool IsUsingStandaloneDataSource();
+
+ // Fetch the global instance of the "component" data source.
+ // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*).
+ static CldDataSource* GetComponentDataSource();
+
+ // Returns true if and only if the data source returned by Get() is the
+ // "static" data source.
+ static bool IsUsingComponentDataSource();
+
+ private:
+ friend class component_updater::CldComponentInstallerTest;
+
+ // For unit test code ONLY. Under normal circumstances the calls to
+ // SetCldDataFilePath() and GetCldDataFilePath() have a DHECK intended to
+ // perform a sanity check on the runtime CLD data source configuration; no
+ // production code should be calling SetCldDataFilePath() or
+ // GetCldDataFilePath() unless the "component" or "standalone" data source is
+ // being used. Unit tests will generally be built with the "static" data
+ // source, and this method allows tests to bypass the DCHECK for testing
+ // purposes.
+ //
+ // Unit tests that use this function should use it in SetUp(), and then call
+ // EnableSanityChecksForTest() in TearDown() for maximum safety.
+ static void DisableSanityChecksForTest();
+
+ // This method [re-]enables the sanity check disabled by
+ // DisableSanityChecksForTest().
+ static void EnableSanityChecksForTest();
+
+ base::FilePath m_cached_filepath; // Guarded by m_file_lock
+ base::Lock m_file_lock; // Guards m_cached_filepath
+
+ DISALLOW_COPY_AND_ASSIGN(CldDataSource);
};
} // namespace translate
diff --git a/components/translate/content/common/component_cld_data_source.cc b/components/translate/content/common/component_cld_data_source.cc
deleted file mode 100644
index feec37e..0000000
--- a/components/translate/content/common/component_cld_data_source.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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 "cld_data_source.h"
-
-namespace translate {
-
-std::string CldDataSource::GetName() {
- return "component";
-}
-
-bool CldDataSource::ShouldRegisterForComponentUpdates() {
- return true;
-}
-
-bool CldDataSource::ShouldUseStandaloneDataFile() {
- return false;
-}
-
-} // namespace translate
diff --git a/components/translate/content/common/standalone_cld_data_source.cc b/components/translate/content/common/standalone_cld_data_source.cc
deleted file mode 100644
index 286ef1d..0000000
--- a/components/translate/content/common/standalone_cld_data_source.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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 "cld_data_source.h"
-
-namespace translate {
-
-std::string CldDataSource::GetName() {
- return "standalone";
-}
-
-bool CldDataSource::ShouldRegisterForComponentUpdates() {
- return false;
-}
-
-bool CldDataSource::ShouldUseStandaloneDataFile() {
- return true;
-}
-
-} // namespace translate
diff --git a/components/translate/content/common/static_cld_data_source.cc b/components/translate/content/common/static_cld_data_source.cc
deleted file mode 100644
index 5673b43..0000000
--- a/components/translate/content/common/static_cld_data_source.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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 "cld_data_source.h"
-
-namespace translate {
-
-std::string CldDataSource::GetName() {
- return "static";
-}
-
-bool CldDataSource::ShouldRegisterForComponentUpdates() {
- return false;
-}
-
-bool CldDataSource::ShouldUseStandaloneDataFile() {
- return false;
-}
-
-} // namespace translate
diff --git a/components/translate/content/renderer/BUILD.gn b/components/translate/content/renderer/BUILD.gn
index db461be..86440fc 100644
--- a/components/translate/content/renderer/BUILD.gn
+++ b/components/translate/content/renderer/BUILD.gn
@@ -6,7 +6,12 @@ import("//build/config/features.gni")
static_library("renderer") {
sources = [
+ "data_file_renderer_cld_data_provider.cc",
+ "data_file_renderer_cld_data_provider.h",
+ "renderer_cld_data_provider.cc",
"renderer_cld_data_provider.h",
+ "renderer_cld_utils.cc",
+ "renderer_cld_utils.h",
"translate_helper.cc",
"translate_helper.h",
]
@@ -28,15 +33,4 @@ static_library("renderer") {
deps += [ "//third_party/cld_2" ]
}
- if (cld2_data_source == "standalone" || cld2_data_source == "component") {
- sources += [
- "data_file_renderer_cld_data_provider.cc",
- "data_file_renderer_cld_data_provider.h",
- ]
- } else if (cld2_data_source == "static") {
- sources += [
- "static_renderer_cld_data_provider.cc",
- "static_renderer_cld_data_provider.h",
- ]
- }
}
diff --git a/components/translate/content/renderer/data_file_renderer_cld_data_provider.cc b/components/translate/content/renderer/data_file_renderer_cld_data_provider.cc
index b7a6ad2..f2af81b 100644
--- a/components/translate/content/renderer/data_file_renderer_cld_data_provider.cc
+++ b/components/translate/content/renderer/data_file_renderer_cld_data_provider.cc
@@ -31,16 +31,6 @@ base::LazyInstance<CLDMmapWrapper>::Leaky g_cld_mmap =
namespace translate {
-// Implementation of the static factory method from RendererCldDataProvider,
-// hooking up this specific implementation for all of Chromium.
-RendererCldDataProvider* CreateRendererCldDataProviderFor(
- content::RenderViewObserver* render_view_observer) {
- // This log line is to help with determining which kind of provider has been
- // configured. See also: chrome://translate-internals
- VLOG(1) << "Creating DataFileRendererCldDataProvider";
- return new DataFileRendererCldDataProvider(render_view_observer);
-}
-
DataFileRendererCldDataProvider::DataFileRendererCldDataProvider(
content::RenderViewObserver* render_view_observer)
: render_view_observer_(render_view_observer) {
diff --git a/components/translate/content/renderer/data_file_renderer_cld_data_provider.h b/components/translate/content/renderer/data_file_renderer_cld_data_provider.h
index 87c1551..acd6261 100644
--- a/components/translate/content/renderer/data_file_renderer_cld_data_provider.h
+++ b/components/translate/content/renderer/data_file_renderer_cld_data_provider.h
@@ -10,17 +10,22 @@
#include "components/translate/content/renderer/renderer_cld_data_provider.h"
#include "ipc/ipc_platform_file.h"
+namespace content {
+class RenderViewObserver;
+}
+
namespace translate {
class DataFileRendererCldDataProvider : public RendererCldDataProvider {
public:
explicit DataFileRendererCldDataProvider(content::RenderViewObserver*);
- virtual ~DataFileRendererCldDataProvider();
+ ~DataFileRendererCldDataProvider() override;
+
// RendererCldDataProvider implementations:
- virtual bool OnMessageReceived(const IPC::Message&) override;
- virtual void SendCldDataRequest() override;
- virtual void SetCldAvailableCallback(base::Callback<void(void)>) override;
- virtual bool IsCldDataAvailable() override;
+ bool OnMessageReceived(const IPC::Message&) override;
+ void SendCldDataRequest() override;
+ void SetCldAvailableCallback(base::Callback<void(void)>) override;
+ bool IsCldDataAvailable() override;
private:
void OnCldDataAvailable(const IPC::PlatformFileForTransit ipc_file_handle,
diff --git a/components/translate/content/renderer/renderer_cld_data_provider.cc b/components/translate/content/renderer/renderer_cld_data_provider.cc
new file mode 100644
index 0000000..d97f3bc
--- /dev/null
+++ b/components/translate/content/renderer/renderer_cld_data_provider.cc
@@ -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.
+
+#include "base/lazy_instance.h"
+#include "components/translate/content/renderer/renderer_cld_data_provider.h"
+
+namespace {
+
+// Our global default instance, alive for the entire lifetime of the process.
+translate::RendererCldDataProvider* g_instance = NULL;
+
+// The default factory, which produces no-op instances of
+// RendererCldDataProvider suitable for use when CLD data is statically-linked.
+base::LazyInstance<translate::RendererCldDataProvider>::Leaky
+g_wrapped_default = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+
+namespace translate {
+
+bool RendererCldDataProvider::IsCldDataAvailable() {
+ return true;
+}
+
+bool RendererCldDataProvider::OnMessageReceived(const IPC::Message& message) {
+ return false; // Message not handled
+}
+
+void RendererCldDataProvider::SetCldAvailableCallback
+(base::Callback<void(void)> callback) {
+ callback.Run();
+}
+
+// static
+void RendererCldDataProvider::SetDefault(
+ RendererCldDataProvider* instance) {
+ if (!IsInitialized()) Set(instance);
+}
+
+// static
+void RendererCldDataProvider::Set(
+ RendererCldDataProvider* instance) {
+ g_instance = instance;
+}
+
+// static
+bool RendererCldDataProvider::IsInitialized() {
+ return g_instance != NULL;
+}
+
+// static
+RendererCldDataProvider* RendererCldDataProvider::Get() {
+ if (IsInitialized()) return g_instance;
+ return &g_wrapped_default.Get();
+}
+
+} // namespace translate
diff --git a/components/translate/content/renderer/renderer_cld_data_provider.h b/components/translate/content/renderer/renderer_cld_data_provider.h
index fb0a333..7d20bb7 100644
--- a/components/translate/content/renderer/renderer_cld_data_provider.h
+++ b/components/translate/content/renderer/renderer_cld_data_provider.h
@@ -6,29 +6,29 @@
#define COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATA_PROVIDER_H_
#include "base/callback.h"
+#include "base/macros.h"
#include "ipc/ipc_listener.h"
namespace IPC {
class Message;
}
-namespace content {
-class RenderViewObserver;
-}
-
namespace translate {
// Renderer-side interface responsible for providing CLD data.
+// The embedder should set an instance as soon as feasible during startup.
// The implementation must be paired with a browser-side implementation of
-// the BrowserCldDataProvider class:
-//
-// components/translate/content/browser/browser_cld_data_provider.h
+// the BrowserCldDataProvider class, typically created by a
+// BrowserCldDataProviderFactory:
+// ../browser/browser_cld_data_provider_factory.h
+// ../browser/browser_cld_data_provider.h
//
-// ... and the glue between them is typically a pair of request/response IPC
-// messages using the CldDataProviderMsgStart IPCMessageStart enumerated
-// constant from ipc_message_start.h
+// The glue between the browser and renderer processes is typically a pair of
+// request/response IPC messages using the CldDataProviderMsgStart
+// "IPCMessageStart" enumerated constant from ipc_message_start.h.
class RendererCldDataProvider : public IPC::Listener {
public:
+ RendererCldDataProvider() {}
~RendererCldDataProvider() override {}
// (Inherited from IPC::Listener)
@@ -39,33 +39,62 @@ class RendererCldDataProvider : public IPC::Listener {
// loop thread.
// This method is defined as virtual in order to force the implementation to
// define the specific IPC message(s) that it handles.
- virtual bool OnMessageReceived(const IPC::Message&) = 0;
+ // The default implementation does nothing and returns false.
+ virtual bool OnMessageReceived(const IPC::Message& message) override;
// Invoked by the renderer process to request that CLD data be obtained and
// that CLD be initialized with it. The implementation is expected to
// communicate with the paired BrowserCldDataProvider implementation on the
// browser side.
// This method must be invoked on the message loop thread.
- virtual void SendCldDataRequest() = 0;
+ // The default implementation does nothing.
+ virtual void SendCldDataRequest() {}
// Convenience method that tracks whether or not CLD data is available.
// This method can be used in the absence of a callback (i.e., if the caller
// wants a simple way to check the state of CLD data availability without
// keeping a separate boolean flag tripped by a callback).
- virtual bool IsCldDataAvailable() = 0;
+ // The default implementation always returns true.
+ virtual bool IsCldDataAvailable();
// Sets a callback that will be invoked when CLD data is successfully
// obtained from the paired BrowserCldDataProvider implementation on the
// browser side, after CLD has been successfully initialized.
// Both the initialization of CLD2 as well as the invocation of the callback
// must happen on the message loop thread.
- virtual void SetCldAvailableCallback(base::Callback<void(void)>) = 0;
-};
+ // The default implementation immediately invokes the callback.
+ virtual void SetCldAvailableCallback(base::Callback<void(void)> callback);
+
+ // Sets the default data provider for this process, i.e. the provider to be
+ // used unless the embedder calls Set(RendererCldDataProvider*). This is the
+ // method that normal (i.e., non-test) Chromium code should use; embedders can
+ // and should use the unconditional Set(RendererCldDataProvider*) method
+ // instead. If a default provider has already been set, this method does
+ // nothing.
+ static void SetDefault(RendererCldDataProvider* instance);
-// Static factory function defined by the implementation that produces a new
-// provider for the specified render view host.
-RendererCldDataProvider* CreateRendererCldDataProviderFor(
- content::RenderViewObserver*);
+ // Unconditionally sets the data provider for this process, overwriting any
+ // previously-configured default. Normal Chromium code should never use this
+ // method; it is provided for embedders to inject a provider from outside of
+ // the Chromium code base. Test code can also use this method to force the
+ // runtime to have a desired behavior.
+ //
+ // The caller is responsible for the lifecycle of the instance. In general,
+ // the instance passed here should live throughout the lifetime of the
+ // process.
+ static void Set(RendererCldDataProvider* instance);
+
+ // Returns true if and only if the current instance for this process is not
+ // NULL.
+ static bool IsInitialized();
+
+ // Returns the instance of the provider previously set by Set(...).
+ // If no instance has been set, a default no-op provider will be returned.
+ static RendererCldDataProvider* Get();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RendererCldDataProvider);
+};
} // namespace translate
diff --git a/components/translate/content/renderer/renderer_cld_utils.cc b/components/translate/content/renderer/renderer_cld_utils.cc
new file mode 100644
index 0000000..210b977
--- /dev/null
+++ b/components/translate/content/renderer/renderer_cld_utils.cc
@@ -0,0 +1,39 @@
+// 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 "components/translate/content/renderer/renderer_cld_utils.h"
+
+#include "components/translate/content/common/cld_data_source.h"
+#include "components/translate/content/renderer/renderer_cld_data_provider.h"
+
+namespace translate {
+
+// static
+void RendererCldUtils::ConfigureDefaultDataProvider() {
+ if (!RendererCldDataProvider::IsInitialized()) {
+ RendererCldDataProvider* provider = NULL;
+ CldDataSource* data_source = NULL;
+
+ // Maintainers: Customize platform defaults here as necessary.
+ // It is appropriate to ifdef this code and customize per-platform.
+ //
+ // Remember to update GYP/GN dependencies of high-level targets as well:
+ // - For the NONE or STATIC data sources, depend upon
+ // third_party/cld_2/cld_2.gyp:cld2_static.
+ // - For the COMPONENT or STANDALONE data sources, depend upon
+ // third_party/cld_2/cld_2.gyp:cld2_dynamic.
+ // - For any other sources, the embedder should already have set a provider
+ // and so this code should never be invoked.
+ // -----------------------------------------------------------------------
+ provider = new RendererCldDataProvider();
+ data_source = CldDataSource::GetStaticDataSource();
+ // -----------------------------------------------------------------------
+
+ // Apply the values defined above
+ RendererCldDataProvider::SetDefault(provider);
+ CldDataSource::SetDefault(data_source);
+ }
+}
+
+} // namespace translate
diff --git a/components/translate/content/renderer/renderer_cld_utils.h b/components/translate/content/renderer/renderer_cld_utils.h
new file mode 100644
index 0000000..e2eeb20
--- /dev/null
+++ b/components/translate/content/renderer/renderer_cld_utils.h
@@ -0,0 +1,41 @@
+// 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 COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_UTILS_H_
+#define COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_UTILS_H_
+
+#include "base/macros.h"
+
+namespace translate {
+
+// Renderer-side utilities for dealing with CLD data source configuration.
+// This class exists primarily to avoid duplicating code in high-level targets
+// such as the Chrome browser, Chrome shell, and so on.
+class RendererCldUtils {
+ public:
+ // Perform conditional configuration of the CLD data provider.
+ // If no specific RendererCldDataProvider has yet been set, an instance is
+ // created and assigned as appropriate for each platform. If a specific
+ // RendererCldDataProviderFactory *has* been set, this method does nothing
+ // (as a side effect of setting the default factory, subsequent invocations
+ // do nothing - making this method reentrant).
+ //
+ // This method will not overwrite a data provider configured by an embedder.
+ //
+ // Maintainers: This is the default data source. It is appropriate to
+ // ifdef this code and customize the default per-platform. This method must
+ // be invoked prior to registering components with the component updater if
+ // the component updater implementation is to be used; it is best to ensure
+ // that this method is called as early as possible.
+ static void ConfigureDefaultDataProvider();
+
+ private:
+ RendererCldUtils() {}
+ ~RendererCldUtils() {}
+ DISALLOW_COPY_AND_ASSIGN(RendererCldUtils);
+};
+
+} // namespace translate
+
+#endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_UTILS_H_
diff --git a/components/translate/content/renderer/static_renderer_cld_data_provider.cc b/components/translate/content/renderer/static_renderer_cld_data_provider.cc
deleted file mode 100644
index bb2ccf1..0000000
--- a/components/translate/content/renderer/static_renderer_cld_data_provider.cc
+++ /dev/null
@@ -1,45 +0,0 @@
-// 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 "static_renderer_cld_data_provider.h"
-
-#include "content/public/renderer/render_view_observer.h"
-#include "ipc/ipc_message.h"
-
-namespace translate {
-// Implementation of the static factory method from RendererCldDataProvider,
-// hooking up this specific implementation for all of Chromium.
-RendererCldDataProvider* CreateRendererCldDataProviderFor(
- content::RenderViewObserver* render_view_observer) {
- return new StaticRendererCldDataProvider();
-}
-
-StaticRendererCldDataProvider::StaticRendererCldDataProvider() {
-}
-
-StaticRendererCldDataProvider::~StaticRendererCldDataProvider() {
-}
-
-bool StaticRendererCldDataProvider::OnMessageReceived(
- const IPC::Message& message) {
- // No-op: data is statically linked
- return false;
-}
-
-void StaticRendererCldDataProvider::SendCldDataRequest() {
- // No-op: data is statically linked
-}
-
-bool StaticRendererCldDataProvider::IsCldDataAvailable() {
- // No-op: data is statically linked
- return true;
-}
-
-void StaticRendererCldDataProvider::SetCldAvailableCallback(
- base::Callback<void(void)> callback) {
- // Data is statically linked, so just call immediately.
- callback.Run();
-}
-
-} // namespace translate
diff --git a/components/translate/content/renderer/static_renderer_cld_data_provider.h b/components/translate/content/renderer/static_renderer_cld_data_provider.h
deleted file mode 100644
index a2f82f9..0000000
--- a/components/translate/content/renderer/static_renderer_cld_data_provider.h
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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 COMPONENTS_TRANSLATE_CONTENT_RENDERER_STATIC_RENDERER_CLD_DATA_PROVIDER_H_
-#define COMPONENTS_TRANSLATE_CONTENT_RENDERER_STATIC_RENDERER_CLD_DATA_PROVIDER_H_
-
-#include "base/files/file.h"
-#include "base/files/memory_mapped_file.h"
-#include "base/lazy_instance.h"
-#include "base/macros.h"
-#include "base/memory/weak_ptr.h"
-#include "components/translate/content/renderer/renderer_cld_data_provider.h"
-#include "ipc/ipc_platform_file.h"
-
-namespace translate {
-
-class StaticRendererCldDataProvider : public RendererCldDataProvider {
- public:
- explicit StaticRendererCldDataProvider();
- ~StaticRendererCldDataProvider() override;
- // RendererCldDataProvider implementations:
- bool OnMessageReceived(const IPC::Message&) override;
- void SendCldDataRequest() override;
- void SetCldAvailableCallback(base::Callback<void(void)>) override;
- bool IsCldDataAvailable() override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(StaticRendererCldDataProvider);
-};
-
-} // namespace translate
-
-#endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_STATIC_RENDERER_CLD_DATA_PROVIDER_H_
diff --git a/components/translate/content/renderer/translate_helper.cc b/components/translate/content/renderer/translate_helper.cc
index 5aff9c8..e2da2f9 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -13,6 +13,8 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/translate/content/common/translate_messages.h"
+#include "components/translate/content/renderer/renderer_cld_data_provider.h"
+#include "components/translate/content/renderer/renderer_cld_utils.h"
#include "components/translate/core/common/translate_constants.h"
#include "components/translate/core/common/translate_metrics.h"
#include "components/translate/core/common/translate_util.h"
@@ -82,7 +84,8 @@ TranslateHelper::TranslateHelper(content::RenderView* render_view,
: content::RenderViewObserver(render_view),
page_seq_no_(0),
translation_pending_(false),
- cld_data_provider_(translate::CreateRendererCldDataProviderFor(this)),
+ cld_data_provider_(
+ static_cast<translate::RendererCldDataProvider*>(NULL)),
cld_data_polling_started_(false),
cld_data_polling_canceled_(false),
deferred_page_capture_(false),
@@ -91,6 +94,8 @@ TranslateHelper::TranslateHelper(content::RenderView* render_view,
extension_group_(extension_group),
extension_scheme_(extension_scheme),
weak_method_factory_(this) {
+ translate::RendererCldUtils::ConfigureDefaultDataProvider();
+ cld_data_provider_ = translate::RendererCldDataProvider::Get();
}
TranslateHelper::~TranslateHelper() {
diff --git a/components/translate/content/renderer/translate_helper.h b/components/translate/content/renderer/translate_helper.h
index 4d9275d..696217c 100644
--- a/components/translate/content/renderer/translate_helper.h
+++ b/components/translate/content/renderer/translate_helper.h
@@ -244,7 +244,10 @@ class TranslateHelper : public content::RenderViewObserver {
base::TimeTicks language_determined_time_;
// Provides CLD data for this process.
- scoped_ptr<RendererCldDataProvider> cld_data_provider_;
+ // The pointer is owned by the global in RendererCldDataProvider and has the
+ // same lifetime as the process itself, so this field is simply a raw pointer
+ // instead of a scoped_ptr.
+ RendererCldDataProvider* cld_data_provider_;
// Whether or not polling for CLD2 data has started.
bool cld_data_polling_started_;