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 CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "chrome/browser/extensions/location_bar_controller.h" | 16 #include "chrome/browser/extensions/location_bar_controller.h" |
17 #include "chrome/browser/extensions/script_executor.h" | 17 #include "chrome/browser/extensions/script_executor.h" |
18 #include "chrome/browser/extensions/script_executor_impl.h" | |
19 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
20 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
21 #include "content/public/browser/web_contents_observer.h" | 20 #include "content/public/browser/web_contents_observer.h" |
22 | 21 |
23 class ExtensionAction; | 22 class ExtensionAction; |
24 class ExtensionService; | 23 class ExtensionService; |
25 class TabContents; | 24 class TabContents; |
26 | 25 |
27 namespace IPC { | 26 namespace IPC { |
28 class Message; | 27 class Message; |
29 } | 28 } |
30 | 29 |
31 namespace extensions { | 30 namespace extensions { |
32 | 31 |
33 class Extension; | 32 class Extension; |
34 | 33 |
35 // A LocationBarController which displays icons whenever a script is executing | 34 // A LocationBarController which displays icons whenever a script is executing |
36 // in a tab. It accomplishes this two different ways: | 35 // in a tab. It accomplishes this two different ways: |
37 // | 36 // |
38 // - For content_script declarations, this receives IPCs from the renderer | 37 // - For content_script declarations, this receives IPCs from the renderer |
39 // notifying that a content script is running (either on this tab or one of | 38 // notifying that a content script is running (either on this tab or one of |
40 // its frames), which is recorded. | 39 // its frames), which is recorded. |
41 // - The ScriptExecutor interface is exposed for programmatically executing | 40 // - Observes a ScriptExecutor so that successfully-executed scripts |
42 // scripts. Successfully executed scripts are recorded. | 41 // can cause a script badge to appear. |
43 // | 42 // |
44 // When extension IDs are recorded a NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED | 43 // When extension IDs are recorded a NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED |
45 // is sent, and those extensions will be returned from GetCurrentActions until | 44 // is sent, and those extensions will be returned from GetCurrentActions until |
46 // the next page navigation. | 45 // the next page navigation. |
47 // | |
48 // Ref-counted so that it can be safely bound in a base::Bind. | |
49 class ScriptBadgeController | 46 class ScriptBadgeController |
50 : public base::RefCountedThreadSafe<ScriptBadgeController>, | 47 : public LocationBarController, |
51 public LocationBarController, | 48 public ScriptExecutor::Observer, |
52 public ScriptExecutor, | |
53 public content::WebContentsObserver, | 49 public content::WebContentsObserver, |
54 public content::NotificationObserver { | 50 public content::NotificationObserver { |
55 public: | 51 public: |
56 explicit ScriptBadgeController(TabContents* tab_contents); | 52 explicit ScriptBadgeController(TabContents* tab_contents, |
| 53 ScriptExecutor* script_executor); |
| 54 virtual ~ScriptBadgeController(); |
57 | 55 |
58 // LocationBarController implementation. | 56 // LocationBarController implementation. |
59 virtual std::vector<ExtensionAction*> GetCurrentActions() const OVERRIDE; | 57 virtual std::vector<ExtensionAction*> GetCurrentActions() const OVERRIDE; |
60 virtual Action OnClicked(const std::string& extension_id, | 58 virtual Action OnClicked(const std::string& extension_id, |
61 int mouse_button) OVERRIDE; | 59 int mouse_button) OVERRIDE; |
62 virtual void NotifyChange() OVERRIDE; | 60 virtual void NotifyChange() OVERRIDE; |
63 | 61 |
64 // ScriptExecutor implementation. | 62 // ScriptExecutor::Observer implementation. |
65 virtual void ExecuteScript(const std::string& extension_id, | 63 virtual void OnExecuteScriptFinished(const std::string& extension_id, |
66 ScriptType script_type, | 64 bool success, |
67 const std::string& code, | 65 int32 page_id, |
68 FrameScope frame_scope, | 66 const std::string& error) OVERRIDE; |
69 UserScript::RunLocation run_at, | |
70 WorldType world_type, | |
71 const ExecuteScriptCallback& callback) OVERRIDE; | |
72 | 67 |
73 private: | 68 private: |
74 friend class base::RefCountedThreadSafe<ScriptBadgeController>; | |
75 virtual ~ScriptBadgeController(); | |
76 | |
77 // Callback for ExecuteScript which if successful and for the current URL | |
78 // records that the script is running, then calls the original callback. | |
79 void OnExecuteScriptFinished(const std::string& extension_id, | |
80 const ExecuteScriptCallback& callback, | |
81 bool success, | |
82 int32 page_id, | |
83 const std::string& error); | |
84 | |
85 // Gets the ExtensionService for |tab_contents_|. | 69 // Gets the ExtensionService for |tab_contents_|. |
86 ExtensionService* GetExtensionService(); | 70 ExtensionService* GetExtensionService(); |
87 | 71 |
88 // Gets the current page ID. | 72 // Gets the current page ID. |
89 int32 GetPageID(); | 73 int32 GetPageID(); |
90 | 74 |
91 // content::WebContentsObserver implementation. | 75 // content::WebContentsObserver implementation. |
92 virtual void DidNavigateMainFrame( | 76 virtual void DidNavigateMainFrame( |
93 const content::LoadCommittedDetails& details, | 77 const content::LoadCommittedDetails& details, |
94 const content::FrameNavigateParams& params) OVERRIDE; | 78 const content::FrameNavigateParams& params) OVERRIDE; |
95 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 79 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
96 | 80 |
97 // content::NotificationObserver implementation. | 81 // content::NotificationObserver implementation. |
98 virtual void Observe(int type, | 82 virtual void Observe(int type, |
99 const content::NotificationSource& source, | 83 const content::NotificationSource& source, |
100 const content::NotificationDetails& details) OVERRIDE; | 84 const content::NotificationDetails& details) OVERRIDE; |
101 | 85 |
102 // IPC::Message handlers. | 86 // IPC::Message handlers. |
103 void OnContentScriptsExecuting(const std::set<std::string>& extension_ids, | 87 void OnContentScriptsExecuting(const std::set<std::string>& extension_ids, |
104 int32 page_id); | 88 int32 page_id); |
105 | 89 |
106 // Tries to insert an extension into the relevant collections, and returns | 90 // Tries to insert an extension into the relevant collections, and returns |
107 // whether any change was made. | 91 // whether any change was made. |
108 bool InsertExtension(const std::string& extension_id); | 92 bool InsertExtension(const std::string& extension_id); |
109 | 93 |
110 // Tries to erase an extension from the relevant collections, and returns | 94 // Tries to erase an extension from the relevant collections, and returns |
111 // whether any change was made. | 95 // whether any change was made. |
112 bool EraseExtension(const Extension* extension); | 96 bool EraseExtension(const Extension* extension); |
113 | 97 |
114 // Delegate ScriptExecutorImpl for running ExecuteScript. | |
115 ScriptExecutorImpl script_executor_; | |
116 | |
117 // Our parent TabContents. | 98 // Our parent TabContents. |
118 TabContents* tab_contents_; | 99 TabContents* tab_contents_; |
119 | 100 |
120 // The current extension actions in the order they appeared. | 101 // The current extension actions in the order they appeared. |
121 std::vector<ExtensionAction*> current_actions_; | 102 std::vector<ExtensionAction*> current_actions_; |
122 | 103 |
123 // The extensions that have called ExecuteScript on the current frame. | 104 // The extensions that have called ExecuteScript on the current frame. |
124 std::set<std::string> extensions_executing_scripts_; | 105 std::set<std::string> extensions_executing_scripts_; |
125 | 106 |
126 // Listen to extension unloaded notifications. | 107 // Listen to extension unloaded notifications. |
127 content::NotificationRegistrar registrar_; | 108 content::NotificationRegistrar registrar_; |
128 | 109 |
129 DISALLOW_COPY_AND_ASSIGN(ScriptBadgeController); | 110 DISALLOW_COPY_AND_ASSIGN(ScriptBadgeController); |
130 }; | 111 }; |
131 | 112 |
132 } // namespace extensions | 113 } // namespace extensions |
133 | 114 |
134 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ | 115 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_ |
OLD | NEW |