Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(925)

Side by Side Diff: components/translate/content/browser/data_file_browser_cld_data_provider.cc

Issue 461633002: Refactor language detection logic to allow non-static CLD data sources. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Create a factory for test harnesses and use it Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "data_file_browser_cld_data_provider.h" 5 #include "data_file_browser_cld_data_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "base/task_runner.h" 15 #include "base/task_runner.h"
16 #include "components/translate/content/common/cld_data_source.h"
16 #include "components/translate/content/common/data_file_cld_data_provider_messag es.h" 17 #include "components/translate/content/common/data_file_cld_data_provider_messag es.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h" 20 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
21 #include "ipc/ipc_message.h" 22 #include "ipc/ipc_message.h"
22 #include "ipc/ipc_message_macros.h" 23 #include "ipc/ipc_message_macros.h"
23 #include "ipc/ipc_platform_file.h" 24 #include "ipc/ipc_platform_file.h"
24 25
25 namespace { 26 namespace {
26 // The data file, cached as long as the process stays alive. 27 // The data file, cached as long as the process stays alive.
27 // We also track the offset at which the data starts, and its length. 28 // We also track the offset at which the data starts, and its length.
28 base::FilePath g_cached_filepath; // guarded by g_file_lock_ 29 base::FilePath g_cached_filepath; // guarded by g_file_lock_
29 base::File* g_cached_file = NULL; // guarded by g_file_lock_ 30 base::File* g_cached_file = NULL; // guarded by g_file_lock_
30 uint64 g_cached_data_offset = -1; // guarded by g_file_lock_ 31 uint64 g_cached_data_offset = -1; // guarded by g_file_lock_
31 uint64 g_cached_data_length = -1; // guarded by g_file_lock_ 32 uint64 g_cached_data_length = -1; // guarded by g_file_lock_
32 33
33 // Guards g_cached_filepath 34 // Guards g_cached_filepath
34 base::LazyInstance<base::Lock> g_file_lock_; 35 base::LazyInstance<base::Lock> g_file_lock_;
35 } // namespace 36 } // namespace
36 37
37 namespace translate { 38 namespace translate {
38 39
39 // Implementation of the static factory method from BrowserCldDataProvider,
40 // hooking up this specific implementation for all of Chromium.
41 BrowserCldDataProvider* CreateBrowserCldDataProviderFor(
42 content::WebContents* web_contents) {
43 VLOG(1) << "Creating DataFileBrowserCldDataProvider";
44 return new DataFileBrowserCldDataProvider(web_contents);
45 }
46
47 void SetCldDataFilePath(const base::FilePath& path) { 40 void SetCldDataFilePath(const base::FilePath& path) {
48 VLOG(1) << "Setting CLD data file path to: " << path.value(); 41 VLOG(1) << "Setting CLD data file path to: " << path.value();
49 base::AutoLock lock(g_file_lock_.Get()); 42 base::AutoLock lock(g_file_lock_.Get());
50 if (g_cached_filepath == path) 43 if (g_cached_filepath == path)
51 return; // no change necessary 44 return; // no change necessary
52 g_cached_filepath = path; 45 g_cached_filepath = path;
53 // For sanity, clean these other values up just in case. 46 // For sanity, clean these other values up just in case.
54 g_cached_file = NULL; 47 g_cached_file = NULL;
55 g_cached_data_length = -1; 48 g_cached_data_length = -1;
56 g_cached_data_offset = -1; 49 g_cached_data_offset = -1;
57 } 50 }
58 51
59 base::FilePath GetCldDataFilePath() { 52 base::FilePath GetCldDataFilePath() {
60 base::AutoLock lock(g_file_lock_.Get()); 53 base::AutoLock lock(g_file_lock_.Get());
54 if (g_cached_filepath == NULL) {
55 g_cached_filepath = translate::CldDataSource::Get()->GetCldDataFilePath();
56 }
61 return g_cached_filepath; 57 return g_cached_filepath;
62 } 58 }
63 59
64 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider( 60 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider(
65 content::WebContents* web_contents) 61 content::WebContents* web_contents)
66 : web_contents_(web_contents), weak_pointer_factory_() { 62 : web_contents_(web_contents), weak_pointer_factory_() {
67 } 63 }
68 64
69 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() { 65 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() {
70 } 66 }
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 // Else, this request has taken care of it all. Cache all info. 230 // Else, this request has taken care of it all. Cache all info.
235 VLOG(1) << "Caching CLD data file information."; 231 VLOG(1) << "Caching CLD data file information.";
236 g_cached_file = file.release(); 232 g_cached_file = file.release();
237 g_cached_data_offset = data_offset; 233 g_cached_data_offset = data_offset;
238 g_cached_data_length = data_length; 234 g_cached_data_length = data_length;
239 } 235 }
240 } 236 }
241 } 237 }
242 238
243 } // namespace translate 239 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698