1// Copyright 2013 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_UI_EXTENSIONS_EXTENSION_INSTALLED_BUBBLE_H_
6#define CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_INSTALLED_BUBBLE_H_
7
8#include "base/memory/weak_ptr.h"
9#include "base/scoped_observer.h"
10#include "content/public/browser/notification_observer.h"
11#include "content/public/browser/notification_registrar.h"
12#include "extensions/browser/extension_registry_observer.h"
13#include "third_party/skia/include/core/SkBitmap.h"
14
15class Browser;
16
17namespace extensions {
18class Extension;
19class ExtensionRegistry;
20}
21
22// Provides feedback to the user upon successful installation of an
23// extension. Depending on the type of extension, the Bubble will
24// point to:
25//    OMNIBOX_KEYWORD-> The omnibox.
26//    BROWSER_ACTION -> The browser action icon in the toolbar.
27//    PAGE_ACTION    -> A preview of the page action icon in the location
28//                      bar which is shown while the Bubble is shown.
29//    GENERIC        -> The wrench menu. This case includes page actions that
30//                      don't specify a default icon.
31//
32// ExtensionInstallBubble manages its own lifetime.
33class ExtensionInstalledBubble : public content::NotificationObserver,
34                                 public extensions::ExtensionRegistryObserver {
35 public:
36  // The behavior and content of this Bubble comes in these varieties:
37  enum BubbleType {
38    OMNIBOX_KEYWORD,
39    BROWSER_ACTION,
40    PAGE_ACTION,
41    GENERIC
42  };
43
44  // Implements the UI for showing the bubble. Owns us.
45  class Delegate {
46   public:
47    virtual ~Delegate() {}
48
49    // Attempts to show the bubble. Called from ShowInternal. Returns false
50    // if, because of animating (such as from adding a new browser action
51    // to the toolbar), the bubble could not be shown immediately.
52    virtual bool MaybeShowNow() = 0;
53  };
54
55  ExtensionInstalledBubble(Delegate* delegate,
56                           const extensions::Extension* extension,
57                           Browser *browser,
58                           const SkBitmap& icon);
59
60  virtual ~ExtensionInstalledBubble();
61
62  const extensions::Extension* extension() const { return extension_; }
63  Browser* browser() { return browser_; }
64  const Browser* browser() const { return browser_; }
65  const SkBitmap& icon() const { return icon_; }
66  BubbleType type() const { return type_; }
67
68  // Stop listening to NOTIFICATION_BROWSER_CLOSING.
69  void IgnoreBrowserClosing();
70
71 private:
72  // Delegates showing the view to our |view_|. Called internally via PostTask.
73  void ShowInternal();
74
75  // content::NotificationObserver:
76  virtual void Observe(int type,
77                       const content::NotificationSource& source,
78                       const content::NotificationDetails& details) OVERRIDE;
79
80  // extensions::ExtensionRegistryObserver:
81  virtual void OnExtensionLoaded(
82      content::BrowserContext* browser_context,
83      const extensions::Extension* extension) OVERRIDE;
84  virtual void OnExtensionUnloaded(
85      content::BrowserContext* browser_context,
86      const extensions::Extension* extension,
87      extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
88
89  // The view delegate that shows the bubble. Owns us.
90  Delegate* delegate_;
91
92  // |extension_| is NULL when we are deleted.
93  const extensions::Extension* extension_;
94  Browser* browser_;
95  const SkBitmap icon_;
96  BubbleType type_;
97  content::NotificationRegistrar registrar_;
98
99  // Listen to extension load, unloaded notifications.
100  ScopedObserver<extensions::ExtensionRegistry,
101                 extensions::ExtensionRegistryObserver>
102      extension_registry_observer_;
103
104  // The number of times to retry showing the bubble if the browser action
105  // toolbar is animating.
106  int animation_wait_retries_;
107
108  base::WeakPtrFactory<ExtensionInstalledBubble> weak_factory_;
109
110  DISALLOW_COPY_AND_ASSIGN(ExtensionInstalledBubble);
111};
112
113#endif  // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_INSTALLED_BUBBLE_H_
114