Home | History | Annotate | Download | only in extensions
      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_LOCATION_BAR_CONTROLLER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_LOCATION_BAR_CONTROLLER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/macros.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/scoped_observer.h"
     13 #include "content/public/browser/web_contents_observer.h"
     14 #include "extensions/browser/extension_registry_observer.h"
     15 
     16 namespace content {
     17 class WebContents;
     18 }
     19 
     20 class ExtensionAction;
     21 
     22 namespace extensions {
     23 
     24 class ActiveScriptController;
     25 class Extension;
     26 class ExtensionRegistry;
     27 class PageActionController;
     28 
     29 // Interface for a class that controls the the extension icons that show up in
     30 // the location bar. Depending on switches, these icons can have differing
     31 // behavior.
     32 class LocationBarController : public content::WebContentsObserver,
     33                               public ExtensionRegistryObserver {
     34  public:
     35   // The action that the UI should take after executing |OnClicked|.
     36   enum Action {
     37     ACTION_NONE,
     38     ACTION_SHOW_POPUP,
     39     ACTION_SHOW_CONTEXT_MENU,
     40   };
     41 
     42   class ActionProvider {
     43    public:
     44     // Returns the action for the given extension, or NULL if there isn't one.
     45     virtual ExtensionAction* GetActionForExtension(
     46         const Extension* extension) = 0;
     47 
     48     // Handles a click on an extension action.
     49     virtual LocationBarController::Action OnClicked(
     50         const Extension* extension) = 0;
     51 
     52     // A notification that the WebContents has navigated in the main frame (and
     53     // not in page), so any state relating to the current page should likely be
     54     // reset.
     55     virtual void OnNavigated() = 0;
     56 
     57     // A notification that the given |extension| has been unloaded, and any
     58     // actions associated with it should be removed.
     59     // The location bar controller will update itself after this if needed, so
     60     // Providers should not call NotifyChange().
     61     virtual void OnExtensionUnloaded(const Extension* extension) {}
     62   };
     63 
     64   explicit LocationBarController(content::WebContents* web_contents);
     65   virtual ~LocationBarController();
     66 
     67   // Returns the actions which should be displayed in the location bar.
     68   std::vector<ExtensionAction*> GetCurrentActions();
     69 
     70   // Notifies this that an ExtensionAction has been clicked, and returns the
     71   // action which should be taken in response (if any).
     72   Action OnClicked(const ExtensionAction* action);
     73 
     74   // Notifies the window that the actions have changed.
     75   static void NotifyChange(content::WebContents* web_contents);
     76 
     77   ActiveScriptController* active_script_controller() {
     78     return active_script_controller_.get();
     79   }
     80 
     81  private:
     82   // content::WebContentsObserver implementation.
     83   virtual void DidNavigateMainFrame(
     84       const content::LoadCommittedDetails& details,
     85       const content::FrameNavigateParams& params) OVERRIDE;
     86 
     87   // ExtensionRegistryObserver implementation.
     88   virtual void OnExtensionUnloaded(
     89       content::BrowserContext* browser_context,
     90       const Extension* extension,
     91       UnloadedExtensionInfo::Reason reason) OVERRIDE;
     92 
     93   // The associated WebContents.
     94   content::WebContents* web_contents_;
     95 
     96   // The controllers for different sources of actions in the location bar.
     97   // Currently, this is only page actions and active script actions, so we
     98   // explicitly own and create both. If there are ever more, it will be worth
     99   // considering making this class own a list of LocationBarControllerProviders
    100   // instead.
    101   scoped_ptr<ActiveScriptController> active_script_controller_;
    102   scoped_ptr<PageActionController> page_action_controller_;
    103 
    104   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
    105       extension_registry_observer_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(LocationBarController);
    108 };
    109 
    110 }  // namespace extensions
    111 
    112 #endif  // CHROME_BROWSER_EXTENSIONS_LOCATION_BAR_CONTROLLER_H_
    113