OLD | NEW |
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 #ifndef COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATA_PROVIDER_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATA_PROVIDER_H_ |
6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATA_PROVIDER_H_ | 6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATA_PROVIDER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" |
9 #include "ipc/ipc_listener.h" | 10 #include "ipc/ipc_listener.h" |
10 | 11 |
11 namespace IPC { | 12 namespace IPC { |
12 class Message; | 13 class Message; |
13 } | 14 } |
14 | 15 |
15 namespace content { | |
16 class RenderViewObserver; | |
17 } | |
18 | |
19 namespace translate { | 16 namespace translate { |
20 | 17 |
21 // Renderer-side interface responsible for providing CLD data. | 18 // Renderer-side interface responsible for providing CLD data. |
| 19 // The embedder should set an instance as soon as feasible during startup. |
22 // The implementation must be paired with a browser-side implementation of | 20 // The implementation must be paired with a browser-side implementation of |
23 // the BrowserCldDataProvider class: | 21 // the BrowserCldDataProvider class, typically created by a |
| 22 // BrowserCldDataProviderFactory: |
| 23 // ../browser/browser_cld_data_provider_factory.h |
| 24 // ../browser/browser_cld_data_provider.h |
24 // | 25 // |
25 // components/translate/content/browser/browser_cld_data_provider.h | 26 // The glue between the browser and renderer processes is typically a pair of |
26 // | 27 // request/response IPC messages using the CldDataProviderMsgStart |
27 // ... and the glue between them is typically a pair of request/response IPC | 28 // "IPCMessageStart" enumerated constant from ipc_message_start.h. |
28 // messages using the CldDataProviderMsgStart IPCMessageStart enumerated | |
29 // constant from ipc_message_start.h | |
30 class RendererCldDataProvider : public IPC::Listener { | 29 class RendererCldDataProvider : public IPC::Listener { |
31 public: | 30 public: |
| 31 RendererCldDataProvider() {} |
32 ~RendererCldDataProvider() override {} | 32 ~RendererCldDataProvider() override {} |
33 | 33 |
34 // (Inherited from IPC::Listener) | 34 // (Inherited from IPC::Listener) |
35 // If the specified message is a response for CLD data, attempts to | 35 // If the specified message is a response for CLD data, attempts to |
36 // initialize CLD2 and returns true in all cases. If initialization is | 36 // initialize CLD2 and returns true in all cases. If initialization is |
37 // successful and a callback has been configured via | 37 // successful and a callback has been configured via |
38 // SetCldAvailableCallback(...), that callback is invoked from the message | 38 // SetCldAvailableCallback(...), that callback is invoked from the message |
39 // loop thread. | 39 // loop thread. |
40 // This method is defined as virtual in order to force the implementation to | 40 // This method is defined as virtual in order to force the implementation to |
41 // define the specific IPC message(s) that it handles. | 41 // define the specific IPC message(s) that it handles. |
42 virtual bool OnMessageReceived(const IPC::Message&) = 0; | 42 // The default implementation does nothing and returns false. |
| 43 virtual bool OnMessageReceived(const IPC::Message& message) override; |
43 | 44 |
44 // Invoked by the renderer process to request that CLD data be obtained and | 45 // Invoked by the renderer process to request that CLD data be obtained and |
45 // that CLD be initialized with it. The implementation is expected to | 46 // that CLD be initialized with it. The implementation is expected to |
46 // communicate with the paired BrowserCldDataProvider implementation on the | 47 // communicate with the paired BrowserCldDataProvider implementation on the |
47 // browser side. | 48 // browser side. |
48 // This method must be invoked on the message loop thread. | 49 // This method must be invoked on the message loop thread. |
49 virtual void SendCldDataRequest() = 0; | 50 // The default implementation does nothing. |
| 51 virtual void SendCldDataRequest() {} |
50 | 52 |
51 // Convenience method that tracks whether or not CLD data is available. | 53 // Convenience method that tracks whether or not CLD data is available. |
52 // This method can be used in the absence of a callback (i.e., if the caller | 54 // This method can be used in the absence of a callback (i.e., if the caller |
53 // wants a simple way to check the state of CLD data availability without | 55 // wants a simple way to check the state of CLD data availability without |
54 // keeping a separate boolean flag tripped by a callback). | 56 // keeping a separate boolean flag tripped by a callback). |
55 virtual bool IsCldDataAvailable() = 0; | 57 // The default implementation always returns true. |
| 58 virtual bool IsCldDataAvailable(); |
56 | 59 |
57 // Sets a callback that will be invoked when CLD data is successfully | 60 // Sets a callback that will be invoked when CLD data is successfully |
58 // obtained from the paired BrowserCldDataProvider implementation on the | 61 // obtained from the paired BrowserCldDataProvider implementation on the |
59 // browser side, after CLD has been successfully initialized. | 62 // browser side, after CLD has been successfully initialized. |
60 // Both the initialization of CLD2 as well as the invocation of the callback | 63 // Both the initialization of CLD2 as well as the invocation of the callback |
61 // must happen on the message loop thread. | 64 // must happen on the message loop thread. |
62 virtual void SetCldAvailableCallback(base::Callback<void(void)>) = 0; | 65 // The default implementation immediately invokes the callback. |
| 66 virtual void SetCldAvailableCallback(base::Callback<void(void)> callback); |
| 67 |
| 68 // Sets the default data provider for this process, i.e. the provider to be |
| 69 // used unless the embedder calls Set(RendererCldDataProvider*). This is the |
| 70 // method that normal (i.e., non-test) Chromium code should use; embedders can |
| 71 // and should use the unconditional Set(RendererCldDataProvider*) method |
| 72 // instead. If a default provider has already been set, this method does |
| 73 // nothing. |
| 74 static void SetDefault(RendererCldDataProvider* instance); |
| 75 |
| 76 // Unconditionally sets the data provider for this process, overwriting any |
| 77 // previously-configured default. Normal Chromium code should never use this |
| 78 // method; it is provided for embedders to inject a provider from outside of |
| 79 // the Chromium code base. Test code can also use this method to force the |
| 80 // runtime to have a desired behavior. |
| 81 // |
| 82 // The caller is responsible for the lifecycle of the instance. In general, |
| 83 // the instance passed here should live throughout the lifetime of the |
| 84 // process. |
| 85 static void Set(RendererCldDataProvider* instance); |
| 86 |
| 87 // Returns true if and only if the current instance for this process is not |
| 88 // NULL. |
| 89 static bool IsInitialized(); |
| 90 |
| 91 // Returns the instance of the provider previously set by Set(...). |
| 92 // If no instance has been set, a default no-op provider will be returned. |
| 93 static RendererCldDataProvider* Get(); |
| 94 |
| 95 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(RendererCldDataProvider); |
63 }; | 97 }; |
64 | 98 |
65 // Static factory function defined by the implementation that produces a new | |
66 // provider for the specified render view host. | |
67 RendererCldDataProvider* CreateRendererCldDataProviderFor( | |
68 content::RenderViewObserver*); | |
69 | |
70 } // namespace translate | 99 } // namespace translate |
71 | 100 |
72 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATAP_PROVIDER_H_ | 101 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_RENDERER_CLD_DATAP_PROVIDER_H_ |
OLD | NEW |