1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/renderer/web_ui_mojo_context_state.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/bind.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/stl_util.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/renderer/resource_fetcher.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/renderer/web_ui_runner.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "gin/converter.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "gin/modules/module_registry.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "gin/per_context_data.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "gin/public/context_holder.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "gin/try_catch.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/bindings/js/core.h"
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "mojo/bindings/js/handle.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/bindings/js/support.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebURLResponse.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/web/WebFrame.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/web/WebScriptSource.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::Context;
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::HandleScope;
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::Isolate;
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::Object;
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::ObjectTemplate;
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using v8::Script;
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// All modules have this prefixed to them when downloading.
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// TODO(sky): move this into some common place.
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const char kModulePrefix[] = "chrome://mojo/";
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RunMain(base::WeakPtr<gin::Runner> runner,
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)             v8::Handle<v8::Value> module) {
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  v8::Isolate* isolate = runner->GetContextHolder()->isolate();
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  v8::Handle<v8::Function> start;
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(gin::ConvertFromV8(isolate, module, &start));
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  runner->Call(start, runner->global(), 0, NULL);
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WebUIMojo -------------------------------------------------------------------
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame,
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             v8::Handle<v8::Context> context)
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : frame_(frame),
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      module_added_(false) {
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::PerContextData* context_data = gin::PerContextData::From(context);
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ContextHolder* context_holder = context_data->context_holder();
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  runner_.reset(new WebUIRunner(frame_, context_holder));
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::Runner::Scope scoper(runner_.get());
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry::From(context)->AddObserver(this);
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  runner_->RegisterBuiltinModules();
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global());
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Warning |frame| may be destroyed.
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(sky): add test for this.
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebUIMojoContextState::~WebUIMojoContextState() {
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::Runner::Scope scoper(runner_.get());
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry::From(
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      runner_->GetContextHolder()->context())->RemoveObserver(this);
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WebUIMojoContextState::Run() {
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ContextHolder* context_holder = runner_->GetContextHolder();
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry::From(context_holder->context())->LoadModule(
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      context_holder->isolate(),
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "main",
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      base::Bind(RunMain, runner_->GetWeakPtr()));
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) {
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::Runner::Scope scoper(runner_.get());
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ContextHolder* context_holder = runner_->GetContextHolder();
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      context_holder->context());
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < ids.size(); ++i) {
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (fetched_modules_.find(ids[i]) == fetched_modules_.end() &&
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        registry->available_modules().count(ids[i]) == 0) {
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      FetchModule(ids[i]);
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebUIMojoContextState::FetchModule(const std::string& id) {
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const GURL url(kModulePrefix + id);
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(sky): better error checks here?
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(url.is_valid() && !url.is_empty());
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(fetched_modules_.find(id) == fetched_modules_.end());
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fetched_modules_.insert(id);
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ResourceFetcher* fetcher = ResourceFetcher::Create(url);
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  module_fetchers_.push_back(fetcher);
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  fetcher->Start(frame_,
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 blink::WebURLRequest::RequestContextScript,
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 blink::WebURLRequest::FrameTypeNone,
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 ResourceFetcher::PLATFORM_LOADER,
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete,
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            base::Unretained(this),
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            fetcher));
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebUIMojoContextState::OnFetchModuleComplete(
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ResourceFetcher* fetcher,
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const blink::WebURLResponse& response,
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& data) {
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK_EQ(kModulePrefix,
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      response.url().string().utf8().substr(0, arraysize(kModulePrefix) - 1));
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::string module =
116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      response.url().string().utf8().substr(arraysize(kModulePrefix) - 1);
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // We can't delete fetch right now as the arguments to this function come from
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // it and are used below. Instead use a scope_ptr to cleanup.
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<ResourceFetcher> deleter(fetcher);
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  module_fetchers_.weak_erase(
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher));
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (data.empty()) {
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED();
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;  // TODO(sky): log something?
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  runner_->Run(data, module);
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebUIMojoContextState::OnDidAddPendingModule(
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& id,
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::vector<std::string>& dependencies) {
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FetchModules(dependencies);
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ContextHolder* context_holder = runner_->GetContextHolder();
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      context_holder->context());
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  registry->AttemptToLoadMoreModules(context_holder->isolate());
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
142