Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / guest_view / extension_options / extension_options_guest.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 "extensions/browser/guest_view/extension_options/extension_options_guest.h"
6
7 #include "base/values.h"
8 #include "components/crx_file/id_util.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/browser/web_contents.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/extension_web_contents_observer.h"
16 #include "extensions/browser/guest_view/extension_options/extension_options_constants.h"
17 #include "extensions/browser/guest_view/extension_options/extension_options_guest_delegate.h"
18 #include "extensions/browser/guest_view/guest_view_manager.h"
19 #include "extensions/common/api/extension_options_internal.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_messages.h"
23 #include "extensions/common/manifest_handlers/options_page_info.h"
24 #include "extensions/common/permissions/permissions_data.h"
25 #include "extensions/strings/grit/extensions_strings.h"
26 #include "ipc/ipc_message_macros.h"
27
28 using content::WebContents;
29 using namespace extensions::core_api;
30
31 namespace extensions {
32
33 // static
34 const char ExtensionOptionsGuest::Type[] = "extensionoptions";
35
36 ExtensionOptionsGuest::ExtensionOptionsGuest(
37     content::BrowserContext* browser_context,
38     int guest_instance_id)
39     : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id),
40       extension_options_guest_delegate_(
41           extensions::ExtensionsAPIClient::Get()
42               ->CreateExtensionOptionsGuestDelegate(this)),
43       has_navigated_(false) {
44 }
45
46 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
47 }
48
49 // static
50 extensions::GuestViewBase* ExtensionOptionsGuest::Create(
51     content::BrowserContext* browser_context,
52     int guest_instance_id) {
53   return new ExtensionOptionsGuest(browser_context, guest_instance_id);
54 }
55
56 void ExtensionOptionsGuest::CreateWebContents(
57     const std::string& embedder_extension_id,
58     int embedder_render_process_id,
59     const GURL& embedder_site_url,
60     const base::DictionaryValue& create_params,
61     const WebContentsCreatedCallback& callback) {
62   // Get the extension's base URL.
63   std::string extension_id;
64   create_params.GetString(extensionoptions::kExtensionId, &extension_id);
65
66   if (!crx_file::id_util::IdIsValid(extension_id)) {
67     callback.Run(NULL);
68     return;
69   }
70
71   if (crx_file::id_util::IdIsValid(embedder_extension_id) &&
72       extension_id != embedder_extension_id) {
73     // Extensions cannot embed other extensions' options pages.
74     callback.Run(NULL);
75     return;
76   }
77
78   GURL extension_url =
79       extensions::Extension::GetBaseURLFromExtensionId(extension_id);
80   if (!extension_url.is_valid()) {
81     callback.Run(NULL);
82     return;
83   }
84
85   // Get the options page URL for later use.
86   extensions::ExtensionRegistry* registry =
87       extensions::ExtensionRegistry::Get(browser_context());
88   const extensions::Extension* extension =
89       registry->enabled_extensions().GetByID(extension_id);
90   if (!extension) {
91     // The ID was valid but the extension didn't exist. Typically this will
92     // happen when an extension is disabled.
93     callback.Run(NULL);
94     return;
95   }
96
97   options_page_ = extensions::OptionsPageInfo::GetOptionsPage(extension);
98   if (!options_page_.is_valid()) {
99     callback.Run(NULL);
100     return;
101   }
102
103   // Create a WebContents using the extension URL. The options page's
104   // WebContents should live in the same process as its parent extension's
105   // WebContents, so we can use |extension_url| for creating the SiteInstance.
106   content::SiteInstance* options_site_instance =
107       content::SiteInstance::CreateForURL(browser_context(), extension_url);
108   WebContents::CreateParams params(browser_context(), options_site_instance);
109   params.guest_delegate = this;
110   callback.Run(WebContents::Create(params));
111 }
112
113 void ExtensionOptionsGuest::DidAttachToEmbedder() {
114   SetUpAutoSize();
115
116   // We should not re-navigate on reattachment.
117   if (has_navigated_)
118     return;
119
120   web_contents()->GetController().LoadURL(options_page_,
121                                           content::Referrer(),
122                                           ui::PAGE_TRANSITION_LINK,
123                                           std::string());
124   has_navigated_ = true;
125 }
126
127 void ExtensionOptionsGuest::DidInitialize() {
128   extension_function_dispatcher_.reset(
129       new extensions::ExtensionFunctionDispatcher(browser_context(), this));
130   if (extension_options_guest_delegate_) {
131     extension_options_guest_delegate_->DidInitialize();
132   }
133 }
134
135 void ExtensionOptionsGuest::DidStopLoading() {
136   scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
137   DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
138       extension_options_internal::OnLoad::kEventName, args.Pass()));
139 }
140
141 const char* ExtensionOptionsGuest::GetAPINamespace() const {
142   return extensionoptions::kAPINamespace;
143 }
144
145 int ExtensionOptionsGuest::GetTaskPrefix() const {
146   return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX;
147 }
148
149 void ExtensionOptionsGuest::GuestSizeChangedDueToAutoSize(
150     const gfx::Size& old_size,
151     const gfx::Size& new_size) {
152   scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
153   args->SetInteger(extensionoptions::kNewWidth, new_size.width());
154   args->SetInteger(extensionoptions::kNewHeight, new_size.height());
155   args->SetInteger(extensionoptions::kOldWidth, old_size.width());
156   args->SetInteger(extensionoptions::kOldHeight, old_size.height());
157   DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
158       extension_options_internal::OnSizeChanged::kEventName, args.Pass()));
159 }
160
161 bool ExtensionOptionsGuest::IsAutoSizeSupported() const {
162   return true;
163 }
164
165 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const {
166   return web_contents();
167 }
168
169 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab(
170     content::WebContents* source,
171     const content::OpenURLParams& params) {
172   if (!extension_options_guest_delegate_)
173     return NULL;
174
175   // Don't allow external URLs with the CURRENT_TAB disposition be opened in
176   // this guest view, change the disposition to NEW_FOREGROUND_TAB.
177   if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
178        params.url.host() != options_page_.host()) &&
179       params.disposition == CURRENT_TAB) {
180     return extension_options_guest_delegate_->OpenURLInNewTab(
181         content::OpenURLParams(params.url,
182                                params.referrer,
183                                params.frame_tree_node_id,
184                                NEW_FOREGROUND_TAB,
185                                params.transition,
186                                params.is_renderer_initiated));
187   }
188   return extension_options_guest_delegate_->OpenURLInNewTab(params);
189 }
190
191 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) {
192   DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
193       extension_options_internal::OnClose::kEventName,
194       make_scoped_ptr(new base::DictionaryValue())));
195 }
196
197 bool ExtensionOptionsGuest::HandleContextMenu(
198     const content::ContextMenuParams& params) {
199   if (!extension_options_guest_delegate_)
200     return false;
201
202   return extension_options_guest_delegate_->HandleContextMenu(params);
203 }
204
205 bool ExtensionOptionsGuest::ShouldCreateWebContents(
206     content::WebContents* web_contents,
207     int route_id,
208     WindowContainerType window_container_type,
209     const base::string16& frame_name,
210     const GURL& target_url,
211     const std::string& partition_id,
212     content::SessionStorageNamespace* session_storage_namespace) {
213   // This method handles opening links from within the guest. Since this guest
214   // view is used for displaying embedded extension options, we want any
215   // external links to be opened in a new tab, not in a new guest view.
216   // Therefore we just open the URL in a new tab, and since we aren't handling
217   // the new web contents, we return false.
218   // TODO(ericzeng): Open the tab in the background if the click was a
219   //   ctrl-click or middle mouse button click
220   if (extension_options_guest_delegate_) {
221     extension_options_guest_delegate_->OpenURLInNewTab(
222         content::OpenURLParams(target_url,
223                                content::Referrer(),
224                                NEW_FOREGROUND_TAB,
225                                ui::PAGE_TRANSITION_LINK,
226                                false));
227   }
228   return false;
229 }
230
231 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) {
232   bool handled = true;
233   IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message)
234     IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
235     IPC_MESSAGE_UNHANDLED(handled = false)
236   IPC_END_MESSAGE_MAP()
237   return handled;
238 }
239
240 void ExtensionOptionsGuest::OnRequest(
241     const ExtensionHostMsg_Request_Params& params) {
242   extension_function_dispatcher_->Dispatch(params,
243                                            web_contents()->GetRenderViewHost());
244 }
245
246 void ExtensionOptionsGuest::SetUpAutoSize() {
247   // Read the autosize parameters passed in from the embedder.
248   bool auto_size_enabled = false;
249   attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize,
250                               &auto_size_enabled);
251
252   int max_height = 0;
253   int max_width = 0;
254   attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight,
255                               &max_height);
256   attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width);
257
258   int min_height = 0;
259   int min_width = 0;
260   attach_params()->GetInteger(extensionoptions::kAttributeMinHeight,
261                               &min_height);
262   attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width);
263
264   // Call SetAutoSize to apply all the appropriate validation and clipping of
265   // values.
266   SetAutoSize(auto_size_enabled,
267               gfx::Size(min_width, min_height),
268               gfx::Size(max_width, max_height));
269 }
270
271 }  // namespace extensions