1 // Copyright 2015 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.
5 #include "extensions/browser/api/guest_view/extension_view/extension_view_internal_api.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/crx_file/id_util.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/storage_partition.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/stop_find_action.h"
15 #include "extensions/common/api/extension_view_internal.h"
16 #include "extensions/common/constants.h"
18 namespace extensionview
= extensions::api::extension_view_internal
;
20 namespace extensions
{
22 bool ExtensionViewInternalExtensionFunction::RunAsync() {
24 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(0, &instance_id
));
25 ExtensionViewGuest
* guest
= ExtensionViewGuest::From(
26 render_frame_host()->GetProcess()->GetID(), instance_id
);
29 return RunAsyncSafe(guest
);
32 // Checks the validity of |src|, including that it follows the chrome extension
33 // scheme and that its extension ID is valid.
34 // Returns true if |src| is valid.
35 bool IsSrcValid(GURL src
) {
36 // Check if src is valid and matches the extension scheme.
37 if (!src
.is_valid() || !src
.SchemeIs(kExtensionScheme
)) {
38 VLOG(0) << "src not valid or match extension scheme";
42 // Get the extension id and check if it is valid.
43 std::string extension_id
= src
.host();
44 if (!crx_file::id_util::IdIsValid(extension_id
)) {
45 VLOG(0) << "extension id not valid: " << extension_id
;
52 bool ExtensionViewInternalLoadSrcFunction::RunAsyncSafe(
53 ExtensionViewGuest
* guest
) {
54 scoped_ptr
<extensionview::LoadSrc::Params
> params(
55 extensionview::LoadSrc::Params::Create(*args_
));
56 EXTENSION_FUNCTION_VALIDATE(params
.get());
57 std::string src
= params
->src
;
59 bool has_load_succeeded
= false;
60 bool is_src_valid
= IsSrcValid(url
);
63 has_load_succeeded
= guest
->NavigateGuest(src
, true /* force_navigation */);
65 // Return whether load is successful.
66 SetResult(new base::FundamentalValue(has_load_succeeded
));
71 bool ExtensionViewInternalParseSrcFunction::RunAsync() {
72 scoped_ptr
<extensionview::ParseSrc::Params
> params(
73 extensionview::ParseSrc::Params::Create(*args_
));
74 EXTENSION_FUNCTION_VALIDATE(params
.get());
75 GURL
url(params
->src
);
76 bool is_src_valid
= IsSrcValid(url
);
78 // Return whether the src is valid and the current extension ID to
80 scoped_ptr
<base::ListValue
> result_list(new base::ListValue());
81 result_list
->AppendBoolean(is_src_valid
);
82 result_list
->AppendString(url
.host());
83 SetResultList(result_list
.Pass());
88 } // namespace extensions