- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / extensions / extension_installed_bubble.cc
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 #include "chrome/browser/ui/extensions/extension_installed_bubble.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/common/extensions/api/extension_action/action_info.h"
15 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
16 #include "chrome/common/extensions/extension.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h"
19
20 using content::Details;
21 using extensions::Extension;
22
23 namespace {
24
25 // How long to wait for browser action animations to complete before retrying.
26 const int kAnimationWaitMs = 50;
27 // How often we retry when waiting for browser action animation to end.
28 const int kAnimationWaitRetries = 10;
29
30 } // namespace
31
32 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate,
33                                                    const Extension* extension,
34                                                    Browser *browser,
35                                                    const SkBitmap& icon)
36     : delegate_(delegate),
37       extension_(extension),
38       browser_(browser),
39       icon_(icon),
40       animation_wait_retries_(0),
41       weak_factory_(this) {
42   if (!extensions::OmniboxInfo::GetKeyword(extension).empty())
43     type_ = OMNIBOX_KEYWORD;
44   else if (extensions::ActionInfo::GetBrowserActionInfo(extension))
45     type_ = BROWSER_ACTION;
46   else if (extensions::ActionInfo::GetPageActionInfo(extension) &&
47            extensions::ActionInfo::IsVerboseInstallMessage(extension))
48     type_ = PAGE_ACTION;
49   else
50     type_ = GENERIC;
51
52   // |extension| has been initialized but not loaded at this point. We need
53   // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
54   // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
55   // be sure that a BrowserAction or PageAction has had views created which we
56   // can inspect for the purpose of previewing of pointing to them.
57   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
58       content::Source<Profile>(browser->profile()));
59   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
60       content::Source<Profile>(browser->profile()));
61   registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING,
62       content::Source<Browser>(browser));
63 }
64
65 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
66
67 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
68   registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING,
69                     content::Source<Browser>(browser_));
70 }
71
72 void ExtensionInstalledBubble::ShowInternal() {
73   if (delegate_->MaybeShowNow())
74     return;
75   if (animation_wait_retries_++ < kAnimationWaitRetries) {
76     base::MessageLoopForUI::current()->PostDelayedTask(
77         FROM_HERE,
78         base::Bind(&ExtensionInstalledBubble::ShowInternal,
79                    weak_factory_.GetWeakPtr()),
80         base::TimeDelta::FromMilliseconds(kAnimationWaitMs));
81   }
82 }
83
84 void ExtensionInstalledBubble::Observe(
85     int type,
86     const content::NotificationSource& source,
87     const content::NotificationDetails& details) {
88   switch (type) {
89     case chrome::NOTIFICATION_EXTENSION_LOADED: {
90       const Extension* extension = Details<const Extension>(details).ptr();
91       if (extension == extension_) {
92         animation_wait_retries_ = 0;
93         // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
94         base::MessageLoopForUI::current()->PostTask(
95             FROM_HERE,
96             base::Bind(&ExtensionInstalledBubble::ShowInternal,
97                        weak_factory_.GetWeakPtr()));
98       }
99       break;
100     }
101     case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
102       const Extension* extension =
103           Details<extensions::UnloadedExtensionInfo>(details)->extension;
104       if (extension == extension_) {
105         // Extension is going away, make sure ShowInternal won't be called.
106         weak_factory_.InvalidateWeakPtrs();
107         extension_ = NULL;
108       }
109       break;
110     }
111     case chrome::NOTIFICATION_BROWSER_CLOSING:
112       delete delegate_;
113       break;
114
115     default:
116       NOTREACHED() << "Received unexpected notification";
117   }
118 }