Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / extensions / extension_enable_flow.cc
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 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
12 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
17
18 using extensions::Extension;
19
20 ExtensionEnableFlow::ExtensionEnableFlow(Profile* profile,
21                                          const std::string& extension_id,
22                                          ExtensionEnableFlowDelegate* delegate)
23     : profile_(profile),
24       extension_id_(extension_id),
25       delegate_(delegate),
26       parent_contents_(NULL),
27       parent_window_(NULL) {
28 }
29
30 ExtensionEnableFlow::~ExtensionEnableFlow() {
31 }
32
33 void ExtensionEnableFlow::StartForWebContents(
34     content::WebContents* parent_contents) {
35   parent_contents_ = parent_contents;
36   parent_window_ = NULL;
37   Run();
38 }
39
40 void ExtensionEnableFlow::StartForNativeWindow(
41     gfx::NativeWindow parent_window) {
42   parent_contents_ = NULL;
43   parent_window_ = parent_window;
44   Run();
45 }
46
47 void ExtensionEnableFlow::StartForCurrentlyNonexistentWindow(
48     base::Callback<gfx::NativeWindow(void)> window_getter) {
49   window_getter_ = window_getter;
50   Run();
51 }
52
53 void ExtensionEnableFlow::Run() {
54   ExtensionService* service =
55       extensions::ExtensionSystem::Get(profile_)->extension_service();
56   const Extension* extension = service->GetExtensionById(extension_id_, true);
57   if (!extension) {
58     extension = extensions::ExtensionRegistry::Get(profile_)->GetExtensionById(
59         extension_id_, extensions::ExtensionRegistry::TERMINATED);
60     // It's possible (though unlikely) the app could have been uninstalled since
61     // the user clicked on it.
62     if (!extension)
63       return;
64     // If the app was terminated, reload it first.
65     service->ReloadExtension(extension_id_);
66
67     // ReloadExtension reallocates the Extension object.
68     extension = service->GetExtensionById(extension_id_, true);
69
70     // |extension| could be NULL for asynchronous load, such as the case of
71     // an unpacked extension. Wait for the load to continue the flow.
72     if (!extension) {
73       StartObserving();
74       return;
75     }
76   }
77
78   CheckPermissionAndMaybePromptUser();
79 }
80
81 void ExtensionEnableFlow::CheckPermissionAndMaybePromptUser() {
82   ExtensionService* service =
83       extensions::ExtensionSystem::Get(profile_)->extension_service();
84   const Extension* extension = service->GetExtensionById(extension_id_, true);
85   if (!extension) {
86     delegate_->ExtensionEnableFlowAborted(false);  // |delegate_| may delete us.
87     return;
88   }
89
90   extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
91   if (!prefs->DidExtensionEscalatePermissions(extension_id_)) {
92     // Enable the extension immediately if its privileges weren't escalated.
93     // This is a no-op if the extension was previously terminated.
94     service->EnableExtension(extension_id_);
95
96     delegate_->ExtensionEnableFlowFinished();  // |delegate_| may delete us.
97     return;
98   }
99
100   CreatePrompt();
101   prompt_->ConfirmReEnable(this, extension);
102 }
103
104 void ExtensionEnableFlow::CreatePrompt() {
105   if (!window_getter_.is_null())
106     parent_window_ = window_getter_.Run();
107   prompt_.reset(parent_contents_ ?
108       new ExtensionInstallPrompt(parent_contents_) :
109       new ExtensionInstallPrompt(profile_, parent_window_, this));
110 }
111
112 void ExtensionEnableFlow::StartObserving() {
113   registrar_.Add(this,
114                  chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
115                  content::Source<Profile>(profile_));
116   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
117                  content::Source<Profile>(profile_));
118   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
119                  content::Source<Profile>(profile_));
120 }
121
122 void ExtensionEnableFlow::StopObserving() {
123   registrar_.RemoveAll();
124 }
125
126 void ExtensionEnableFlow::Observe(int type,
127                                   const content::NotificationSource& source,
128                                   const content::NotificationDetails& details) {
129   switch (type) {
130     case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
131       const Extension* extension =
132           content::Details<const Extension>(details).ptr();
133       if (extension->id() == extension_id_) {
134         StopObserving();
135         CheckPermissionAndMaybePromptUser();
136       }
137
138       break;
139     }
140     case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR: {
141       StopObserving();
142       delegate_->ExtensionEnableFlowAborted(false);
143       break;
144     }
145     case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
146       const Extension* extension =
147           content::Details<const Extension>(details).ptr();
148       if (extension->id() == extension_id_) {
149         StopObserving();
150         delegate_->ExtensionEnableFlowAborted(false);
151       }
152
153       break;
154     }
155     default:
156       NOTREACHED();
157   }
158 }
159
160 void ExtensionEnableFlow::InstallUIProceed() {
161   ExtensionService* service =
162       extensions::ExtensionSystem::Get(profile_)->extension_service();
163
164   // The extension can be uninstalled in another window while the UI was
165   // showing. Treat it as a cancellation and notify |delegate_|.
166   const Extension* extension = service->GetExtensionById(extension_id_, true);
167   if (!extension) {
168     delegate_->ExtensionEnableFlowAborted(true);
169     return;
170   }
171
172   service->GrantPermissionsAndEnableExtension(extension);
173   delegate_->ExtensionEnableFlowFinished();  // |delegate_| may delete us.
174 }
175
176 void ExtensionEnableFlow::InstallUIAbort(bool user_initiated) {
177   delegate_->ExtensionEnableFlowAborted(user_initiated);
178   // |delegate_| may delete us.
179 }
180
181 content::WebContents* ExtensionEnableFlow::OpenURL(
182     const content::OpenURLParams& params) {
183   chrome::ScopedTabbedBrowserDisplayer displayer(
184       profile_, chrome::GetActiveDesktop());
185   return displayer.browser()->OpenURL(params);
186 }