• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_
7 
8 #include <map>
9 #include <set>
10 #include <string>
11 
12 #include "base/compiler_specific.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "chrome/browser/extensions/location_bar_controller.h"
16 #include "chrome/browser/extensions/tab_helper.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_contents_observer.h"
20 
21 class ExtensionAction;
22 class ExtensionService;
23 class GURL;
24 
25 namespace base {
26 class ListValue;
27 }  // namespace base
28 
29 namespace IPC {
30 class Message;
31 }
32 
33 namespace extensions {
34 
35 class Extension;
36 
37 // A LocationBarController which displays icons whenever a script is executing
38 // in a tab.
39 //
40 // When extension IDs are recorded a NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED
41 // is sent, and those extensions will be returned from GetCurrentActions until
42 // the next page navigation.
43 class ScriptBadgeController
44     : public LocationBarController,
45       public TabHelper::ScriptExecutionObserver,
46       public content::WebContentsObserver,
47       public content::NotificationObserver {
48  public:
49   explicit ScriptBadgeController(content::WebContents* web_contents,
50                                  TabHelper* tab_helper);
51   virtual ~ScriptBadgeController();
52 
53   // LocationBarController implementation.
54   virtual std::vector<ExtensionAction*> GetCurrentActions() const OVERRIDE;
55   virtual void GetAttentionFor(const std::string& extension_id) OVERRIDE;
56   virtual Action OnClicked(const std::string& extension_id,
57                            int mouse_button) OVERRIDE;
58   virtual void NotifyChange() OVERRIDE;
59 
60   // TabHelper::ScriptExecutionObserver implementation.
61   virtual void OnScriptsExecuted(
62       const content::WebContents* web_contents,
63       const ExecutingScriptsMap& extension_ids,
64       int32 on_page_id,
65       const GURL& on_url) OVERRIDE;
66 
67  private:
68   // Gets the Profile for |web_contents_|.
69   Profile* profile() const;
70 
71   // Gets the ExtensionService for |web_contents_|.
72   ExtensionService* GetExtensionService() const;
73 
74   // Gets the current page ID, or -1 if no navigation entry has been committed.
75   int32 GetPageID();
76 
77   // content::WebContentsObserver implementation.
78   virtual void DidNavigateMainFrame(
79       const content::LoadCommittedDetails& details,
80       const content::FrameNavigateParams& params) OVERRIDE;
81 
82   // content::NotificationObserver implementation.
83   virtual void Observe(int type,
84                        const content::NotificationSource& source,
85                        const content::NotificationDetails& details) OVERRIDE;
86 
87   // Adds the extension's icon to the list of script badges.  Returns
88   // the script badge ExtensionAction that was added, or NULL if
89   // extension_id isn't valid.
90   ExtensionAction* AddExtensionToCurrentActions(
91       const std::string& extension_id);
92 
93   // Called when an extension is running script on the current tab,
94   // and tries to insert an extension into the relevant collections.
95   // Returns true if any change was made.
96   bool MarkExtensionExecuting(const std::string& extension_id);
97 
98   // The current extension actions.  These come from calls to ExecuteScript or
99   // getAttention on the current frame.
100   std::set<std::string> extensions_in_current_actions_;
101 
102   // Listen to extension unloaded notifications.
103   content::NotificationRegistrar registrar_;
104 
105   DISALLOW_COPY_AND_ASSIGN(ScriptBadgeController);
106 };
107 
108 }  // namespace extensions
109 
110 #endif  // CHROME_BROWSER_EXTENSIONS_SCRIPT_BADGE_CONTROLLER_H_
111