Add ICU message format support
[chromium-blink-merge.git] / content / renderer / web_ui_mojo.cc
blobaab0af22ce4273dd88ec1b594d74a6aca3d6d537
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.
5 #include "content/renderer/web_ui_mojo.h"
7 #include "content/common/view_messages.h"
8 #include "content/public/renderer/render_frame.h"
9 #include "content/public/renderer/render_view.h"
10 #include "content/renderer/web_ui_mojo_context_state.h"
11 #include "gin/per_context_data.h"
12 #include "third_party/WebKit/public/web/WebKit.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15 #include "v8/include/v8.h"
17 namespace content {
19 namespace {
21 const char kWebUIMojoContextStateKey[] = "WebUIMojoContextState";
23 struct WebUIMojoContextStateData : public base::SupportsUserData::Data {
24 scoped_ptr<WebUIMojoContextState> state;
27 } // namespace
29 WebUIMojo::MainFrameObserver::MainFrameObserver(WebUIMojo* web_ui_mojo)
30 : RenderFrameObserver(RenderFrame::FromWebFrame(
31 web_ui_mojo->render_view()->GetWebView()->mainFrame())),
32 web_ui_mojo_(web_ui_mojo) {
35 WebUIMojo::MainFrameObserver::~MainFrameObserver() {
38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext(
39 v8::Local<v8::Context> context,
40 int world_id) {
41 web_ui_mojo_->DestroyContextState(context);
44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() {
45 web_ui_mojo_->OnDidFinishDocumentLoad();
48 void WebUIMojo::MainFrameObserver::OnDestruct() {
51 WebUIMojo::WebUIMojo(RenderView* render_view)
52 : RenderViewObserver(render_view),
53 RenderViewObserverTracker<WebUIMojo>(render_view),
54 main_frame_observer_(this) {
57 WebUIMojo::~WebUIMojo() {
60 void WebUIMojo::CreateContextState() {
61 v8::HandleScope handle_scope(blink::mainThreadIsolate());
62 blink::WebLocalFrame* frame =
63 render_view()->GetWebView()->mainFrame()->toWebLocalFrame();
64 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
65 gin::PerContextData* context_data = gin::PerContextData::From(context);
66 WebUIMojoContextStateData* data = new WebUIMojoContextStateData;
67 data->state.reset(new WebUIMojoContextState(
68 render_view()->GetWebView()->mainFrame(), context));
69 context_data->SetUserData(kWebUIMojoContextStateKey, data);
72 void WebUIMojo::DestroyContextState(v8::Local<v8::Context> context) {
73 gin::PerContextData* context_data = gin::PerContextData::From(context);
74 if (!context_data)
75 return;
76 context_data->RemoveUserData(kWebUIMojoContextStateKey);
79 void WebUIMojo::OnDidFinishDocumentLoad() {
80 v8::HandleScope handle_scope(blink::mainThreadIsolate());
81 WebUIMojoContextState* state = GetContextState();
82 if (state)
83 state->Run();
86 WebUIMojoContextState* WebUIMojo::GetContextState() {
87 blink::WebLocalFrame* frame =
88 render_view()->GetWebView()->mainFrame()->toWebLocalFrame();
89 v8::HandleScope handle_scope(blink::mainThreadIsolate());
90 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
91 gin::PerContextData* context_data = gin::PerContextData::From(context);
92 if (!context_data)
93 return NULL;
94 WebUIMojoContextStateData* context_state =
95 static_cast<WebUIMojoContextStateData*>(
96 context_data->GetUserData(kWebUIMojoContextStateKey));
97 return context_state ? context_state->state.get() : NULL;
100 void WebUIMojo::DidCreateDocumentElement(blink::WebLocalFrame* frame) {
101 CreateContextState();
104 void WebUIMojo::DidClearWindowObject(blink::WebLocalFrame* frame) {
105 if (frame != render_view()->GetWebView()->mainFrame())
106 return;
108 // NOTE: this function may be called early on twice. From the constructor
109 // mainWorldScriptContext() may trigger this to be called. If we are created
110 // before the page is loaded (which is very likely), then on first load this
111 // is called. In the case of the latter we may have already supplied the
112 // handle to the context state so that if we destroy now the handle is
113 // lost. If this is the result of the first load then the contextstate should
114 // be empty and we don't need to destroy it.
115 WebUIMojoContextState* state = GetContextState();
116 if (state && !state->module_added())
117 return;
119 v8::HandleScope handle_scope(blink::mainThreadIsolate());
120 DestroyContextState(frame->mainWorldScriptContext());
123 } // namespace content