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/memory/weak_ptr.h" | |
11 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
12 #include "base/string16.h" | 13 #include "base/string16.h" |
13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
14 | 15 |
15 namespace file_util { | 16 namespace file_util { |
16 class MemoryMappedFile; | 17 class MemoryMappedFile; |
17 } | 18 } |
18 | 19 |
19 typedef struct _HyphenDict HyphenDict; | 20 typedef struct _HyphenDict HyphenDict; |
20 | 21 |
21 namespace content { | 22 namespace content { |
23 class RenderThread; | |
22 | 24 |
23 // A class that hyphenates a word. This class encapsulates the hyphen library | 25 // 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 | 26 // 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 | 27 // 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 | 28 // 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. | 29 // deletes it when it moves to a page that does not need hyphenation. |
28 class CONTENT_EXPORT Hyphenator { | 30 class CONTENT_EXPORT Hyphenator { |
29 public: | 31 public: |
30 explicit Hyphenator(base::PlatformFile file); | 32 explicit Hyphenator(base::PlatformFile file); |
31 ~Hyphenator(); | 33 ~Hyphenator(); |
32 | 34 |
33 // Initializes the hyphen library and allocates resources needed for | 35 // Initializes the hyphen library and allocates resources needed for |
34 // hyphenation. | 36 // hyphenation. |
35 bool Initialize(); | 37 bool Initialize(); |
36 | 38 |
39 // Attaches this object to a render thread. This function adds a MessageFilter | |
40 // object to the specified render thread to listen hyphenator messages and | |
41 // sends a HyphenatorHostMsg_OpenDictionary message. | |
42 bool Attach(content::RenderThread* thread, const string16& locale); | |
jam
2012/08/23 21:34:29
ditto
Hironori Bono
2012/08/27 06:57:28
Done. Thanks for noticing this redundant comment.
jam
2012/08/27 16:31:10
I had meant the "content::"
| |
43 | |
44 // Returns whether this object can hyphenate words. When this object does not | |
45 // have a dictionary file attached, this function sends an IPC request to open | |
46 // the file. | |
47 bool CanHyphenate(const string16& locale); | |
48 | |
37 // Returns the last hyphenation point, the position where we can insert a | 49 // 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, | 50 // hyphen, before the given position. If there are not any hyphenation points, |
39 // this function returns 0. | 51 // this function returns 0. |
40 size_t ComputeLastHyphenLocation(const string16& word, size_t before_index); | 52 size_t ComputeLastHyphenLocation(const string16& word, size_t before_index); |
41 | 53 |
54 // Sets a hyphenation dictionary to this object. This function is expected to | |
55 // be called when a MessageFilter object receives HyphenatorMsg_SetDictionary | |
56 // messages, i.e. when a browser opens the hyphenation dictionary requested by | |
57 // this object. | |
58 void SetDictionary(base::PlatformFile rule_file); | |
59 | |
42 private: | 60 private: |
43 // The dictionary used by the hyphen library. | 61 // The dictionary used by the hyphen library. |
44 HyphenDict* dictionary_; | 62 HyphenDict* dictionary_; |
45 | 63 |
46 // The dictionary file and its memory-mapping object. (Our copy of the hyphen | 64 // 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 | 65 // library uses a memory-mapped file opened by a browser so renderers can use |
48 // it without opening the file.) | 66 // it without opening the file.) |
67 string16 locale_; | |
49 base::PlatformFile rule_file_; | 68 base::PlatformFile rule_file_; |
50 scoped_ptr<file_util::MemoryMappedFile> rule_map_; | 69 scoped_ptr<file_util::MemoryMappedFile> rule_map_; |
51 | 70 |
52 // A cached result. WebKit often calls ComputeLastHyphenLocation with the same | 71 // 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 | 72 // 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 | 73 // 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 | 74 // for a word. This class caches the hyphenation points returned by the hyphen |
56 // library to avoid calling the library multiple times. | 75 // library to avoid calling the library multiple times. |
57 string16 word_; | 76 string16 word_; |
58 bool result_; | 77 bool result_; |
59 std::vector<int> hyphen_offsets_; | 78 std::vector<int> hyphen_offsets_; |
60 | 79 |
80 base::WeakPtrFactory<Hyphenator> weak_factory_; | |
81 | |
61 DISALLOW_COPY_AND_ASSIGN(Hyphenator); | 82 DISALLOW_COPY_AND_ASSIGN(Hyphenator); |
62 }; | 83 }; |
63 | 84 |
64 } // namespace content | 85 } // namespace content |
65 | 86 |
66 #endif // CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | 87 #endif // CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
OLD | NEW |