| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "ui/gfx/native_widget_types.h" | 14 #include "ui/gfx/native_widget_types.h" |
| 15 | 15 |
| 16 class Browser; | 16 class Browser; |
| 17 class Extension; | 17 class Extension; |
| 18 class ExtensionFunction; | 18 class ExtensionFunction; |
| 19 class ListValue; | 19 class ListValue; |
| 20 class Profile; | 20 class Profile; |
| 21 class RenderViewHost; | 21 class RenderViewHost; |
| 22 class TabContents; | 22 class TabContents; |
| 23 struct ExtensionHostMsg_Request_Params; | 23 struct ExtensionHostMsg_Request_Params; |
| 24 | 24 |
| 25 // A factory function for creating new ExtensionFunction instances. | 25 // A factory function for creating new ExtensionFunction instances. |
| 26 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); | 26 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); |
| 27 | 27 |
| 28 // ExtensionFunctionDispatcher receives requests to execute functions from | 28 // ExtensionFunctionDispatcher receives requests to execute functions from |
| 29 // Chrome extensions running in a RenderViewHost and dispatches them to the | 29 // Chrome extensions running in a RenderViewHost and dispatches them to the |
| 30 // appropriate handler. It lives entirely on the UI thread. | 30 // appropriate handler. It lives entirely on the UI thread. |
| 31 // | 31 // |
| 32 // ExtensionFunctionDispatcher should be a member of some class that hosts | 32 // ExtensionFunctionDispatcher should be a member of some class that hosts |
| 33 // RenderViewHosts and wants them to be able to display extension content. | 33 // RenderViewHosts and wants them to be able to display extension content. |
| 34 // This class should also implement ExtensionFunctionDispatcher::Delegate. | 34 // This class should also implement ExtensionFunctionDispatcher::Delegate. |
| 35 // | 35 // |
| 36 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a | 36 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a |
| 37 // single RVH, a single extension, or a single URL. This is by design so that | 37 // single RVH, a single extension, or a single URL. This is by design so that |
| 38 // we can gracefully handle cases like TabContents, where the RVH, extension, | 38 // we can gracefully handle cases like TabContents, where the RVH, extension, |
| 39 // and URL can all change over the lifetime of the tab. Instead, these items | 39 // and URL can all change over the lifetime of the tab. Instead, these items |
| 40 // are all passed into each request. | 40 // are all passed into each request. |
| 41 class ExtensionFunctionDispatcher { | 41 class ExtensionFunctionDispatcher |
| 42 : public base::SupportsWeakPtr<ExtensionFunctionDispatcher> { |
| 42 public: | 43 public: |
| 43 class Delegate { | 44 class Delegate { |
| 44 public: | 45 public: |
| 45 // Returns the browser that this delegate is associated with, if any. | 46 // Returns the browser that this delegate is associated with, if any. |
| 46 // Returns NULL otherwise. | 47 // Returns NULL otherwise. |
| 47 virtual Browser* GetBrowser() = 0; | 48 virtual Browser* GetBrowser() = 0; |
| 48 | 49 |
| 49 // Returns the native view for this extension view, if any. This may be NULL | 50 // Returns the native view for this extension view, if any. This may be NULL |
| 50 // if the view is not visible. | 51 // if the view is not visible. |
| 51 virtual gfx::NativeView GetNativeViewOfHost() = 0; | 52 virtual gfx::NativeView GetNativeViewOfHost() = 0; |
| 52 | 53 |
| 53 // Asks the delegate for any relevant TabContents associated with this | 54 // Asks the delegate for any relevant TabContents associated with this |
| 54 // context. For example, the TabContents in which an infobar or | 55 // context. For example, the TabContents in which an infobar or |
| 55 // chrome-extension://<id> URL are being shown. Callers must check for a | 56 // chrome-extension://<id> URL are being shown. Callers must check for a |
| 56 // NULL return value (as in the case of a background page). | 57 // NULL return value (as in the case of a background page). |
| 57 virtual TabContents* GetAssociatedTabContents() const = 0; | 58 virtual TabContents* GetAssociatedTabContents() const = 0; |
| 58 | 59 |
| 59 protected: | 60 protected: |
| 60 virtual ~Delegate() {} | 61 virtual ~Delegate() {} |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 // The peer object allows us to notify ExtensionFunctions when we are | |
| 64 // destroyed. | |
| 65 // TODO: this should use WeakPtr | |
| 66 struct Peer : public base::RefCounted<Peer> { | |
| 67 explicit Peer(ExtensionFunctionDispatcher* dispatcher) | |
| 68 : dispatcher_(dispatcher) {} | |
| 69 ExtensionFunctionDispatcher* dispatcher_; | |
| 70 | |
| 71 private: | |
| 72 friend class base::RefCounted<Peer>; | |
| 73 | |
| 74 ~Peer() {} | |
| 75 }; | |
| 76 | |
| 77 // Gets a list of all known extension function names. | 64 // Gets a list of all known extension function names. |
| 78 static void GetAllFunctionNames(std::vector<std::string>* names); | 65 static void GetAllFunctionNames(std::vector<std::string>* names); |
| 79 | 66 |
| 80 // Override a previously registered function. Returns true if successful, | 67 // Override a previously registered function. Returns true if successful, |
| 81 // false if no such function was registered. | 68 // false if no such function was registered. |
| 82 static bool OverrideFunction(const std::string& name, | 69 static bool OverrideFunction(const std::string& name, |
| 83 ExtensionFunctionFactory factory); | 70 ExtensionFunctionFactory factory); |
| 84 | 71 |
| 85 // Resets all functions to their initial implementation. | 72 // Resets all functions to their initial implementation. |
| 86 static void ResetFunctions(); | 73 static void ResetFunctions(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 111 // The profile that this dispatcher is associated with. | 98 // The profile that this dispatcher is associated with. |
| 112 Profile* profile() { return profile_; } | 99 Profile* profile() { return profile_; } |
| 113 | 100 |
| 114 private: | 101 private: |
| 115 // Helper to send an access denied error to the requesting render view. | 102 // Helper to send an access denied error to the requesting render view. |
| 116 void SendAccessDenied(RenderViewHost* render_view_host, int request_id); | 103 void SendAccessDenied(RenderViewHost* render_view_host, int request_id); |
| 117 | 104 |
| 118 Profile* profile_; | 105 Profile* profile_; |
| 119 | 106 |
| 120 Delegate* delegate_; | 107 Delegate* delegate_; |
| 121 | |
| 122 scoped_refptr<Peer> peer_; | |
| 123 }; | 108 }; |
| 124 | 109 |
| 125 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ | 110 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ |
| OLD | NEW |