Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / location_bar_controller.cc
1 // Copyright 2014 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/extensions/location_bar_controller.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/extensions/active_script_controller.h"
9 #include "chrome/browser/extensions/page_action_controller.h"
10 #include "chrome/common/extensions/api/extension_action/action_info.h"
11 #include "content/public/browser/invalidate_type.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/web_contents.h"
14 #include "extensions/browser/extension_registry.h"
15
16 namespace extensions {
17
18 LocationBarController::LocationBarController(
19     content::WebContents* web_contents)
20     : WebContentsObserver(web_contents),
21       web_contents_(web_contents),
22       active_script_controller_(new ActiveScriptController(web_contents_)),
23       page_action_controller_(new PageActionController(web_contents_)),
24       extension_registry_observer_(this) {
25   extension_registry_observer_.Add(
26       ExtensionRegistry::Get(web_contents_->GetBrowserContext()));
27 }
28
29 LocationBarController::~LocationBarController() {
30 }
31
32 std::vector<ExtensionAction*> LocationBarController::GetCurrentActions() {
33   const ExtensionSet& extensions =
34       ExtensionRegistry::Get(web_contents_->GetBrowserContext())
35           ->enabled_extensions();
36   std::vector<ExtensionAction*> current_actions;
37   for (ExtensionSet::const_iterator iter = extensions.begin();
38        iter != extensions.end();
39        ++iter) {
40     // Right now, we can consolidate these actions because we only want to show
41     // one action per extension. If clicking on an active script action ever
42     // has a response, then we will need to split the actions.
43     ExtensionAction* action =
44         page_action_controller_->GetActionForExtension(*iter);
45     if (!action)
46       action = active_script_controller_->GetActionForExtension(*iter);
47     if (action)
48       current_actions.push_back(action);
49   }
50
51   return current_actions;
52 }
53
54 ExtensionAction::ShowAction LocationBarController::OnClicked(
55     const ExtensionAction* action) {
56   const Extension* extension =
57       ExtensionRegistry::Get(web_contents_->GetBrowserContext())
58           ->enabled_extensions().GetByID(action->extension_id());
59   CHECK(extension) << action->extension_id();
60
61   ExtensionAction::ShowAction page_action =
62       page_action_controller_->GetActionForExtension(extension) ?
63           page_action_controller_->OnClicked(extension) :
64           ExtensionAction::ACTION_NONE;
65   ExtensionAction::ShowAction active_script_action =
66       active_script_controller_->GetActionForExtension(extension) ?
67           active_script_controller_->OnClicked(extension) :
68           ExtensionAction::ACTION_NONE;
69
70   // PageAction response takes priority.
71   return page_action != ExtensionAction::ACTION_NONE ? page_action :
72                                                        active_script_action;
73 }
74
75 // static
76 void LocationBarController::NotifyChange(content::WebContents* web_contents) {
77   web_contents->NotifyNavigationStateChanged(
78       content::INVALIDATE_TYPE_PAGE_ACTIONS);
79 }
80
81 void LocationBarController::DidNavigateMainFrame(
82     const content::LoadCommittedDetails& details,
83     const content::FrameNavigateParams& params) {
84   if (details.is_in_page)
85     return;
86
87   page_action_controller_->OnNavigated();
88   active_script_controller_->OnNavigated();
89 }
90
91 void LocationBarController::OnExtensionUnloaded(
92     content::BrowserContext* browser_context,
93     const Extension* extension,
94     UnloadedExtensionInfo::Reason reason) {
95   bool should_update = false;
96   if (page_action_controller_->GetActionForExtension(extension)) {
97     page_action_controller_->OnExtensionUnloaded(extension);
98     should_update = true;
99   }
100   if (active_script_controller_->GetActionForExtension(extension)) {
101     active_script_controller_->OnExtensionUnloaded(extension);
102     should_update = true;
103   }
104
105   if (should_update)
106     NotifyChange(web_contents());
107 }
108
109 }  // namespace extensions