Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / dev_tools_controller.mm
1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/dev_tools_controller.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include <Cocoa/Cocoa.h>
11
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #import "chrome/browser/ui/cocoa/view_id_util.h"
16 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "ui/base/cocoa/base_view.h"
20 #include "ui/base/cocoa/focus_tracker.h"
21 #include "ui/gfx/size_conversions.h"
22
23 using content::WebContents;
24
25 @interface DevToolsContainerView : BaseView {
26   DevToolsContentsResizingStrategy strategy_;
27
28   // Weak references. Ownership via -subviews.
29   NSView* devToolsView_;
30   NSView* contentsView_;
31 }
32
33 - (void)setContentsResizingStrategy:
34     (const DevToolsContentsResizingStrategy&)strategy;
35 - (void)adjustSubviews;
36 - (void)showDevTools:(NSView*)devToolsView;
37 - (void)hideDevTools;
38
39 @end
40
41
42 @implementation DevToolsContainerView
43
44 - (void)setContentsResizingStrategy:
45     (const DevToolsContentsResizingStrategy&)strategy {
46   strategy_.CopyFrom(strategy);
47 }
48
49 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
50   [self adjustSubviews];
51 }
52
53 - (void)showDevTools:(NSView*)devToolsView {
54   NSArray* subviews = [self subviews];
55   DCHECK_EQ(1u, [subviews count]);
56   contentsView_ = [subviews objectAtIndex:0];
57   devToolsView_ = devToolsView;
58   // Place DevTools under contents.
59   [self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil];
60 }
61
62 - (void)hideDevTools {
63   DCHECK_EQ(2u, [[self subviews] count]);
64   [devToolsView_ removeFromSuperview];
65   contentsView_ = nil;
66   devToolsView_ = nil;
67 }
68
69 - (void)adjustSubviews {
70   if (![[self subviews] count])
71     return;
72
73   if (!devToolsView_) {
74     DCHECK_EQ(1u, [[self subviews] count]);
75     NSView* contents = [[self subviews] objectAtIndex:0];
76     [contents setFrame:[self bounds]];
77     return;
78   }
79
80   DCHECK_EQ(2u, [[self subviews] count]);
81   gfx::Rect new_devtools_bounds;
82   gfx::Rect new_contents_bounds;
83   ApplyDevToolsContentsResizingStrategy(
84       strategy_, gfx::Size(NSSizeToCGSize([self bounds].size)),
85       [self flipNSRectToRect:[devToolsView_ bounds]],
86       [self flipNSRectToRect:[contentsView_ bounds]],
87       &new_devtools_bounds, &new_contents_bounds);
88   [devToolsView_ setFrame:[self flipRectToNSRect:new_devtools_bounds]];
89   [contentsView_ setFrame:[self flipRectToNSRect:new_contents_bounds]];
90 }
91
92 @end
93
94 @interface DevToolsController (Private)
95 - (void)showDevToolsView;
96 - (void)hideDevToolsView;
97 @end
98
99
100 @implementation DevToolsController
101
102 - (id)init {
103   if ((self = [super init])) {
104     devToolsContainerView_.reset(
105         [[DevToolsContainerView alloc] initWithFrame:NSZeroRect]);
106     [devToolsContainerView_
107         setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
108   }
109   return self;
110 }
111
112 - (NSView*)view {
113   return devToolsContainerView_.get();
114 }
115
116 - (void)updateDevToolsForWebContents:(WebContents*)contents
117                          withProfile:(Profile*)profile {
118   DevToolsWindow* newDevToolsWindow = contents ?
119       DevToolsWindow::GetDockedInstanceForInspectedTab(contents) : NULL;
120
121   bool shouldHide = devToolsWindow_ && devToolsWindow_ != newDevToolsWindow;
122   bool shouldShow = newDevToolsWindow && devToolsWindow_ != newDevToolsWindow;
123
124   if (shouldHide)
125     [self hideDevToolsView];
126
127   devToolsWindow_ = newDevToolsWindow;
128   if (devToolsWindow_) {
129     const DevToolsContentsResizingStrategy& strategy =
130         devToolsWindow_->GetContentsResizingStrategy();
131     devToolsWindow_->web_contents()->GetView()->SetOverlayView(
132         contents->GetView(),
133         gfx::Point(strategy.insets().left(), strategy.insets().top()));
134     [devToolsContainerView_ setContentsResizingStrategy:strategy];
135   } else {
136     DevToolsContentsResizingStrategy zeroStrategy;
137     [devToolsContainerView_ setContentsResizingStrategy:zeroStrategy];
138   }
139
140   if (shouldShow)
141     [self showDevToolsView];
142
143   [devToolsContainerView_ adjustSubviews];
144   if (shouldHide || shouldShow)
145     [[devToolsContainerView_ window] disableScreenUpdatesUntilFlush];
146 }
147
148 - (void)showDevToolsView {
149   focusTracker_.reset(
150       [[FocusTracker alloc] initWithWindow:[devToolsContainerView_ window]]);
151
152   // |devToolsView| is a WebContentsViewCocoa object, whose ViewID was
153   // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to
154   // VIEW_ID_DEV_TOOLS_DOCKED here.
155   NSView* devToolsView =
156       devToolsWindow_->web_contents()->GetView()->GetNativeView();
157   view_id_util::SetID(devToolsView, VIEW_ID_DEV_TOOLS_DOCKED);
158
159   [devToolsContainerView_ showDevTools:devToolsView];
160 }
161
162 - (void)hideDevToolsView {
163   devToolsWindow_->web_contents()->GetView()->RemoveOverlayView();
164   [devToolsContainerView_ hideDevTools];
165   [focusTracker_ restoreFocusInWindow:[devToolsContainerView_ window]];
166   focusTracker_.reset();
167 }
168
169 @end