OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | 5 #ifndef CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
6 #define CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | 6 #define CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/string16.h" | 12 #include "base/string16.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "content/public/renderer/render_process_observer.h" | |
15 #include "ipc/ipc_platform_file.h" | |
14 | 16 |
15 namespace file_util { | 17 namespace file_util { |
16 class MemoryMappedFile; | 18 class MemoryMappedFile; |
17 } | 19 } |
18 | 20 |
19 typedef struct _HyphenDict HyphenDict; | 21 typedef struct _HyphenDict HyphenDict; |
20 | 22 |
21 namespace content { | 23 namespace content { |
24 class RenderThread; | |
22 | 25 |
23 // A class that hyphenates a word. This class encapsulates the hyphen library | 26 // A class that hyphenates a word. This class encapsulates the hyphen library |
24 // and manages resources used by the library. When this class uses a huge | 27 // and manages resources used by the library. When this class uses a huge |
25 // dictionary, it takes lots of memory (~1.3MB for English). A renderer should | 28 // dictionary, it takes lots of memory (~1.3MB for English). A renderer should |
26 // create this object only when it renders a page that needs hyphenation and | 29 // create this object only when it renders a page that needs hyphenation and |
27 // deletes it when it moves to a page that does not need hyphenation. | 30 // deletes it when it moves to a page that does not need hyphenation. |
28 class CONTENT_EXPORT Hyphenator { | 31 class CONTENT_EXPORT Hyphenator : public RenderProcessObserver { |
29 public: | 32 public: |
30 explicit Hyphenator(base::PlatformFile file); | 33 explicit Hyphenator(base::PlatformFile file); |
31 ~Hyphenator(); | 34 virtual ~Hyphenator(); |
32 | 35 |
33 // Initializes the hyphen library and allocates resources needed for | 36 // Initializes the hyphen library and allocates resources needed for |
34 // hyphenation. | 37 // hyphenation. |
35 bool Initialize(); | 38 bool Initialize(); |
36 | 39 |
40 bool Attach(content::RenderThread* thread, const string16& locale); | |
41 | |
42 // Returns whether this object can hyphenate words. When this object does not | |
43 // have a dictionary file attached, this function sends an IPC request to open | |
44 // the file. | |
45 bool CanHyphenate(const string16& locale); | |
46 | |
37 // Returns the last hyphenation point, the position where we can insert a | 47 // Returns the last hyphenation point, the position where we can insert a |
38 // hyphen, before the given position. If there are not any hyphenation points, | 48 // hyphen, before the given position. If there are not any hyphenation points, |
39 // this function returns 0. | 49 // this function returns 0. |
40 size_t ComputeLastHyphenLocation(const string16& word, size_t before_index); | 50 size_t ComputeLastHyphenLocation(const string16& word, size_t before_index); |
41 | 51 |
52 // content::RenderProcessObserver implementation. | |
jam
2012/08/27 16:31:10
nit: no need for "content::"
Hironori Bono
2012/08/28 06:54:37
Done. Thanks for your suggestion.
| |
53 virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE; | |
54 | |
42 private: | 55 private: |
56 void OnSetDictionary(IPC::PlatformFileForTransit rule_file); | |
57 | |
43 // The dictionary used by the hyphen library. | 58 // The dictionary used by the hyphen library. |
44 HyphenDict* dictionary_; | 59 HyphenDict* dictionary_; |
45 | 60 |
46 // The dictionary file and its memory-mapping object. (Our copy of the hyphen | 61 // The dictionary file and its memory-mapping object. (Our copy of the hyphen |
47 // library uses a memory-mapped file opened by a browser so renderers can use | 62 // library uses a memory-mapped file opened by a browser so renderers can use |
48 // it without opening the file.) | 63 // it without opening the file.) |
64 string16 locale_; | |
49 base::PlatformFile rule_file_; | 65 base::PlatformFile rule_file_; |
50 scoped_ptr<file_util::MemoryMappedFile> rule_map_; | 66 scoped_ptr<file_util::MemoryMappedFile> rule_map_; |
51 | 67 |
52 // A cached result. WebKit often calls ComputeLastHyphenLocation with the same | 68 // A cached result. WebKit often calls ComputeLastHyphenLocation with the same |
53 // word multiple times to find the best hyphenation point when it finds a line | 69 // word multiple times to find the best hyphenation point when it finds a line |
54 // break. On the other hand, the hyphen library returns all hyphenation points | 70 // break. On the other hand, the hyphen library returns all hyphenation points |
55 // for a word. This class caches the hyphenation points returned by the hyphen | 71 // for a word. This class caches the hyphenation points returned by the hyphen |
56 // library to avoid calling the library multiple times. | 72 // library to avoid calling the library multiple times. |
57 string16 word_; | 73 string16 word_; |
58 bool result_; | 74 bool result_; |
59 std::vector<int> hyphen_offsets_; | 75 std::vector<int> hyphen_offsets_; |
60 | 76 |
61 DISALLOW_COPY_AND_ASSIGN(Hyphenator); | 77 DISALLOW_COPY_AND_ASSIGN(Hyphenator); |
62 }; | 78 }; |
63 | 79 |
64 } // namespace content | 80 } // namespace content |
65 | 81 |
66 #endif // CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | 82 #endif // CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
OLD | NEW |