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 "components/dom_distiller/content/browser/distiller_javascript_utils.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/common/isolated_world_ids.h"
14 namespace dom_distiller
{
17 // An invalid world ID to check against.
18 const int invalid_world_id
= -1;
19 // The ID of the world javascript should execute in; init to invalid ID.
20 int distiller_javascript_world_id
= invalid_world_id
;
23 void SetDistillerJavaScriptWorldId(const int id
) {
24 // Never allow running in main world (0).
25 DCHECK(id
> content::ISOLATED_WORLD_ID_GLOBAL
);
26 // Only allow ID to be set once.
27 DCHECK(distiller_javascript_world_id
== invalid_world_id
);
28 distiller_javascript_world_id
= id
;
31 bool DistillerJavaScriptWorldIdIsSet() {
32 return distiller_javascript_world_id
!= invalid_world_id
;
35 void RunIsolatedJavaScript(
36 content::RenderFrameHost
* render_frame_host
,
37 const std::string
& buffer
,
38 const content::RenderFrameHost::JavaScriptResultCallback
& callback
) {
39 // Make sure world ID was set.
40 DCHECK(distiller_javascript_world_id
!= invalid_world_id
);
41 render_frame_host
->ExecuteJavaScriptInIsolatedWorld(
42 base::UTF8ToUTF16(buffer
), callback
,
43 distiller_javascript_world_id
);
46 void RunIsolatedJavaScript(content::RenderFrameHost
* render_frame_host
,
47 const std::string
& buffer
) {
48 RunIsolatedJavaScript(render_frame_host
, buffer
,
49 content::RenderFrameHost::JavaScriptResultCallback());
52 } // namespace dom_distiller